从图像中删除像素数为 X 或更少的形状

发布于 2024-10-07 04:11:15 字数 112 浏览 7 评论 0原文

如果我有一张图像,比如说正方形。是否可以删除由 10 个(非白色)像素或更少像素形成的所有形状并保留由 11 个像素或更多像素形成的所有形状?我想以编程方式或使用命令行来完成此操作。

提前致谢!

If I have a image with, let's say squares. Is it possible to remove all shapes formed by 10 (non white) pixels or less and keep all shapes that is formed by 11 pixels or more? I want to do it programmatically or with a command line.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谁对谁错谁最难过 2024-10-14 04:11:16

一种称为侵蚀的算法可能会有用。它适用于布尔图像,缩小所有“真实”区域,删除其表面像素的一层。涂抹几次,小区域消失,大区域仍然存在(尽管缩小了)。使用相反的算法膨胀(对图像的逻辑补集应用侵蚀)来缩小幸存者。通过测试像素是否位于“对象”内部(无论您如何定义)来找到一种定义布尔图像的方法,并找到一种将结果应用到原始图像以将不需要的小对象更改为背景颜色的方法。

更具体地说,需要查看示例。

Possibly an algorithm called Erosion may be useful. It works on boolean images, shrinking all areas of "true" removing one layer of their surface pixels. Apply a few times, and small areas disappear, bigger ones remain (though shrunken). De-shrink the survivors with the opposite algorithm, dilation (apply erosion to the logical complement of the image). Find a way to define a boolean images by testing if a pixel is inside an "object" however you define it, and find a way to apply the results to the original image to change the unwanted small objects to the background color.

To be more specific would require seeing examples.

烟雨凡馨 2024-10-14 04:11:16

查找洪水填充算法并更改它们以计算像素而不是填充。然后,如果形状足够小,则用白色填充它。

Look up flood fill algorithms and alter them to count the pixels instead of filling. Then if the shape is small enough, fill it with white.

优雅的叶子 2024-10-14 04:11:16

有几种方法可以解决这个问题。您所指的通常称为文档成像应用中的去斑。文档扫描仪在扫描过程中经常会在图像中引入大量污垢和噪音,因此必须将其去除,以帮助提高 OCR 准确性。

我假设您正在此处处理黑白图像,或者可以将图像转换为黑白图像,否则它会变得更加复杂。去斑是通过分析页面上的所有斑点来完成的。决定斑点大小的另一种方法是决定宽度、高度和组合像素数。

Leptonica.com - 是一个基于 C 的开源库,具有您需要的 blob 分析功能。通过一些简单的检查和循环,您可以删除这些较小的对象。 Leptonica 也可以很容易地编译成命令行程序。有很多示例程序,这是学习 Leptionica 的最佳方式。

为了进行测试,您可能需要尝试 ImageMagick。它有一个用于去斑的命令行选项,但没有其他参数。
http://www.imagemagick.org/script/command-line-options .php#despeckle

另一种选择是在 Google 中查找“去斑点”算法。

There are a couple of ways to approach this. What you are referring to is commonly called Despeckle in Document Imaging Applications. Document scanners often introduce a lot of dirt and noise into an image during scanning and so this must be removed removed to help improve OCR accuracy.

I assume you are processing B/W images here or can convert your image to B/W otherwise it becomes a lot more complex. Despeckle is done by analysing all the blobs on the page. Another way to decide on blob size is to decide on width, height and number of pixels combined.

Leptonica.com - Is an Open Source C based library that has the blob analysis functions you require. With some simple check and loops you can delete these smaller objects. Leptonica can also be compiled quite easily into a command line program. There are many example programs and that is the best way to learn Leptionica.

For testing, you may want to try ImageMagick. It has a command line option for despeckle but it has no further parameters.
http://www.imagemagick.org/script/command-line-options.php#despeckle

The other option is to look for "despeckle" algorithms in Google.

戏蝶舞 2024-10-14 04:11:16

ImageMagick 从版本 6.8.9-10 开始,包含一个 -connected-components 选项,可用于执行您想要的操作,但是来自 示例在官方网站中提供,如何实际获取减去已删除的连通分量的原始图像并不是立即显而易见的。

我几乎确信有一种更简单的方法,但我是通过执行一系列步骤的笨重脚本来完成的:

  • 首先,我从连接的组件示例中运行命令:

    转换为.png \
      -定义连接组件:verbose=true \
      -连接组件 8 out.png
    
  • 这会产生以下格式的输出:

    对象(id:边界框质心区域平均颜色):
    (...)
    181: 9x9+1601+916 1605.2,920.2 44 灰色(0)
    185: 5x5+1266+923 1268.0,925.0 13 灰色(0)
    274: 5x5+2276+1661 2278.0,1663.0 13 灰度(255)
    
  • 然后,我使用 awk 仅过滤包含黑色分量区域(以像素为单位)的行(平均颜色 gray(0) 在我的图像中)小于我的阈值 $min_cc_area。请注意,connected-components 可以选择过滤小于给定区域的组件,但我需要相反的功能。 awk 行类似于以下内容:

    {if ($4 < $min_cc_area && $5=="gray(0)") { print $2 }}
    
  • 然后,我继续为 ImageMagick 创建一个命令行,在这些连接组件的顶部绘制白色矩形。 -draw 命令需要 x1,y1 x2,y2 形式的坐标,因此我再次使用 awk 根据 [w 格式的坐标计算坐标]x[h]+x1+y1-connected-components 给出:

    awk '{print "白色填充矩形 " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }'
    
  • 最后,我运行创建的 ImageMagick 命令行来创建一个组合所有白色矩形的新图像 。

最后,我得到了以下脚本:

# usage: $0 infile min_cc_area outfile
infile=$1
min_cc_area=$2
outfile=$3
awk_exp="{if (\$4 < $min_cc_area && \$5==\"gray(0)\") { print \$2 }}"

draw_rects=""
draw_rects+=$(convert $infile -define connected-components:verbose=true \
  -connected-components 8 null: | \
  awk "$awk_exp" | tr 'x+' '  ' | \
  awk '{print " rectangle " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }')

convert $infile -draw "fill white $draw_rects" $outfile

请注意,如果已删除的 CC 与已删除组件的边界矩形相交,则此解决方案可能会擦除已删除 CC 附近的黑色像素。

ImageMagick, starting from version 6.8.9-10, includes a -connected-components option which can be used to do what you want, however from the example provided in the official website, it is not immediately obvious how to actually obtain the original image minus the removed connected components.

I'm almost sure there is a simpler way, but I did it via a clunky script performing a series of steps:

  • First, I ran the command from the connected components example:

    convert in.png \
      -define connected-components:verbose=true \
      -connected-components 8 out.png
    
  • This produces output in the following format:

    Objects (id: bounding-box centroid area mean-color):
    (...)
    181: 9x9+1601+916 1605.2,920.2 44 gray(0)
    185: 5x5+1266+923 1268.0,925.0 13 gray(0)
    274: 5x5+2276+1661 2278.0,1663.0 13 gray(255)
    
  • Then, I used awk to filter only the lines containing an area (in pixels) of black components (mean-color gray(0) in my image) smaller than my threshold $min_cc_area. Note that connected-components has an option to filter components smaller than a given area, but I needed the opposite. The awk line is similar to the following:

    {if ($4 < $min_cc_area && $5=="gray(0)") { print $2 }}
    
  • I then proceeded to create a command-line for ImageMagick where I drew white rectangles on top of these connected components. The -draw command expects coordinates in the form x1,y1 x2,y2, so I used awk again to compute the coordinates from the ones in the format [w]x[h]+x1+y1 given by -connected-components:

    awk '{print "white fill rectangle " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }'
    
  • Finally, I ran the created ImageMagick command-line to create a new image combining all the white rectangles on top of the original one.

In the end, I got the following script:

# usage: $0 infile min_cc_area outfile
infile=$1
min_cc_area=$2
outfile=$3
awk_exp="{if (\$4 < $min_cc_area && \$5==\"gray(0)\") { print \$2 }}"

draw_rects=""
draw_rects+=$(convert $infile -define connected-components:verbose=true \
  -connected-components 8 null: | \
  awk "$awk_exp" | tr 'x+' '  ' | \
  awk '{print " rectangle " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }')

convert $infile -draw "fill white $draw_rects" $outfile

Note that this solution may erase black pixels near the removed CC's, if they insersect the bounding rectangle of the removed component.

若相惜即相离 2024-10-14 04:11:16

您需要一个连接组件标记算法。它将扫描图像并为每个连接的形状提供一个 ID 号,并为每个像素分配一个其所属形状的 ID 号。

运行连接组件过滤器后,只需计算分配给每个对象的像素,找到像素少于 10 个的对象,并将这些对象中的像素替换为白色。

You want a connected components labeling algorithm. It will scan through the image and give every connected shape an id number, as well as assign every pixel an id number of what shape it belongs to.

After running a connected components filter, just count the pixels assigned to each object, find the objects that have less than 10 pixels, and replace the pixels in those objects with white.

只是一片海 2024-10-14 04:11:16

如果您可以使用 openCV,这段代码可以实现您想要的功能(即,despakle)。您可以在第一行中使用 Size(3,3) 参数来消除更大或更小的噪声伪影。

Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3,3));
morphologyEx(image, image, MORPH_OPEN, element);
morphologyEx(image, image, MORPH_CLOSE, element);

If you can use openCV, this piece of code does what you want (i.e., despakle). You can play w/ parameters of Size(3,3) in the first line to get rid of bigger or smaller noisy artifacts.

Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3,3));
morphologyEx(image, image, MORPH_OPEN, element);
morphologyEx(image, image, MORPH_CLOSE, element);
情深如许 2024-10-14 04:11:16

您只想计算出每个组件的面积。因此,8 向跟踪算法可能会有所帮助。我有一个用 C++ 编码的 API 解决这个问题。如果您愿意,请给我发电子邮件。

You just want to figure out the area of each components. So an 8-direction tracking algorithm could help. I have an API solve this problem coded in C++. If you want, send me an email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文