Page 1 of 1

crop to largest area of a given color

Posted: 2011-03-14T19:21:56-07:00
by andienz
Hi!
I need some help to brainstorm this little problem. I'm working on a little project where I need to draw a box around the largest area of a certain color in an image. I'm currently using +opaque and -fuzz to single out the color and then use -trim with - info in order to get the coordinates for the box. (Have exact syntax at work, and I'm at home researching this little puzzle).
Unfortunately it can happen that I have two areas of the same color in the picture and I would like to draw the box only around the largest area. Any Idea how I could "eliminate" all areas but the largest? Let's say I have two blue discs. One smaller than the other. I'd like the box to be drawn only around the larger circle. Not around the small one. Any Ideas? Maybe there is a quick and easy way of doing that?

Thanks for considering!

Andi

Re: crop to largest area of a given color

Posted: 2011-03-14T19:50:49-07:00
by andienz
I just found this post: viewtopic.php?f=1&t=11705
It already jogged my mind towards a solution. I actually can determine a point on the image where I'll use -floodfill to isolate the color area I'm after. This actually works perfectly in my application because I'm also only interested if the color area enters a certain area of the image. I'm wondering though what happens if I hit the background color. I'll have to write some code that would recognize the output generated in this case. Hmm. I could probably catch that case by purposely placing dots of the original background color into the corners in order to help -trim to do the right job. If the trimmed image is the same size like the original, I know that I didn't hit my blob. I still appreciate any more "brain-storms" :-)

Good night for now ...
Andi

Re: crop to largest area of a given color

Posted: 2011-03-14T20:19:03-07:00
by fmw42
Convert your color to white and everything else to black (see -fill black +opaque somecolor -fill white -opaque somecolor and use -fuzz if your colors are not pure). Then use my script separate (see below) to extract all separate white regions. Then trim to bounding box and get size. Keep only the largest.

Re: crop to largest area of a given color

Posted: 2011-03-14T20:29:55-07:00
by anthony
This solution was not available in 2005 when the previous topic was originally explored.

This finds the largest internal area. NOT the largest bounds.


Convert the color to a mask image, and generate a morphology 'distance gradient'
http://www.imagemagick.org/Usage/morphology/#distance

Chebyshev kernel will find the largest rectangular area!

Now find the pixel(s) with the largest distance, and you have the center of the largest area.



The Distance function can be a little slow (multiple iterations), but I do have a fast 2-pass algorithm, which I hope to implement at some point. Addendum -- a fast distance is now implemented.

Re: crop to largest area of a given color

Posted: 2011-03-15T04:15:30-07:00
by andienz
Awesome! Thanks for your ideas!

Anthony, is there a way to get the bounding box coordinates of the largest area, instead of the center of the largest area or inscribed rectangle?

Andi

Re: crop to largest area of a given color

Posted: 2011-03-15T16:54:15-07:00
by anthony
andienz wrote:Anthony, is there a way to get the bounding box coordinates of the largest area, instead of the center of the largest area or inscribed rectangle?
Not simply. Segmentation to isolate each area seems to be the best way. But I could be wrong.

The real problem with outer bounds is that you can get two or more areas overlapping, or even have one completely contained inside another. That makes a morphology method much more difficult.

I do plan on a 'labeling' morphology function, which is a single pass function. This would make segmentation of shapes a lot simpler. But my ToDo list is quite long and new functions need a good block of programming time to implement. It also depends on a question I have for Cristy.

Re: crop to largest area of a given color

Posted: 2011-03-16T10:06:18-07:00
by fmw42
andienz wrote:Awesome! Thanks for your ideas!

Anthony, is there a way to get the bounding box coordinates of the largest area, instead of the center of the largest area or inscribed rectangle?

Andi

If you use -trim without +repage, then the virtual canvas coordinates are stored with the offset to the top left corner of the trimmed area relative to the original in the verbose info: (if you do not use jpg format, but png, gif, or miff or tiff)

My separate script could be used to get all separate regions and then modified to test each for the maximum bounding box and its offset and size coordinates or even the area (number of white pixels) in the region as opposed to simply the bounding box.

Re: crop to largest area of a given color

Posted: 2011-03-16T19:19:26-07:00
by anthony
fmw42 wrote:If you use -trim without +repage, then the virtual canvas coordinates are stored.
You don't need to save that image, just get the info: you can use -format %g for just the bounds.

However the -trim method only works if their is one and only one color area. It will not tell you the largest bounds of a number of possibly overlapping areas. You need to segment each area first.

Re: crop to largest area of a given color

Posted: 2011-03-16T20:44:25-07:00
by fmw42
You need to segment each area first.
That is where my separate script comes in!

Fred