# Find Mid Point

Hi, i have some problems. I have a binary image and i want to find middle value by substracts x1 and x2 cols value. likewise, i want to subtracts y1 and y2. here is the code that i used : link text Thank's

edit retag close merge delete

Sort by » oldest newest most voted

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_min+(x_max - x_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?)

more

Not an answer, just a minor correction to your illustration: Once you have found Y1 and Y2, the formula (Y2-Y1)/2 gives you the distance between Y1 and Y3. The actual position of Y3 is that distance + Y1, which means that Y3=(Y2+Y1)/2.

more

I guess an even more easier option is to use the findContours function which will return a list of bounding boxes corresponding to the blobs in your image.

You can retrieve the measurements of this boundingbox, combined with the top left corner, which will give you all data that you actually need to calculate the center fast.

more

Official site

GitHub

Wiki

Documentation