图像二值化

3

裁剪图像 1

裁剪图像 2

二值化图像

很抱歉图片大小。

现在,我有数据集中的这两个裁剪图像。 我将把这些图像输入到机器学习算法中。 在这样做之前,我想提取二值化数字,并将二进制图像馈入算法,而不是直接馈入原始图像。您能详细说明如何实现这种干净的二值化吗?

我尝试了大津和其他阈值方法,但它们无法在图像中给出清晰的数字。


我只尝试过otsu,但效果并不好。我想要像我上面分享的大图中的“2”。我不需要与上面图像完全相同的东西,任何比otsu更清晰的东西都可以。 - user3206070
你能否发布详细的步骤吗? - user3206070
提供的输入图像永远不会产生像您发布的示例那样的“干净”二进制图像。 - Piglet
这些图片看起来像是来自于一个验证码。 - Bonzo
@Bonzo:不,这些是SVHN数据集的一部分。 - user3206070
2个回答

6

我曾经尝试过使用二色量化、灰度转换和归一化处理,虽然我不知道它在你的其余图片中的效果如何。

我只是用ImageMagick在命令行上进行了操作,具体如下:

convert input.png +dither -colors 3 -colors 2 -colorspace gray -normalize -scale 250x result.png

所以,它加载图像并禁用抖动,这样后续的量化只会产生2种实际颜色而不是抖动混合物。然后,我将其量化为3种颜色 - 仍在RGB颜色空间中 - 然后进一步降至2种颜色。然后,我将这两种颜色转换为灰度,并对其进行归一化,使较暗的颜色变为黑色,较亮的颜色变为白色。

enter image description here

enter image description here


哇,这些结果很棒,但我需要在Python/OpenCV中完成。你知道ImageMagick在这里找到这两种颜色时会使用什么算法/代码吗? - user3206070

4

在ImageMagick中,一个替代Mark Setchell建议的方法是转向OpenCV。OpenCV具有自适应阈值处理,请参见https://docs.opencv.org/3.3.1/d7/d4d/tutorial_py_thresholding.html和连通组件处理,请参见https://docs.opencv.org/3.1.0/d3/dc0/group__imgproc__shape.html#gac2718a64ade63475425558aa669a943a以及https://www.pyimagesearch.com/2016/10/31/detecting-multiple-bright-spots-in-an-image-with-python-and-opencv/

1) convert to grayscale
2) stretch image to full dynamic range
3) apply local (adaptive) thresholding
4) optionally use connected components labelling to remove regions smaller than some total number of pixels (area).


convert 2.png \
-colorspace gray \
-auto-level \
-lat 20x20+10% \
2_lat.gif

enter image description here

convert 19.png \
-colorspace gray \
-auto-level \
-negate \
-lat 20x20+5% \
19_lat.gif

enter image description here

在这里进行可选的连接组件处理:
convert 2_lat.gif \
-define connected-components:area-threshold=40 \
-define connected-components:mean-color=true \
-connected-components 4 \
2_lat_ccl.gif

enter image description here

convert 19_lat.gif \
-define connected-components:area-threshold=20 \
-define connected-components:mean-color=true \
-connected-components 4 \
19_lat_ccl.gif

enter image description here

为了使图像更加平滑,您可能需要使用矢量化工具(例如potrace)将栅格图像转换为矢量图像。

好的!请继续发下去吧! - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接