First time here? Check out the FAQ!

Ask Your Question
0

Find Mid Point

asked Jul 16 '13

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

Preview: (hide)

3 answers

Sort by » oldest newest most voted
1

answered Jul 16 '13

updated Jul 16 '13

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

Preview: (hide)
1

answered Jul 16 '13

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.

Preview: (hide)
0

answered Jul 16 '13

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.

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jul 16 '13

Seen: 4,844 times

Last updated: Jul 16 '13