Retrieve the coordinates corresponding to a certain 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
badabou
Posts: 54
Joined: 2008-06-23T06:12:24-07:00

Retrieve the coordinates corresponding to a certain color

Post by badabou »

Hello,

Is it possible to retrieve the coordinates x,y (and width,height) of a shape in an image with imagemagick ?

Let me explain :

There is a function in flash which recovers an area corresponding to a certain color (getColorBoundsRect) :

Image

With this picture, the function will return the coordinates of the area filled with the red circle.

I would do the same with imagemagick but I do not know if this is possible.

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

Re: Retrieve the coordinates corresponding to a certain colo

Post by fmw42 »

Use -trim without a following +repage, then get the geometry size and offset


convert 92246458.png +repage -fuzz 10% -trim 92246458_trim.png
size=`convert 92246458_trim.png -format "%P" info:`
offsets=`convert 92246458_trim.png -format "%O" info:`
echo "size=$size"
echo "offsets=$offsets"

choose an appropriate fuzz value to separate the background from the circle (presumably without the white square).

see
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/Usage/crop/#trim

The above is for unix variables. For windows, see http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Retrieve the coordinates corresponding to a certain colo

Post by anthony »

Or are you trying to get the locations of all red pixels? Just the bounding box indicated in white? Or just the 'best guess' at the center and diameter of a circle? Perhaps you just one a single coordinate anywhere within the circle? Or maybe the mask of the circle?

There are lots of possibilities as to your meaning of 'coordinates of the shape'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply