I have a graysacle png image and I want to extract all the connected components from my image. Some of the components have same intensity but I want to assign a unique label to every object. here is my image

enter image description here

I tried this code:

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

But I get just three objects using this. Please tell me how to get each object.

share|improve this question
    
Where does the label function come from? – Janne Karila Jun 5 '13 at 10:45
    
Possible solution: stackoverflow.com/a/5304140/190597 – unutbu Jun 5 '13 at 10:52
    
I am using something similar actually. The label function is from scipy.ndimage But getting the result that I posted – Khushboo Jun 5 '13 at 11:45
up vote 7 down vote accepted

J.F. Sebastian shows a way to identify objects in an image. It requires manually choosing a gaussian blur radius and threshold value, however:

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

With blur_radius = 1.0, this finds 4 objects. With blur_radius = 0.5, 5 objects are found:

enter image description here

share|improve this answer
    
Hmm, I didn't try Gaussian blurring earlier. This method works better. Thanks :) – Khushboo Jun 5 '13 at 13:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.