2013-06-05 29 views
8

我有一個graysacle png圖像,我想從我的圖像中提取所有連接的組件。 某些組件具有相同的強度,但我想爲每個對象分配一個唯一的標籤。 這裏是我的形象從Python中的圖像中提取連接的對象

enter image description here

我試過這段代碼:

img = imread(images + 'soccer_cif' + str(i).zfill(6) + '_GT_index.png') 
labeled, nr_objects = label(img) 
print "Number of objects is %d " % nr_objects 

但我得到的只有三個使用該對象。 請告訴我如何獲取每個對象。

+0

'label'函數來自哪裏? –

+0

可能的解決方案:http://stackoverflow.com/a/5304140/190597 – unutbu

+0

我正在使用類似的東西。標籤功能是從scipy.ndimage 但得到的結果,我張貼 – Khushboo

回答

8

J.F. Sebastian shows a way來識別圖像中的對象。它要求手動選擇高斯模糊半徑和閾值,但是:

import scipy 
from scipy import ndimage 
import matplotlib.pyplot as plt 

fname='index.png' 
blur_radius = 1.0 
threshold = 50 

img = scipy.misc.imread(fname) # gray-scale image 
print(img.shape) 

# smooth the image (to remove small objects) 
imgf = ndimage.gaussian_filter(img, blur_radius) 
threshold = 50 

# find connected components 
labeled, nr_objects = ndimage.label(imgf > threshold) 
print "Number of objects is %d " % nr_objects 

plt.imsave('/tmp/out.png', labeled) 
plt.imshow(labeled) 

plt.show() 

enter image description here

隨着blur_radius = 1.0,這個發現4個對象。 隨着blur_radius = 0.5,5名對象中發現:

enter image description here

+0

嗯,我沒有嘗試高斯模糊較早。 這種方法效果更好。謝謝 :) – Khushboo

0

如果對象的邊界是完全清楚的,你必須在IMG的二進制圖像,就可以避免高斯濾波和剛做這行:

labeled, nr_objects = ndimage.label(img)