Page 1 of 1

Centriod calculation using connected-components

Posted: 2017-07-19T09:59:11-07:00
by palodylan
Hi,
Is anyone aware of how the -connected components is calculating its centroid? I would imagine it is just using the 8-connected criterion to find all of the dots associated with the object and then just doing an average position of each of these pixels found, but I just want to make sure.

In addition, if the program kicks out a centroid value, e.g. 1,1 does this mean that the centroid is at the center of that pixel?

Finally, can anyone confirm that the pixels start counting in imagemagick from 0, not 1. I would assume this, but again I would just like the confirmation.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T11:13:00-07:00
by snibgo
When a component is found, its centroid is the average of every x and every y in the component. The centroid coordinates may not be integers. If they are, that is the centre of a pixel.

IM coordinates start at (0,0) top-left.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T11:15:11-07:00
by fmw42
The centroid is the average x,y location of all pixels in the region.

Pixel coordinates are explained at http://www.imagemagick.org/Usage/distor ... oordinates. I think only -distort considers pixels to have an area (image coordinates) rather than just points (pixel coordinates). So I believe that CCL uses pixel coordinates. But the centroid can have fractional components.

ImageMagick considers image coordinates to start at 0 for x and y.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T13:42:28-07:00
by palodylan
Thanks for your help, I appreciate it.
Do you know if there is any way to change the centroid calculation to do a weighted mean that is dependent on the intensity of the pixel? I'm working in grayscale.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T13:59:44-07:00
by fmw42
You could extract the region as its own image and then use identify -verbose -moments to get the relevant moments. See https://en.wikipedia.org/wiki/Image_moment (M10 and M01). The CCL centroid may be weighted, but for binary or constant regions, it will be equivalent to unweighted. If it is weighted, you would need to specify a fuzz value for CCL to consider a region for all gray values within the fuzz value. I would have to test this to see if CCL is doing a weighted average for the centroid. You could look at the code in vision.c

It does not look like it does a weighted average from lines 409 and 410

object[id].centroid.x+=x;
object[id].centroid.y+=y;

So you would need to extract the region as its own color and black background and then use identify -verbose -moments yourimage.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T14:17:33-07:00
by snibgo
Connected components, in vision.c, are found by looking for adjacent pixels of the same colour (with fuzz distance). So pixels in a component have equal intensities, so weighting wouldn't work.

Re: Centriod calculation using connected-components

Posted: 2017-07-19T21:47:36-07:00
by fmw42
Can you provide an example image and also please always provide your IM version and platform.