簡體   English   中英

如果黑色像素在8個或更少的群集中,則將其刪除

[英]Removing black pixel if they are in a cluster of 8 or less

我不確定如何解決這個問題,我有一個黑白像素列表,例如;

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 以后,將my_list del_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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM