2017-01-16 20 views

回答

1

如果圖像始終這口井相連,你可以選擇基於其大小的組件。我在Python代碼(可能是一個更簡單的方法,但這是我如何做到這一點):

#get all connected components in the image with their stats (including their size, in pixel) 
nb_edges, output, stats, _ = cv2.connectedComponentsWithStats(img, connectivity=8) 
#output is an image where every component has a different value  
size=stats[1:,-1] #extracting the size from the statistics 

#selecting bigger components 
for e in range(0,nb_edges-1): 
    #replace this line depending on your application, here I chose to keep 
    #all components above the mean size of components in the image 
    if size[e]>=np.mean(size): 
     th_up = e + 2 
     th_do = th_up 

     #masking to keep only the components which meet the condition 
     mask = cv2.inRange(output, th_do, th_up) 
     result = cv2.bitwise_xor(original_img, mask) 
+0

謝謝!但是,您可以將其更改爲C++。 Python與opencv3有一些錯誤。而且我也不熟悉Python。對不起 –

+0

連接組件是最好的方式。 @BryanYU請不要期望人們爲你寫明確的代碼。有足夠的文檔可用於將Python轉換爲C++,反之亦然 –

+0

感謝您的建議。 –