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

Matteo Niccoli matteo.niccoli at gmail.com
Wed Mar 9 12:50:18 EST 2016


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

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

> 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.
>
> On Wed, Mar 9, 2016 at 1:59 AM, Matteo <matteo.niccoli at gmail.com> wrote:
>
>> 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
>>
>> On Monday, March 7, 2016 at 11:18:25 PM UTC-7, Juan Nunez-Iglesias wrote:
>>>
>>> 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. =)
>>>
>>> On Tue, Mar 8, 2016 at 3:06 AM, Matteo <matteo.... at gmail.com> wrote:
>>>
>>>> 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:
>>>>
>>>> https://github.com/mycarta/rainbowbot/blob/master/crop_image_reduce_colors.ipynb
>>>>
>>>> The issue I have with this code is that:
>>>> If I run it with the current test image (
>>>> https://github.com/mycarta/rainbowbot/blob/master/stuff4matching_cmap_notebooks/test1.png)
>>>> or even a nimage with map touching the border(
>>>> https://github.com/mycarta/rainbowbot/blob/master/stuff4matching_cmap_notebooks/test2.png)
>>>> the code seems to work.
>>>>
>>>> However, in the case of an image with larger blocks of text (
>>>> https://github.com/mycarta/rainbowbot/blob/master/stuff4matching_cmap_notebooks/test_with_text.png
>>>> )
>>>> 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
>>>>
>>>> --
>>>> 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... at googlegroups.com.
>>>> To post to this group, send email to scikit... at 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
>>>> <https://groups.google.com/d/msgid/scikit-image/b5e56392-b181-4f70-a090-115a14ffe05c%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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+unsubscribe at googlegroups.com.
>> To post to this group, send email to scikit-image at googlegroups.com.
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/scikit-image/057d7338-f94a-4151-8200-0bf3edeb50dc%40googlegroups.com
>> <https://groups.google.com/d/msgid/scikit-image/057d7338-f94a-4151-8200-0bf3edeb50dc%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> 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+unsubscribe at googlegroups.com.
> To post to this group, send email to scikit-image at googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/scikit-image/CA%2BJHcKSGywM5%2BLKA-fniCebFs3arTy4_y48Oq0JTh7hqc5r%3DBg%40mail.gmail.com
> <https://groups.google.com/d/msgid/scikit-image/CA%2BJHcKSGywM5%2BLKA-fniCebFs3arTy4_y48Oq0JTh7hqc5r%3DBg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20160309/309876e9/attachment.html>


More information about the scikit-image mailing list