Discussion:
Detect largest area of a figure in a b/w image with Mahotas
Mark w
2013-06-04 15:58:48 UTC
Permalink
There is this function in [Mahotas][1]: `bwperim` that returns an image
with only objects' contours.
How can I count contours and calculate the area of the objects and get a
new image with only the largest object?
Maybe similar to [opencv findcontours][2] and [cv2.contoursarea][3]

![image sample][4]


code:

path = 'image/2aJM6eB.jpg'
fork = mh.imread(path)
imgbnbin = fork[:,:,0]
bfork = imgbnbin < 150 # binarization
bfork = mh.morph.dilate(bfork, disk7)
bfork = mh.morph.close(bfork, disk3)
bwfork = mh.bwperim(bfork)


image after binarization, dilation,closing and bwperim:
![image after binarization, dilation,closing and bwperim][5]


[1]: https://pypi.python.org/pypi/mahotas
[2]:
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
[3]:
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#double%20contourArea%28InputArray%20contour,%20bool%20oriented%29
[4]: Loading Image...
[5]: Loading Image...
--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
jip
2013-06-05 07:38:24 UTC
Permalink
Hi, here's a solution (ipython notebook)<Loading Image...>.
The result is :<https://lh3.googleusercontent.com/-NO00LMnt1vw/Ua7pcfTnynI/AAAAAAAABZA/uCzDGhzXon8/s1600/convexhull_largestblob.png>
However, there may be an easier way to find the largest part of the image.

jean-pat
Post by Mark w
There is this function in [Mahotas][1]: `bwperim` that returns an image
with only objects' contours.
How can I count contours and calculate the area of the objects and get a
new image with only the largest object?
Maybe similar to [opencv findcontours][2] and [cv2.contoursarea][3]
![image sample][4]
path = 'image/2aJM6eB.jpg'
fork = mh.imread(path)
imgbnbin = fork[:,:,0]
bfork = imgbnbin < 150 # binarization
bfork = mh.morph.dilate(bfork, disk7)
bfork = mh.morph.close(bfork, disk3)
bwfork = mh.bwperim(bfork)
![image after binarization, dilation,closing and bwperim][5]
[1]: https://pypi.python.org/pypi/mahotas
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#double%20contourArea%28InputArray%20contour,%20bool%20oriented%29
[4]: http://i.stack.imgur.com/CCKqV.jpg
[5]: http://i.stack.imgur.com/Ymh53.png
--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
jip
2013-06-05 07:41:25 UTC
Permalink
sorry for the link: http://nbviewer.ipython.org/5712192
Post by Mark w
There is this function in [Mahotas][1]: `bwperim` that returns an image
with only objects' contours.
How can I count contours and calculate the area of the objects and get a
new image with only the largest object?
Maybe similar to [opencv findcontours][2] and [cv2.contoursarea][3]
![image sample][4]
path = 'image/2aJM6eB.jpg'
fork = mh.imread(path)
imgbnbin = fork[:,:,0]
bfork = imgbnbin < 150 # binarization
bfork = mh.morph.dilate(bfork, disk7)
bfork = mh.morph.close(bfork, disk3)
bwfork = mh.bwperim(bfork)
![image after binarization, dilation,closing and bwperim][5]
[1]: https://pypi.python.org/pypi/mahotas
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#double%20contourArea%28InputArray%20contour,%20bool%20oriented%29
[4]: http://i.stack.imgur.com/CCKqV.jpg
[5]: http://i.stack.imgur.com/Ymh53.png
--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Mark w
2013-06-07 00:53:46 UTC
Permalink
Thank you! I tried to delete small dots from image and then find largest
object in the image but i get a black image!
I tried this because i need to delete dots, do some stuff, and find largest
object.
Where Do i wrong?

path = 'categoriearcheo/sextans/g (000).jpg'
fork = mh.imread(path)
bfork = fork[:,:,0]<230

lab, n = mh.label(bfork)
labsizes = mh.labeled.labeled_size(lab)[1:]
print("labsizes")
print(labsizes)

labsizes = np.delete(labsizes,np.where(labsizes < 10)[0])

print("NUOVI labsizes")
print(labsizes)
size_largest_blob = sorted(labsizes)[-1]
label_largest_blob = np.where(np.asarray(labsizes)==size_largest_blob)

largest_blob = lab== label_largest_blob[0][0]+1
plt.gray()
plt.title("largest_blob")
plt.imshow(largest_blob)
plt.show()
--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Jean-Patrick Pommier
2013-06-07 12:14:14 UTC
Permalink
I realize that my przvious answer was not very good, there's an example for
too big particle here
http://pythonhosted.org/mahotas/labeled.html?highlight=labelled, so
removing too small particles could be done the same way.
Post by Mark w
Thank you! I tried to delete small dots from image and then find largest
object in the image but i get a black image!
I tried this because i need to delete dots, do some stuff, and find
largest object.
Where Do i wrong?
path = 'categoriearcheo/sextans/g (000).jpg'
fork = mh.imread(path)
bfork = fork[:,:,0]<230
lab, n = mh.label(bfork)
labsizes = mh.labeled.labeled_size(lab)[1:]
print("labsizes")
print(labsizes)
labsizes = np.delete(labsizes,np.where(labsizes < 10)[0])
print("NUOVI labsizes")
print(labsizes)
size_largest_blob = sorted(labsizes)[-1]
label_largest_blob = np.where(np.asarray(labsizes)==size_largest_blob)
largest_blob = lab== label_largest_blob[0][0]+1
plt.gray()
plt.title("largest_blob")
plt.imshow(largest_blob)
plt.show()
--
You received this message because you are subscribed to the Google Groups
"pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...