Re: Segmentation and Granulometry with scikit-image

1,120 views
Skip to first unread message

Emmanuelle Gouillart

unread,
Sep 21, 2012, 4:19:12 PM9/21/12
to scikit...@googlegroups.com, ashz
Hi,

the good news is that it is possible to perform the same
operations with functions from skimage, plus some functions from
scipy.ndimage (not in skimage to avoid code duplication). Below is a
correspondence table that you can use for your applications. You can use
the online doc to know more about these functions on
http://scikits-image.org/docs/dev/api/api.html. You may also want to read
the segmentation tutorial on
http://scikits-image.org/docs/dev/user_guide/tutorial_segmentation.html

Matlab <-> skimage (or scipy.ndimage)

imread skimage.io.imread
edge skimage.filter.canny
strel skimage.morphology.selem
imdilate skimage.morphology.dilation
imfill ndimage.binary_fill_holes
imclearborder skimage.segmentation.clear_border (**)
imerode skimage.morphology.erosion
bwperim skimage.measure.find_contours or
ndimage.morphological_gradient
adapthisteq skimage.exposure.equalize
imadjust skimage.exposure.rescale_intensity
imopen skimage.morphology.opening

(**) only in the development version, the code is on
https://github.com/scikits-image/scikits-image/blob/master/skimage/segmentation/_clear_border.py

Hope this is enough to get you started!

Cheers,
Emmanuelle


On Wed, Sep 19, 2012 at 04:01:06AM -0700, ashz wrote:
> Hi,

> I am very interested in python image analysis and especially in
> Scikit-image.

> As a newbie would it be possible to get an explanation how to convert the
> Matlab codes below to python+scikit-image.

> Detecting a Cell Using Image Segmentation:
> http://www.mathworks.com/help/images/examples/detecting-a-cell-using-image-segmentation.html

> Granulometry of Snowflakes:
> http://www.mathworks.com/products/image/examples.html?file=/products/demos/shipping/images/ipexsnow.html

> Thanks a lot.

Stéfan van der Walt

unread,
Sep 21, 2012, 4:46:10 PM9/21/12
to scikit...@googlegroups.com
On Wed, Sep 19, 2012 at 4:01 AM, ashz <ashz...@gmail.com> wrote:

> Granulometry of Snowflakes:
> http://www.mathworks.com/products/image/examples.html?file=/products/demos/shipping/images/ipexsnow.html

Tony, do you think the morphological reconstruction would work for
this? Otherwise perhaps one of the segmentation algorithms.
Translating the example line-by-line is pretty easy, except that we do
not have adaptive histogram equalization implemented yet.
This one is straightforward, except that convex_hull_objects would
have made it simpler. We should also spruce up
skimage.segmentation.visualize_boundaries, which could have been of
use here.

https://gist.github.com/3763784

Regards
Stéfan

Tony Yu

unread,
Sep 24, 2012, 6:57:38 PM9/24/12
to scikit...@googlegroups.com
On Fri, Sep 21, 2012 at 8:46 PM, Stéfan van der Walt <ste...@sun.ac.za> wrote:
On Wed, Sep 19, 2012 at 4:01 AM, ashz <ashz...@gmail.com> wrote:

> Granulometry of Snowflakes:
> http://www.mathworks.com/products/image/examples.html?file=/products/demos/shipping/images/ipexsnow.html

Tony, do you think the morphological reconstruction would work for
this?

Yes and no. Morphological reconstruction (specifically, h-dome, a.k.a. h-maxima or regional max) tries to enhance features (maxima) based on the local region. So the h-dome of a dim snowflake and bright snowflake will look similar if they have a similar peakiness. In the original example, dim peaks are still much dimmer than the bright peaks, even after the adaptive histogram equalization. 

Also, the h-dome of an image will tend to remove the corona-like blurring around the larger snowflakes since the middle is so much brighter than the surrounding region. I've attached an image of the results, along with the code to generate it (although you can't run it without the snowflakes image, which I probably shouldn't email). You'll notice that dim peaks are much brighter than in the example, and the large snowflakes are smaller (because of suppression of the corona region). These differences alter the intensity curve quite a bit (I'm not sure what the "right" output is, though).
 
Otherwise perhaps one of the segmentation algorithms.
Translating the example line-by-line is pretty easy, except that we do
not have adaptive histogram equalization implemented yet.

BTW, there's a pending PR to add adaptive histogram equalization. (Although, I'm not sure how I feel about the large header file required for that PR.)

As for the original post about these Matlab examples: Thanks for bringing this up, ashz! These are exactly the type of examples I'd like to see in the user guide: Something that takes the user all the way from image loading to data extraction. We'd probably want to write our own instead of simply translating the Matlab examples. It'd be great if people on this list could provide examples from their own research.

Best,
-Tony

 
Inline image 1

# Code to generate image above
# Note: this won't run without image from Matlab install:
#     toolbox/images/imdemos/snowflakes.png


from skimage import io
from skimage import img_as_float
import skimage.morphology as morph
from skimage import exposure
from scipy import ndimage


# snowflake image from a Matlab install
img = io.imread('snowflakes.png')
# convert from uint8 to float since we'll be subtracting (prevent underflow)
orig = img_as_float(img)

img = exposure.equalize(img)
img = ndimage.gaussian_filter(img, 1)

# look for regional maxima greater than surrounding regions by h
h = 0.2
img_background = morph.reconstruction(img-h, img)
img_hdome = img - img_background
img = exposure.rescale_intensity(img_hdome)

io.imshow(img)
# io.imsave('snowflakes_enhanced.png', img)
io.show()
 
 

image.png

Dan

unread,
Dec 15, 2012, 1:58:27 PM12/15/12
to scikit...@googlegroups.com, scikit...@googlegroups.com
Dear Tony,

Thanks for the script.

I was running the script and I got this error:
Traceback (most recent call last):
  File "C:/Users/DK/PProjects/skimage_seg/scikit_seg.py", line 18, in <module>
    img_background = morph.reconstruction(img-h, img)
AttributeError: 'module' object has no attribute 'reconstruction'

Process finished with exit code 1

Any reason why?

Thanks a lot,
Dan

Tony Yu

unread,
Dec 15, 2012, 4:24:31 PM12/15/12
to scikit...@googlegroups.com, scikit...@googlegroups.com
On Sat, Dec 15, 2012 at 1:58 PM, Dan <dk...@walla.co.il> wrote:
Dear Tony,

Thanks for the script.

I was running the script and I got this error:
Traceback (most recent call last):
  File "C:/Users/DK/PProjects/skimage_seg/scikit_seg.py", line 18, in <module>
    img_background = morph.reconstruction(img-h, img)
AttributeError: 'module' object has no attribute 'reconstruction'

Process finished with exit code 1

Any reason why?

Thanks a lot,
Dan

Hi Dan,

Which version of scikit-image are you running? You'll need at least 0.7 (the latest stable release). You can check the version with the following shell command:

    $ python -c "import skimage; print skimage.__version__"

-Tony 

--
 
 

Dan

unread,
Dec 15, 2012, 6:45:29 PM12/15/12
to scikit...@googlegroups.com, scikit...@googlegroups.com
Hi,

Thanks for the rapid respnse, I am using 0.7.2 on windows.

The error is now:

 Traceback (most recent call last):
  File "C:/Users/DK/PProjects/skimage_seg/scikit_seg.py", line 3, in <module>
    import skimage.morphology as morph
  File "C:\Python27\lib\site-packages\skimage\morphology\__init__.py", line 7, in <module>
    from ._skeletonize import skeletonize, medial_axis
ImportError: cannot import name skeletonize

Any idea why?.

Thanks

Tony Yu

unread,
Dec 16, 2012, 9:41:35 AM12/16/12
to scikit...@googlegroups.com
On Sat, Dec 15, 2012 at 6:45 PM, Dan <dk...@walla.co.il> wrote:
Hi,

Thanks for the rapid respnse, I am using 0.7.2 on windows.

The error is now:

 Traceback (most recent call last):
  File "C:/Users/DK/PProjects/skimage_seg/scikit_seg.py", line 3, in <module>
    import skimage.morphology as morph
  File "C:\Python27\lib\site-packages\skimage\morphology\__init__.py", line 7, in <module>
    from ._skeletonize import skeletonize, medial_axis
ImportError: cannot import name skeletonize

Any idea why?.

Thanks


Hi Dan,

How did you install scikit-image? Also, what's the output of the following?

import skimage
print skimage.__version__
print skimage.__file__

-Tony

Dan

unread,
Dec 16, 2012, 10:36:13 AM12/16/12
to scikit...@googlegroups.com
Hi,

The output:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import skimage
>>> print skimage.__version__
0.7.2
>>> print skimage.__file__
C:\Python27\lib\site-packages\skimage\__init__.pyc
>>>

Thanks

Dan

unread,
Dec 16, 2012, 10:41:43 AM12/16/12
to scikit...@googlegroups.com
Hi,

I installed Windows binaries (kindly provided by Christoph Gohlke)- file name:scikit-image-0.7.2.win32-py2.7.‌exe

Thanks

Tony Yu

unread,
Dec 16, 2012, 11:23:00 PM12/16/12
to scikit...@googlegroups.com
On Sun, Dec 16, 2012 at 10:41 AM, Dan <dk...@walla.co.il> wrote:
Hi,

I installed Windows binaries (kindly provided by Christoph Gohlke)- file name:scikit-image-0.7.2.win32-py2.7.exe


Thanks

El domingo, 16 de diciembre de 2012 10:36:13 UTC-5, Dan escribió:
Hi,

The output:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import skimage
>>> print skimage.__version__
0.7.2
>>> print skimage.__file__
C:\Python27\lib\site-packages\skimage\__init__.pyc
>>>

Thanks


Hmm... that all looks good to me.

Maybe something went wrong during the install? This is bizarre, because `_skeletonize` is a regular python file (not cython, so it's not a build issue). If you look in 

"C:\Python27\lib\site-packages\skimage\morphology\"

is there a "_skeletonize.py" and does it like the original (i.e. does it have a `skeletonize` function)?
 
-Tony

--
 
 

Dan

unread,
Dec 20, 2012, 3:33:39 AM12/20/12
to scikit...@googlegroups.com
Dear Tony,

The files were a match. I uninstalled python and the other libraries, installed them again and it works fine now. I do not what was the problem.

Thanks a lot for the help.

Dan
Reply all
Reply to author
Forward
0 new messages