CCentroid:Calc


The Calc method computes a centroid position in an image. The starting coordinate (x,y) is passed as arguments of this method. The other centroid parameters are stored in the CCentroid class.

Syntax

nX, nY, bSuccess = CCentroid:Calc( CImage, nX0, nY0 )

nX, nY, bSuccess = CCentroid:Calc( CImage, CPoint )

where

    CImage is the image object to measure.

    nX0 and nY0 are initial guesses for the centroid coordinate (x,y), measured in pixels coordinates

    CPoint is an initial guess for the centroid coordinate (x,y), measured in pixel coordinates.

    nX and nY are the returned centroid position, measured in pixel coordinates.

    bSuccess is the returned success code. On success it is true, otherwise false.

  

This method returns the x and y centroid coordinates plus a boolean success flag. Upon a successful calculation, the centroid coordinates are stored in the class object and later can be retrieved using the X and Y methods. The centroid calculation can fail if the starting coordinate or the calculated centroid lies outside the image. The original starting coordinates passed are stored in the CCentroid object and can be fetched using the GetStartingPos method.

Example

Case 1: Assume that an image exists as the CImage object I. The example below shows calculation of the centroid. The results returned by the functional call and those saved in class members should be identical.

 

C = CCentroid:new()

-- create a CCentroid object

x0 = 192 ; y0 = 440

-- point near the centroid position

x, y, bSuccess = C:Calc(I,x0,y0)

-- calculate the centroid

if ( bSuccess ) then

-- if successful, print the result

 Printf("X=%lg, Y=%lg", x,y)

-- returned by the method

 Printf("X=%lg, Y=%lg", C:X(),C:Y())

-- stored in the class

end

 

 

Case 2: Below is shown the same calculation but hardwiring the x and y starting points and ignoring the success parameter returned by Calc. The refined x,y values for the centroid position are printed directly by setting Calc() as the last parameter for the Printf function:

 

C = CCentroid:new()

-- create a CCentroid object

Printf("X=%lg, Y=%lg", C:Calc(I,192,440) )

-- print the result

Related Topics

X

Y

GetStartingPos

CCentroid class

Image Coordinate System

Subpixel Coordinate Definition