从 node.js 中的扫描图像评估复选框

Posted

技术标签:

【中文标题】从 node.js 中的扫描图像评估复选框【英文标题】:Evaluate check box from a scanned image in node.js 【发布时间】:2016-06-20 13:01:06 【问题描述】:

我想从扫描的图像中评估复选框是否被选中。为此,我找到了像 node-dv 和 node-fv 这样的节点模块。但是什么时候安装这个我在mac上得到了以下错误。

../deps/opencv/modules/core/src/arithm1.cpp:444:51: error: constant expression evaluates to 4294967295 which cannot be narrowed to type 'int' [-Wc++11-narrowing]
static int CV_DECL_ALIGNED(16) v64f_absmask[] =  0xffffffff, 0x7fffffff, 0xffffffff, 0x7fffffff ;
                                                  ^~~~~~~~~~
../deps/opencv/modules/core/src/arithm1.cpp:444:51: note: insert an explicit cast to silence this issue
static int CV_DECL_ALIGNED(16) v64f_absmask[] =  0xffffffff, 0x7fffffff, 0xffffffff, 0x7fffffff ;
                                                  ^~~~~~~~~~
                                                  static_cast<int>( )
../deps/opencv/modules/core/src/arithm1.cpp:444:75: error: constant expression evaluates to 4294967295 which cannot be narrowed to type 'int' [-Wc++11-narrowing]
static int CV_DECL_ALIGNED(16) v64f_absmask[] =  0xffffffff, 0x7fffffff, 0xffffffff, 0x7fffffff ;
                                                                          ^~~~~~~~~~
../deps/opencv/modules/core/src/arithm1.cpp:444:75: note: insert an explicit cast to silence this issue
static int CV_DECL_ALIGNED(16) v64f_absmask[] =  0xffffffff, 0x7fffffff, 0xffffffff, 0x7fffffff ;
                                                                          ^~~~~~~~~~
                                                                          static_cast<int>( )
2 errors generated.
make: *** [Release/obj.target/libopencv/deps/opencv/modules/core/src/arithm1.o] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/entapzian/.nvm/versions/node/v4.3.1/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)

上述依赖是解决我问题的最佳方法吗?如果不是,请建议我一个好的解决方案。

【问题讨论】:

每张扫描图像中的复选框是否位于完全相同的位置?如果是这样,我很乐意发布一个简单的方法来确定它们的状态。 @aecend 是的复选框在所有图像中具有相同的模式 【参考方案1】:

对不起,我昨天和今天一直很忙。这是一个示例,它抓取图像的预定义区域并确定复选框是填充还是空。这只是一个起点,可能还有很大的改进空间,但如果扫描的图像质量不错,它应该可以工作。

第一步是获取图像的像素。接下来,您通过根据模式抓取它们来获取图像中包含复选框的区域。最后,通过将图像中该区域的平均亮度与未选中框的基线亮度进行比较来评估是否选中了复选框。

我建议使用get-pixels Node.js 包来获取图像像素。

这是一个示例,您可以根据自己的需要进行调整:

var get_pixels = require(‘get-pixels’);
var image_uri = 'path_to_image';

get_pixels(image_uri, process_image);

var pattern_width = 800, // Width of your pattern image
    pattern_height = 1100; // Height of your pattern image

// The pattern image doesn't need to be loaded, you just need to use its dimensions to reference the checkbox regions below
// This is only for scaling purposes in the event that the scanned image is of a higher or lower resolution than what you used as a pattern.

var checkboxes = [
    x1: 10, y1: 10, x2: 30, y2: 30, // Top left and bottom right corners of the region containing the checkbox
    x1: 10, y1: 60, x2: 30, y2: 80
];

// You'll need to get these by running this on an unchecked form and logging out the adjusted_average of the regions
var baseline_average = ??, // The average brightness of an unchecked region
    darkness_tolerance = ??; // The offset below which the box is still considered unchecked

function process_image(err, pixels) 

    if (!err) 

        var regions = get_regions(pixels);

        var checkbox_states = evaluate_regions(regions);

        // Whatever you want to do with the determined states

    else
        console.log(err);
        return;
    



function get_regions(pixels) 

    var regions = [], // Array to hold the pixel data from selected regions
        img_width = pixels.shape[0], // Get the width of the image being processed
        img_height = pixels.shape[1], // Get the height
        scale_x = img_width / pattern_width, // Get the width scale difference between pattern and image (for different resolution scans)
        scale_y = img_height / pattern_height; // Get the height scale difference

    for (var i = 0; i < checkboxes.length; i++) 

        var start_x = Math.round(checkboxes[i].x1 * scale_x),
            start_y = Math.round(checkboxes[i].y1 * scale_y),
            end_x = Math.round(checkboxes[i].x2 * scale_x),
            end_y = Math.round(checkboxes[i].y2 * scale_y),
            region = [];

        for (var y = start_y; y <= end_y; y++) 
            for (var x = start_x; y <= end_x; x++) 
                region.push(
                    pixels.get(x, y, 0), // Red channel
                    pixels.get(x, y, 1), // Green channel
                    pixels.get(x, y, 2), // Blue channel
                    pixels.get(x, y, 3) // Alpha channel
                );
            
        

        regions.push(region);

    

    return regions;



function evaluate_regions(regions) 

    var states = [];

    for (var i = 0; i < regions.length; i++) 

        var brightest_value = 0,
            darkest_value = 255,
            total = 0;

        for (var j = 0; j < regions[i].length; j+=4) 

            var brightness = (regions[i][j] + regions[i][j + 1] + regions[i][j + 2]) / 3; // Pixel brightness
            if (brightness > brightest_value) brightest_value = brightness;
            if (brightness < darkest_value) darkest_value = brightness;
            total += brightness;

        

        var adjusted_average = (total / (regions[i].length / 4)) - darkest_value; // Adjust contrast
        var checked = baseline_average - adjusted_average > darkness_tolerance ? true : false;

        states.push(checked);

    

    return states;

 

【讨论】:

@azhar 我认为这可能会满足您的需求。如果您需要任何帮助来调整它以适应您的项目,请随时告诉我,我很乐意提供帮助。我过去曾使用过这样的流程,而且效果出奇的好。 我认为这是迄今为止最好的答案,感谢您的回复。

以上是关于从 node.js 中的扫描图像评估复选框的主要内容,如果未能解决你的问题,请参考以下文章

如何将复选框值从 HTML 发送到 Node JS?

Java Swing,尝试用图像图标复选框替换 JTable 中的布尔复选框

图像作为 Python 中的复选框

jQuery评估复选框是不是被选中并包含类true [重复]

Node.js+Web TWAIN,实现Web文档扫描和图像上传

如何在不使用android中的图像的情况下增加复选框边框