Average location

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
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Average location

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Average location

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply