当分离 < 1 px 时使用 python 中的 PIL 检测图像形状边缘的算法

Algorithm for detecting edge of shape in image with PIL in python when separation < 1 px

这是一个很容易解决的问题,我正在尝试重新排列平面中的形状,但首先我需要以正确的方式检测它们,我想出了这个非常低效的算法但是它可以很好地完成工作,直到它到达两个形状,它们之间的距离 < 1px:

这里有 python 伪代码:

#all pixels
for x in range(0, image.width):
    for y in range(0, image.height):

        if pixel is black:
            # mark start of shapes

        else:
            if shape is open:
                for r in range (0, image.height):
                    if pixel is black:
                        # found shape, keep shape open
                    else:
                        # close shape
            else:
                for r in range (0, image.height):
                    paint pixel gray # this draws the vertical gray lines in the example

这是生成的图像:

如您所见,灰色条绘制在形状之间,但当两个形状靠得太近(彼此之间的距离小于 1px)时它不起作用

重要提示:我不需要对垂直重叠的形状进行这项工作。

我不太关心 python/pillow 语法,如果你能很好地解释你的算法是做什么的,它是如何工作的,它看起来像 python / PIL 代码。

听起来您想同时查看当前像素列和前一列。如果两列中都没有黑色的 y 位置,则它是一个新形状(或没有形状)。