Problem with using sp.ndimage.label to get largest object in a binary image

87 views
Skip to first unread message

Matteo

unread,
Mar 7, 2016, 11:06:07 AM3/7/16
to scikit-image
Hello there

I'm putting together some code to grab an image with a map, colorbar, and possibly text and other elements, guess what the largest object in the image is -assuming it is the map - crop the image to the map extent, then reduce the number of colors in the map.
My Jupiter notebook is at this location on GitHub: 

The issue I have with this code is that:

the output of [9] is an array that is all zeroes, although as far as I know it should not. Can anyone suggest why this is happening?

I am sure it is something I am overlooking in my code/understanding of image processing.

Thanks
Matteo

Juan Nunez-Iglesias

unread,
Mar 8, 2016, 1:18:25 AM3/8/16
to scikit...@googlegroups.com
Hi Matteo,

It would be useful if you showed us the notebook being run with the problematic image, as well as the original.

Having said that, it looks like perhaps you should replace this line:
mask_sizes[0] = 0

with

sizes[0] = 0

higher up. Perhaps your background is bigger than your foreground in the problem image. =)

Juan.

PS: Incidentally, have a look at skimage.morphology.remove_small_objects and skimage.morphology.remove_small_holes (this last one is in the just-released 0.12 version).

PPS: Also incidentally, for ndimage we often use the convention "from scipy import ndimage as ndi"

PPPS: Also, skimage.io.imread can directly load images from URLs. =)

--
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/b5e56392-b181-4f70-a090-115a14ffe05c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matteo

unread,
Mar 8, 2016, 9:59:36 AM3/8/16
to scikit-image
Hello Juan

Thanks for your reply.
Here is a second notebook with the problem image to look at. https://github.com/mycarta/rainbowbot/blob/master/crop_image_reduce_colors_text_test.ipynb

I have not had time to change anything except for running it, and won't be able until tonight. When I do, I will look into your suggestions, and recommendations, and any other comments you may have from seeing the notebook.
But, so, the background counts when it comes to labeling/size? I had assumed the operations would only apply to bright elements, that's the misunderstanding.

Also, being here, the way, is there a better way, or a dedicated operation, to get the largest object/blob in a binary image?

Cheers,
Matteo

Juan Nunez-Iglesias

unread,
Mar 8, 2016, 5:45:16 PM3/8/16
to scikit...@googlegroups.com
Hi Matteo,

ndimage.label keeps 0 as 0 (background) and then labels every nonzero blob as 1, 2, ... n. Then, numpy.bincount is completely agnostic as to whether you are looking at images or any other kind of array, and is just going to count the values, including 0.

As I mentioned, remove_small_objects does what you want but you need to find out the size of the largest blob and pass that in as the min_size parameter.

Juan.

Matteo Niccoli

unread,
Mar 9, 2016, 12:50:19 PM3/9/16
to scikit...@googlegroups.com
Thanks again Juan

Once I've had a chance to experiment with these options and get a polished, working notebook running, I will re-post it.
Matteo

--
You received this message because you are subscribed to a topic in the Google Groups "scikit-image" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scikit-image/2_OZaXf2xEs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scikit-image...@googlegroups.com.

To post to this group, send email to scikit...@googlegroups.com.

Matteo Niccoli

unread,
Mar 10, 2016, 11:19:52 PM3/10/16
to scikit...@googlegroups.com
Hello Juan

I've cleaned up the notebook and moved things around a bit. But mainly, I modified the ndimaged.label part (thanks to you for my better understanding of how it works), and also  replaced the masking with remove_small_objects. here's my new notebook:

The one part that puzzled me for a bit is that things did not work when I used skimage.io.imread to import the image, but then I figured out it was because of the alpha channel...
:-)

Inline image 1

Cheers,
Matteo

On Tue, Mar 8, 2016 at 3:44 PM, Juan Nunez-Iglesias <jni....@gmail.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "scikit-image" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scikit-image/2_OZaXf2xEs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scikit-image...@googlegroups.com.

To post to this group, send email to scikit...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages