利用PIL / Pillow快速算法去除图像中的杂散像素

时间:2015-08-19 22:08:19

标签: python algorithm math image-manipulation pillow

我需要从动态生成的图像中移除孤独/杂散像素,也就是说,不属于宽度/高度超过4像素的任何形状的像素,这只是一个例子,仅仅是一个例子。说明一个,但问题是一样的。

enter image description here

正如你所看到的,我有很多"杂散像素"我需要从我的图像中删除,每个不属于中心形状的黑点每个都是1像素,有时会有两个黑色像素或更多像这样

enter image description here

现在,想象一下我们正在检查像素,我们有:(我们正在检查的像素总是5)

enter image description here

我需要制作5个白色,因为2和6没有连接。

所以,例如在这种情况下:

enter image description here

它不应该是5白色,因为它旁边有3个像素。

我认为会这样做,不是吗?

到目前为止,我的算法效率非常低,我需要检查巨大的图像,我需要快速完成,这是迄今为止看起来如何(伪代码)

# all pixels in the image
for x in range(image_width):
    for y in range(image_height):

        # all 8 pixels surrounding the pixel we are stepped into
        for r in range (x -1, x + 1) and k in range (y -1, y + 1)):
            if pixel[r, k] is black:
               black_pixels += 1
               if black_pixels > 2:
                  #make it white
                  pixel[x, y] = (255, 255, 255)
                  #stop
                  break

Haven没有对代码进行测试,但它应该有用,虽然这可能对我需要的速度来说太慢了,我需要一个可以从我的图像中删除杂散像素的快速算法,如{{ 3}}

任何建议都非常受欢迎。

0 个答案:

没有答案