在抖动图像中将像素聚集到具有相同颜色的区域

Cluster pixels into region with same color in dithered images

我需要在相似图像中定位密集区域,其中密度取决于像素之间的距离。距离小于某个阈值的像素将被包含在一个组中。白框显示我想要的结果。欢迎任何想法或现有算法。

首先我要说我从来没有解决过这个特定的问题。但这是一个我认为可行的想法。

我会列出具有您想要的颜色的每个像素的坐标。然后我也会将它们放入 quad-tree structure。然后我将浏览列表并将每个像素与小于或等于所讨论距离的四边形内的任何像素进行比较。这应该会显着减少您需要进行的比较次数。

我只是在命令行上使用 ImageMagick 进行了一些实验,它在大多数 Linux 发行版中可用,并且可用于 macOS 和 Windows。

首先,我删除了你的白色注释并设置了黑白阈值:

convert PaOdy.jpg -fill black -fuzz 20% -opaque white -threshold 20% clean.png

然后我尝试了模糊 3x3 和阈值:

convert clean.png -blur 3x3 -threshold 20% z.png

然后我尝试了模糊 5x5 和阈值:

convert clean.png -blur 5x5 -threshold 20% z.png

然后我尝试使用模糊、阈值和膨胀来填充形状:

convert clean.png -blur 5x5 -threshold 20% -morphology dilate disk:5 z.png

显然,您可以根据需要改变大小和阈值。然后我尝试 "Connected Components Analysis"(也称为 "labelling" 和 "Blob Analysis")来查找像素块,如下所示:

convert clean.png -blur 5x5 -threshold 20%  \
     -morphology dilate disk:5              \
     -define connected-components:verbose=1 \
     -connected-components 8 -auto-level    \
     result.png

输出

Objects (id: bounding-box centroid area mean-color):
  0: 627x459+0+0 309.0,225.9 269780 srgb(0,0,0)
  18: 145x72+326+387 392.5,421.0 7378 srgb(255,255,255)      <--- This blob
  5: 87x58+304+60 349.5,90.2 2194 srgb(255,255,255)
  11: 128x35+239+186 300.2,202.2 2011 srgb(255,255,255)
  1: 52x41+447+36 471.1,54.7 1107 srgb(255,255,255)
  8: 43x21+441+170 462.9,180.3 678 srgb(255,255,255)
  2: 28x32+502+37 514.2,50.5 611 srgb(255,255,255)
  17: 26x31+134+298 145.9,313.4 608 srgb(255,255,255)
  22: 52x24+373+435 396.6,451.7 513 srgb(0,0,0)
  14: 27x24+187+231 199.6,242.1 433 srgb(255,255,255)
  10: 30x18+385+181 399.0,189.0 431 srgb(255,255,255)
  6: 22x26+565+83 575.2,95.5 400 srgb(255,255,255)
  21: 31x17+409+418 421.9,425.0 298 srgb(0,0,0)
  4: 18x19+536+50 544.4,58.8 243 srgb(255,255,255)
  15: 19x17+60+261 69.1,269.3 221 srgb(255,255,255)
  3: 17x13+602+49 609.6,54.8 162 srgb(255,255,255)
  9: 15x14+422+179 429.0,185.8 147 srgb(255,255,255)
  23: 16x10+99+449 106.8,454.2 128 srgb(255,255,255)
  13: 14x13+224+216 230.4,221.9 127 srgb(255,255,255)
  7: 13x12+212+101 218.0,106.5 116 srgb(255,255,255)
  16: 12x11+126+274 131.5,279.0 92 srgb(255,255,255)
  12: 11x12+19+201 24.0,206.5 92 srgb(255,255,255)
  20: 5x7+377+415 379.5,417.4 20 srgb(0,0,0)
  19: 2x2+397+407 397.7,407.3 3 srgb(0,0,0)

希望你能在图像中看到每个 "blob" 都有自己的颜色(灰色阴影),这意味着 blob 中的所有像素都被标记为相同数.

如果我们现在分析文本输出,例如,在这样的行中查找:

18: 145x72+326+387 392.5,421.0 7378 srgb(255,255,255)

您可以看到它宽 145 像素,高 72 像素,从左上角坐标 326,387 开始。然后我们看到它的面积和颜色等。让我们用红色框起来:

convert clean.png -stroke red -fill none -draw "rectangle 326,387 471,458 " z.png


我只是简单地在命令行上分享想法,无需编译等。您可以使用 OpenCV 在 C++ 中实现上述内容,或者在 Python 中使用 OpenCV,或者在 C++ 中使用 CImg,或者Python 使用 PIL(枕头)。