Page 1 of 1
need xy coordinate of blob center
Posted: 2010-09-15T06:53:30-07:00
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
Re: need xy coordinate of blob center
Posted: 2010-09-15T07:26:05-07:00
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
Re: need xy coordinate of blob center
Posted: 2010-09-15T07:39:30-07:00
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.
Re: need xy coordinate of blob center
Posted: 2010-09-15T09:57:13-07:00
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
Re: need xy coordinate of blob center
Posted: 2010-09-15T10:30:04-07:00
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
Re: need xy coordinate of blob center
Posted: 2010-09-15T10:36:12-07:00
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
Re: need xy coordinate of blob center
Posted: 2010-09-15T17:59:20-07:00
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.