# 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

This forum is disabled, please visit https://forum.opencv.org

Find Mid Point

add a comment

1

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?)

1

0

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.

Asked: **
2013-07-15 22:11:23 -0600
**

Seen: **4,188 times**

Last updated: **Jul 16 '13**

Width measurement of image parts

How to filter an RGB image and transform into an BW one

How to get RGBA8888 image data from 8UC4 Matrix?

count the number of black pixel for each column in a binary image

Matching ranges over many dimensions

Some problem about displaying a raw image file

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.