Python 如果黑色像素在8个或更少的簇中,则移除它们

Python 如果黑色像素在8个或更少的簇中,则移除它们,python,pandas,opencv,numpy,Python,Pandas,Opencv,Numpy,我不知道如何解决这个问题,我有一个黑白像素列表,如 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

我不知道如何解决这个问题,我有一个黑白像素列表,如

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)
将numpy导入为np
my_list=np.array([255]*100)
我的清单[10:17]=0
我的清单[20:30]=0
打印(我的列表)
del_list=[]
计数=0
当前删除列表=[]
对于迭代器,枚举中的值(my_列表):
如果值<255:
当前\u del\u列表+=[迭代器]
计数+=1
打印(当前删除列表)
其他:
如果计数>8:
计数=0
当前删除列表=[]
其他:
删除列表+=当前删除列表
当前删除列表=[]
计数=0
我的列表[删除列表]=255
我的清单=清单(我的清单)
打印(我的列表)

对于列表中的每个元素,我正在测试它是否低于255。如果是,计数器将递增。如果在下一个255次上升时,该计数器低于8,则会将当前位置列表添加到
delu列表中。随后,在
my\u列表中,
del\u列表中位置的所有值都设置为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])

这可能会有所帮助-我的第一个方法中有一个bug。现在我的例子起作用了。只需运行我答案中的代码。