Counting objects using label in skimage.measure

731 views
Skip to first unread message

Arctic_python

unread,
Nov 18, 2015, 11:02:52 PM11/18/15
to scikit-image
Hello,
I am trying to count the number of objects(that has "ones") in a binary array. Of course I do not expect to count holes.
But when I run the below code,
file1='4.csv'
a=np.loadtxt(open(file1,'rb'),delimiter=',',dtype=int)
#print (a.shape)

img=measure.label(a)
propsa = measure.regionprops(img)
length = len(propsa)
print ('length='+str(length))
for label in propsa:
    print (label.centroid)

returns

length=2
(214.23444957510378, 505.25546156532539)
(238.77173913043478, 740.28260869565213)
I get two objects. From reading the centroid coordinates(above), it seems it is counting the center of the white object and the cavity (can be seen in the image bellow). 
Why is this algorithm counting cavities and not only objects that are of "ones"? Is there an argument to enforce only objects and not cavities?
Attached is the csv file if you want to try for yourself.
Thanks





4.png
4.csv
Auto Generated Inline Image 1

Johannes Schönberger

unread,
Nov 18, 2015, 11:34:30 PM11/18/15
to scikit...@googlegroups.com
Hi,

set background=0 when calling the label function.

Best, Johannes

<Auto Generated Inline Image 1.png>

--
You received this message because you are subscribed to the Google Groups "scikit-image" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image...@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/18179150-16c2-4d34-b56f-8f3e6d91401f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<4.png><4.csv><Auto Generated Inline Image 1.png>

Arctic_python

unread,
Nov 19, 2015, 5:55:33 PM11/19/15
to scikit-image
Hello Johannes,
thanks for the advise, but it did not work. When I set the background=0, the script counted one object when there were two. According to the one object coordinate, with centroid prop=(214.59826983468628, 505.59264087219293), it missed the small dot in the bottom of the image.



Trying to understand what is the algorithm dose, I tested the following code:

a=np.array(np.matrix('0 1 0 0 1;0 1 0 0 0; 0 0 0 0 0;0 0 0 0 1'))
print(a)

import numpy as np
from skimage import filters, morphology, measure

img=measure.label(a, background=0)

propsa = measure.regionprops(img)
length = len(propsa)
print ('length='+str(length))
for label in propsa:
    print (label.centroid)

returns
0 1 0 0 1]
 [0 1 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 1]]
length=2
(0.0, 4.0)
(3.0, 4.0)

When trying
a=np.array(np.matrix('0 1 0 0 1;0 1 0 0 0; 0 0 0 0 0;0 0 0 0 1'))
print(a)
import numpy as np
from skimage import filters, morphology, measure



img=measure.label(a)
propsa = measure.regionprops(img)
length = len(propsa)
print ('length='+str(length))
for label in propsa:
    print (label.centroid)

returns
[[0 1 0 0 1]
 [0 1 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 1]]
length=3
(0.5, 1.0)
(0.0, 4.0)
(3.0, 4.0)

I do not understand why when background=0  the program counts two objects, and three object when background is default.
Thanks for the help
Auto Generated Inline Image 1
Reply all
Reply to author
Forward
0 new messages