Ask Your Question
0

Find Mid Point

asked 2013-07-15 22:11:23 -0500

zuniarwbk gravatar image

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 image description Thank's

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2013-07-15 23:07:59 -0500

updated 2013-07-16 00:05:48 -0500

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

edit flag offensive delete link more
1

answered 2013-07-15 23:39:09 -0500

LA2 gravatar image

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.

edit flag offensive delete link more
0

answered 2013-07-16 07:04:19 -0500

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.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-07-15 22:11:23 -0500

Seen: 3,357 times

Last updated: Jul 16 '13