Page 1 of 1

Average location

Posted: 2015-03-20T05:28:06-07:00
by tom_dl
I'm trying to use ImageMagick to find the location of a calibration target in an image. I've written enough code to output a b&w (i.e. bilevel, not grayscale) image, which looks like a small black square on a giant white background. I've used the following code to find the location of the all of the black pixels:

Code: Select all

identify -precision 5 -define identify:locate=minimum -define identify:limit=1000 calibrationtarget.png 2>results.txt
which gives me a text file with <1000 coordinates (the sqaure is about 30x30). However, I'd ideally like the x coordinates average and the y coordinates average to tell me "average location" of the square (i.e. its centre). Is there any way to get IM to output these two coordinates? Thanks!

Re: Average location

Posted: 2015-03-20T12:33:55-07:00
by snibgo
A number of possible centres could be used.

1. The centre of the rectangle that encloses your object. Use "-trim" or "-format %@".

2. The centroid (centre of gravity) of the object. This is the mean x and mean y of the pixels in the object. Use "-connected-components", see http://magick.imagemagick.org/script/co ... onents.php

3. The pixel that is furthest from the edges of your object. Use "-morphology gradient". See example at http://im.snibgo.com/growcut.htm#visstr

For a square object, these should find the same coordinate. For some objects, such as a crescent moon, methods (1) and (2) will return a coord that is not inside the object, but method (3) works.