morphology, area open and close

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
alosca
Posts: 52
Joined: 2010-03-04T17:54:57-07:00
Authentication code: 8675308
Location: Pasadena, California

morphology, area open and close

Post by alosca »

Is there any equivalent of area opening and area closing in IM ?
These are operators that can remove and fill binary regions having at least a given number of pixels (area size). They are very useful to remove undesired tiny spots before e.g. a thinning operation. They are shape independent so defining proper morphological kernels is not really feasible.

cheers,
- alex.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology, area open and close

Post by fmw42 »

see -morphology at http://www.imagemagick.org/Usage/morphology/ It includes open and close operations as well as dilate and erode, thin, etc
alosca
Posts: 52
Joined: 2010-03-04T17:54:57-07:00
Authentication code: 8675308
Location: Pasadena, California

Re: morphology, area open and close

Post by alosca »

Thanks for the link and for documenting the morphology suite. I have been using it extensively and it has been very very useful.

Area opening and closing are different from opening and closing with a structured element (kernel) as they are not shape related but simply area related (sometimes called 'area morphology' in mathematical morphology lingo). The usage is to remove objects of a given size or smaller in the image, or, equivalently, paint the area with foreground (closing) or background (opening) color in binary images (it can also be used in gray scale images: paint the area with immediately underneath color).

Below is a figure extracted from an article that uses area opening and closing for image segmentation ("Robust iris segmentation on uncalibrated noisy images using mathematical morphology", Luengo-Oroz et.al., 2009). Note how areas below a certain size (1000 pixels or less) disappear - read it from left to right (f and g are original images):

Image

This would be something very useful in the morphology package as it is a much used tool in many problems/applications (I current use standalone programs, aopen and aclose). Maybe we can request the developers to add these to their ever growing TODO list ? I can imagine having something like

convert img -morphology AreaOpen value
convert img -morphology AreaClose value
convert img -morphology AreaOpenClose value (since they are usually done in pairs)

This might entail implementing a 'connected components' solution, which I think it is not implemented yet (?).

- alex.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology, area open and close

Post by fmw42 »

cross post this to the developers forum
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: morphology, area open and close

Post by anthony »

Moved topic - left a shadow link from Users forum


I would need to know a lot more about how it should be implemented to add it.

Do you have a web page link to the paper? Or perhaps you can mail it to me at
A.Thyssen AT griffith.edu.au

However be warned I have very little time (at least until XMas) for programming. Morphology was added while I was on vacation in china!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
alosca
Posts: 52
Joined: 2010-03-04T17:54:57-07:00
Authentication code: 8675308
Location: Pasadena, California

Re: morphology, area open and close

Post by alosca »

Here is the classical reference to the area opening and closing topic:

http://citeseerx.ist.psu.edu/viewdoc/do ... 1&type=pdf

The idea is more or less the following (area closing for binary images): find the connected components in the image and paint with background color those regions with area less than or equal to a given value. Area opening is the same for the negated image. And in the gray scale case you work with each gray level as if each were a binary image and proceed as above. These are expressed in the paper. I imagine that since flood fill is already implemented, this should not be a big leap. The extra work is to count the number of pixels per region and flood fill those satisfying the area size constraint.

Having the connected components by itself is already a big plus since it is the basic operation necessary to distinguish the various regions in an image. Usually one generates an output image for the connected components where each region/component has a different gray level value (leaving 0 for background which is also a connected region). The largest value + 1 gives the number of connected components/regions in the entire image. It is easy to see that the histogram of this generated connected components image gives the area in pixels of each region.

In one of my works for example I have to query each connected component separately to find out its geometric properties (area, perimeter, diameter, etc.) and according to computed values do something conditionally (discard or accept the region).

In

http://alumni.media.mit.edu/~rahimi/connected/

you will find source code. A tutorial is at

http://homepages.inf.ed.ac.uk/rbf/HIPR2/label.htm

Thanks for taking the time to take a look at these.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: morphology, area open and close

Post by anthony »

Thanks I will study it. Though As I mentioned I don't have a lot of time.
I have added it to my ToDo list. Though other programmers are welcome to implement it. IM is after all, an Open Source project.

Oh I see it is closely related to the connected components morphology segmentation, with the added measure of pixel counting. yes I do plan to implement something like this, though I can see now that I will need to also include some pixel counting to the algorithm.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: morphology, area open and close

Post by fmw42 »

I don't know if this is useful for some future reference -- opinions welcome. But one could compute the various higher order moments (zeroth moment is area as I recall) of each connected component. See http://en.wikipedia.org/wiki/Image_moments This would allow culling on other moments besides count or area. Something to consider down the road when/if this gets included.

I demonstrate something similar for one closed graylevel region. But the computation is less straighforward than using connected components and counts, etc. See http://www.fmwconcepts.com/imagemagick/ ... shape_mean
Post Reply