select by color and shape

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
john123
Posts: 1
Joined: 2014-04-16T06:14:59-07:00
Authentication code: 6789

select by color and shape

Post by john123 »

I have images like this:
Image
http://tinypic.com/view.php?pic=2ibe2d5&s=8

All images have only two colors - white background and black shapes.
I want to colorize shapes, but each shape to different color, for ex. rectangle to red, circle to green and stain to blue.
In every image i don't know position, size etc of each shape. All i know is that shapes don't overlap.
Any ideas how to do it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select by color and shape

Post by fmw42 »

What platform and version of Imagemagick. If Linux,Mac OSX or Widows with Cygwin, see my script, separate, at the link below. But the colors will be assigned to each shape as they are found in the image, not by shape. That is in every image, a rectangle will not be red all the time. Note you will have to negate your image, so that the objects are white on a black background for my script to work.

If you have to find all similar shapes, the once you have each shape separated, you can use the Image Moments to classify shapes. Moments are not built into IM via the identify command. Also see my script, moments, that will show you the elliptical equivalent intensity, radii and rotation. You can use those 3 arguments to classify shapes as well.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: select by color and shape

Post by anthony »

If your problem is in the form of separating circles from rectangles. That can be quite tricky, but can be done in a number of ways. A good start point is using something like Fred suggested to separate each shape from the other shapes, so you have one image per shape. That will greatly simplify your analysis.

After that...
rectangles by doing a hough transform (not built into IM) and looking for 4 lines,
circles by doing a circular hough transform.

Morphology can also help in this regard, specifically for more general analysis of 'random' shapes. For example it can grade shapes by size, lengths, how far the 'center' is from nearest edge using a Distance Gradient. There may be multiple centers (peaks). Or in the case of a rectangle a line of centers of equal distance from an edge - regardless of rotation. Similarly for glyphs (letters or symbols) that was draw using a pen of constant thickness you will get lots of 'centers' that have very similar distances from edges, and a larger center around where lines join.

Once you have a center you can reverse the distance gradient to find how far the furthest edge is from that center, as the crow flys, verse how far it is via a internal path through the shape, using Constrained Distance. If the two are very different, then you not only have 'arms' or 'psuedopods' sticking out of the shape, those arms curve around or even curve back towards the center.

Separating the random shapes from others is harder. One way is to generate a morphological skeletion, and then analysing those lines to determine ... number of arms, whether the shape has 'holes' in it and how many. etc..
http://www.imagemagick.org/Usage/morpho ... g_skeleton

Fractal analysis may also help but I am not certain how to use it, but I mention it as another line of study for shape analysis.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply