如果黑色像素在8或更少的簇中删除



我不确定如何解决这个问题,我有一个黑白像素的列表,例如;

x = [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255   0   0   0 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255   0   0   0   0   0   0 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255   0   0 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
 255]

我正在寻找一种与此类似的事情的python的方式,

if x[2] = 0 
then look ahead 8 places, 
if all x[2] to x[10] are 0 and x[11] = 255 change all to 255
if all are not 0 
see if all x[2] to x[9] are zero 
...
...
...
see if x[2] to x[3] are zero

到目前为止我似乎没有工作

for w in range(len(x)):
        if x[w] == 0 and x[w+8] == 0 and x[w+9] == 255:
            print thresh1[h][w]
        elif x[w] == 0 and x[w+7] == 0 and x[w+8] == 255:
            print thresh1[h][w]
        elif x[w] == 0 and x[w+6] == 0 and x[w+8] == 255:
            print thresh1[h][w]
        elif x[w] == 0 and x[w+5] == 0 and x[w+8] == 255:
            print thresh1[w]
        elif x[w] == 0 and x[w+4] == 0 and x[w+8] == 255:
            print thresh1[w]
        elif x[w] == 0 and x[w+3] == 0 and x[w+8] == 255:
            print thresh1w]
        elif x[w] == 0 and x[w+2] == 0 and x[w+8] == 255:
            print thresh1[h][w]
        elif x[w] == 0 and x[h][w+1] == 0 and x[w+8] == 255:
            print thresh1[h][w]
        elif x[w] == 0 and x[w+1] == 255:
            print x[w]

任何帮助都非常感谢。

此解决方案似乎有些复杂,可能会有所改进,但可以使用:

import numpy as np
my_list = np.array([255]*100)
my_list[10:17] = 0
my_list[20:30] = 0
print(my_list)
del_list = []
count = 0
current_del_list = []
for iterator, value in enumerate(my_list):
    if value < 255:
        current_del_list += [iterator]
        count+=1
        print(current_del_list)
    else:
        if count > 8:
            count = 0
            current_del_list = []
        else:
            del_list += current_del_list
            current_del_list = []
            count=0
my_list[del_list]=255
my_list = list(my_list)
print(my_list)

对于列表中的每个元素,我正在测试其低于255。如果是,则计数器会增加。如果此计数器在下一个255时低于8,则将其添加到del_list的当前位置列表。后来,del_list中的所有值在my_list中设置为255。

您可以使用形态开放操作。

In [131]: from scipy.ndimage.morphology import grey_opening
In [132]: grey_closing(np.array(x), structure=np.ones(8))
Out[132]:
array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255])

最新更新