Ask Your Question

Revision history [back]

What you want is the maxima and minima of the shape. Use something like this, with one scan of the image.

int x_min = img.cols, x_max = 0, y_min = img.rows, y_max = 0;
int r, c;
for( r = 0 ; r < img.rows ; ++r )
{
    for( c = 0 ; c < img.cols ; ++c )
    {
        if( img.at< uchar >( r, c ) == 255 )
        {
            if( r < y_min )
                y_min = r;
            if( r > y_max )
                y_max = r;
            if( c < x_min )
                x_min = c;
            if( c > x_max )
                x_max = c;
        }
    }
}
cv::Point middle( (int)((x_max - x_min)/2), (int)((y_max-y_min)/2) );

If you are more interested by the center of gravity of silhouette, try this sample, which draw the center with a circle, using the moment and findcontours (that you probably already used to detect your silhouette?)

click to hide/show revision 2
update center position

What you want is the maxima and minima of the shape. Use something like this, with one scan of the image.

int x_min = img.cols, x_max = 0, y_min = img.rows, y_max = 0;
int r, c;
for( r = 0 ; r < img.rows ; ++r )
{
    for( c = 0 ; c < img.cols ; ++c )
    {
        if( img.at< uchar >( r, c ) == 255 )
        {
            if( r < y_min )
                y_min = r;
            if( r > y_max )
                y_max = r;
            if( c < x_min )
                x_min = c;
            if( c > x_max )
                x_max = c;
        }
    }
}
cv::Point middle( (int)((x_max (int)(x_min+(x_max - x_min)/2), (int)((y_max-y_min)/2) (int)(y_min+(y_max-y_min)/2) );

If you are more interested by the center of gravity of silhouette, try this sample, which draw the center with a circle, using the moment and findcontours (that you probably already used to detect your silhouette?)