Centriod calculation using connected-components

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
palodylan
Posts: 3
Joined: 2017-07-19T09:51:35-07:00
Authentication code: 1151

Centriod calculation using connected-components

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

Re: Centriod calculation using connected-components

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centriod calculation using connected-components

Post 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.
palodylan
Posts: 3
Joined: 2017-07-19T09:51:35-07:00
Authentication code: 1151

Re: Centriod calculation using connected-components

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centriod calculation using connected-components

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

Re: Centriod calculation using connected-components

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centriod calculation using connected-components

Post by fmw42 »

Can you provide an example image and also please always provide your IM version and platform.
Post Reply