Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检测背景/前景反转的区域_C++_Opencv - Fatal编程技术网

C++ 检测背景/前景反转的区域

C++ 检测背景/前景反转的区域,c++,opencv,C++,Opencv,如何检测背景和前景反转的区域(文本为黑色背景,白色背景)。我想反转这些区域,使所有文本为白色背景,黑色背景 有3个区域的文本为黑色,背景为白色 我的方法是这样: 有些结构存储已经处理过的像素 编写一个例程bool属于\u black\u text(常量像素坐标和坐标);,该例程将应用于图像中的每个像素。根据处理坐标的方式替换相应的参数 检查像素是否已处理,如果已处理,则返回(我们需要这样做以节省时间) 检查像素是否为黑色,否则返回 查找黑色像素的连接组件。将其所有像素标记为已处理 查找与黑色组件

如何检测背景和前景反转的区域(文本为黑色背景,白色背景)。我想反转这些区域,使所有文本为白色背景,黑色背景

有3个区域的文本为黑色,背景为白色


我的方法是这样:

有些结构存储已经处理过的像素

编写一个例程
bool属于\u black\u text(常量像素坐标和坐标);
,该例程将应用于图像中的每个像素。根据处理坐标的方式替换相应的参数

  • 检查像素是否已处理,如果已处理,则返回(我们需要这样做以节省时间)
  • 检查像素是否为黑色,否则返回
  • 查找黑色像素的连接组件。将其所有像素标记为已处理
  • 查找与黑色组件相邻的所有白色像素的连接组件的并集。将其所有像素标记为已处理
  • 检查白色组件的平截面积是否大于黑色连接组件的平截面积,如果大于黑色连接组件,则表示它包围了黑色组件,因此黑色组件为文本
  • 检查白色组件的平截体是否具有一定的最小尺寸,以排除它是带有黑洞的白色字母
  • 相应地行动(颠倒那些)
  • 由于第6步也反转了白色分量,我们需要处理两幅图像,原始图像和结果,我们需要原始图像来保留白色分量

    一个改进是以某种方式标记文本周围先前的白色组件,这样我们就不必为每个新字母重新找到该组件

    编辑:将单个白色连接组件更改为与黑色组件相邻的所有白色像素的连接组件的并集。否则,白色组件可能类似于某个字母中的眼睛

    注意:也许更好的方法是从白色像素开始,创建连接的组件,然后找出白色组件中的每个连接组件,并反转整个内容

    编辑:添加了另一个步骤,现在是步骤6

    伪代码:

    鉴于:

    Pixel: both coordinate and content
    PixelList: stores a list of Pixel
    Greyscale: a pixel can be evaluated to it's greyscale value within 0..1
    is black: true iff Greyscale < 0.5
    is white: not is black
    Image: 2D-array of Pixel, accessed via image(pixel)
    minimal_size: to be too big to be assumed to be a letter
    
    算法:

    instantiate 2D-array of bool that is named handled, same size as image, default value false
    instantiate a copy of image named result
    
    for each pixel in image:
        if handled(pixel) continue
        if pixel is white continue
    
        PixelList black_component = connected_component(pixel, is black)
        PixelList white_seed = adjacent(black_component)
        PixelList white_component
        for each white_pixel in white seed:
            merge connected_component(white_pixel, is white) into white_component
    
        handled(white_component) = true
        handled(black_component) = true
    
        if Frustrum(white_component) < minimal_size continue
        if Frustrum(black_component) is within Frustrum(white_component)
            invert white_component and black_component in result
    
    实例化名为handled的bool的二维数组,与image大小相同,默认值为false
    实例化名为result的映像副本
    对于图像中的每个像素:
    如果已处理(像素),请继续
    如果像素为白色,则继续
    PixelList黑色\u组件=连接的\u组件(像素,为黑色)
    像素列表白色种子=相邻(黑色分量)
    像素列表白光组件
    对于白色种子中的每个白色像素:
    将连接的_组件(白色_像素,为白色)合并到白色_组件中
    已处理(白色分量)=真
    已处理(黑色分量)=真
    如果圆台(白色组件)<最小尺寸继续
    如果平截面(黑色分量)在平截面(白色分量)内
    在结果中反转白色分量和黑色分量
    
    只需获取图像的最大值,然后执行,image=(maxValue-image).看看这个:我不想反转整个图像..只有在白色背景上文本为黑色的区域,你必须搜索包含一些黑色像素的较大的白色矩形,并将其反转。可能你必须设置最小的矩形大小,以避免将字母/小图片与白色矩形区域混淆。它可以ems很难进行字体识别。白线内部没有黑色像素,不会受到影响。输入图像的格式是什么?格式:
    CV_8UC1
    你能举个例子吗?@clarkk你的意思是实现整个算法吗?不,这是你的工作。你决定如何检测黑色文本的一些标准,然后去吧。顺便说一句,在你的示例图像中,所有有问题的区域都是矩形的,如果你可能得到的所有图像都有这个属性,你只需搜索白色矩形即可。@clarkk我的算法错误,请等待更新。Edit:修复它。如果我的算法有什么不清楚的地方,请提问。如何计算数字of轮廓中的非零像素(非边界框)@clarkk我不理解你在这方面的问题。什么是非零像素?某种颜色?为什么要使用轮廓?一般来说,在这种情况下,你会做的是,从左到右,每当你穿过轮廓线时,升起一个计数器,当计数器当前存储一个像素时,将每个像素计算在轮廓内n奇数。
    instantiate 2D-array of bool that is named handled, same size as image, default value false
    instantiate a copy of image named result
    
    for each pixel in image:
        if handled(pixel) continue
        if pixel is white continue
    
        PixelList black_component = connected_component(pixel, is black)
        PixelList white_seed = adjacent(black_component)
        PixelList white_component
        for each white_pixel in white seed:
            merge connected_component(white_pixel, is white) into white_component
    
        handled(white_component) = true
        handled(black_component) = true
    
        if Frustrum(white_component) < minimal_size continue
        if Frustrum(black_component) is within Frustrum(white_component)
            invert white_component and black_component in result