crop to largest area of a given color

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
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

crop to largest area of a given color

Post 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
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

Re: crop to largest area of a given color

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop to largest area of a given color

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop to largest area of a given color

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

Re: crop to largest area of a given color

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop to largest area of a given color

Post 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.
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: crop to largest area of a given color

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: crop to largest area of a given color

Post 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.
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: crop to largest area of a given color

Post by fmw42 »

You need to segment each area first.
That is where my separate script comes in!

Fred
Post Reply