need xy coordinate of blob center

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

need xy coordinate of blob center

Post by andienz »

Hi!

I have a black image with a white blob somewhere. I would like to get the average "center" of the of the blob in xy coordinated. It could also happen that the blob is interrupted with blackness cutting it in two or several portions. In other words, is there a fast way to get the coordinates of all white pixels in my picture and calculate the average center and spit it out as x,y coordinates?

I'm using IM 6.4.0 01/19/10 Q16 with cygwin on Windows XP

Thanks for your suggestions.

Andi
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: need xy coordinate of blob center

Post by el_supremo »

You could try trimming the image of the surrounding black pixels (using -trim, with -fuzz if necessary) and then use the coordinate of the centre of the resulting image (width/2,height/2).

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
dproc
Posts: 19
Joined: 2010-09-14T20:39:15-07:00
Authentication code: 8675308

Re: need xy coordinate of blob center

Post by dproc »

The key word here is "fast". The only way to do this precisely is by considering every single white pixel, as you say. If you're willing to sacrifice some precision, then you might try iterating the entire image only for every nth pixel (where n is an integer greater than 1), or you might randomly choose pixels and stop once "enough" white pixels have been found (although the random generator might not be all that fast).

Another option is to find one white pixel, then use a flood-fill algorithm from that pixel to find all the white pixels in the blob. But that might not be all that fast, either--unless the blobs are relatively small compared to the total number of pixels in the image. If there are multiple blobs (because one is cut, for example, as you say) then you could use this algorithm on each blob and then count the pixels in each blob and weight the blob centers accordingly in order to find the center of the "group" of blobs.

If the blobs are never cut with anything but a pretty thin line, then you might blur the image to "remove" that cut. Of course, that'll be really slow, though.
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

Re: need xy coordinate of blob center

Post by andienz »

The aproxximate center using the trim method could actually work for me. Only one tiny problem. I described this probably a little misleading. I need the center/middle of the blob in reference to the original much larger picture. I'm trying to measure the location of the blob within the original picture size. Is there a way to get output from IM how much it was trimming from the four sides (top, bottom, left, right)? Also, if getting that output is possible, a little help with the commandline syntax would be much appreciated ever since I'm not a IM Expert. ( I use it occasionally whenever the need arises ..:-)

Thanks!

Andi
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

Re: need xy coordinate of blob center

Post by andienz »

Reading the -trim manual I found this:

Code: Select all

convert trimtest.png info:-
trimtest.png PNG 25x19 2400x400+14+14 DirectClass 16-bit
I'll just use a combination of grep and awk to get the values pushed into single variables.
This gives me exactly what I need. I quickly can calculate the center of the found area: x=14+(25/2), y=14+(19/2)

Thanks for the great idea!

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

Re: need xy coordinate of blob center

Post by fmw42 »

When you trim or crop an image, if you leave off +repage, IM keeps the virtual canvas information (size and offset). This shows as page geometry in identify -verbose or you can use string formats.

see %g or %X and %Y at http://www.imagemagick.org/script/escape.php

convert logo: -format "%wx%h" info:
640x480

convert logo: -gravity center -crop 50x50%+0+0 logo_crop.png
convert logo_crop.png -format "%wx%h" info:
320x240

convert logo_crop.png -format "%g" info:
640x480+160+120

convert logo_crop.png -format "%Xx%Y" info:
+160x+120
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: need xy coordinate of blob center

Post by anthony »

How well behaved is your 'blob'

You may like to smooth the blob first using Morphology with say a Disk Image using, open
http://www.imagemagick.org/Usage/morphology/#open
This will allow you to get rid of any 'spikes' that may produce bad trim bounds.

Also while it is not ultra fast (though plans are to make it VERY fast) you could try generating a Distance gradient, and then looking for the brighest pixel, whcih will be the pixel most distant from any edge. That is the point that will be at the center of the largest circle that can fit completely inside the blob.
http://www.imagemagick.org/Usage/morphology/#distance

This point is also generally known as the 'last erode' though that exact morphology method has not been implemented at this time ,and can produce multiple 'centers'.

Dilating or Closing the blob once before applying distance can be used to ignore small gaps and nicks from that method of center determination.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply