Ask Your Question
-1

Measure the Length and Width of the bounding Box

asked 2013-07-03 08:22:07 -0600

lenteken gravatar image

This is only simple application of opencv but I don't know how to do it . I will apply this application to measure the dimension of the object(Height and Width). Look at this video what I want to do. http://www.youtube.com/watch?v=bcswZLwhTUI

image description

Please look at this code if it is correct.

   int main(int, char**)
 {
 Mat threshold_output;
 vector<vector<Point> > contours;
 vector<Vec4i> hierarchy;
 RNG rng(12345);

 CvCapture* capture = cvCaptureFromCAM(0);

 cv::Mat frame; cv::Mat src_gray;

while(true) {
    frame = cvQueryFrame( capture );

    cvtColor( frame,src_gray, CV_BGR2GRAY ); // produces out2, a one-channel image (CV_8UC1)
    blur( src_gray, src_gray, Size(11,11) );

    threshold( src_gray, threshold_output, 100, 200, CV_THRESH_BINARY_INV);

    findContours( threshold_output, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    /// Find the rotated rectangles and ellipses for each contour
    vector<RotatedRect> minRect( contours.size() );

  for( int i = 0; i < contours.size(); i++ )
     { 
         minRect[i] = minAreaRect( Mat(contours[i]) );
     }

/// Draw contours + rotated rects + ellipses
  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  for( int i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( rng.uniform(0,0), rng.uniform(0,0), rng.uniform(250,250) );
       // contour
       drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
       // rotated rectangle

       Point2f rect_points[4]; 
       minRect[i].points( rect_points );

       for(int j = 0; j < 4; j++ )
       line( frame, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
     }

 namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
 imshow( "Contours", frame );

    cvWaitKey(33);
}
return 0;

} `

edit retag flag offensive close merge delete

Comments

1

what's the question exactly?

elmiguelao gravatar imageelmiguelao ( 2013-07-03 09:23:31 -0600 )edit

How can measure the bounding rect based on the contour I've detected ?

lenteken gravatar imagelenteken ( 2013-07-03 09:35:08 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2013-07-03 10:19:41 -0600

berak gravatar image

let's say, this is your bbox:

< -- w -->

0 _______ 1   ^
|         |   |
|         |   h
|         |   |
3 _______ 2   v

if i understand you right, you want to know the width & height of it ?

  w = sqrt( (p1.x-p0.x)*(p1.x-p0.x) + (p1.y-p0.y)*(p1.y-p0.y) );
  h = sqrt( (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y) );

in other words, it's just the euclidian norm.

edit flag offensive delete link more

Comments

How can I implement it in my program?

lenteken gravatar imagelenteken ( 2013-07-03 11:57:44 -0600 )edit
1

By using the function described by SR ;)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-04 04:13:12 -0600 )edit
4

answered 2013-07-03 10:35:22 -0600

SR gravatar image

See RotatedRect::size (member variable). It is simple yet not documented.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-03 08:22:07 -0600

Seen: 4,431 times

Last updated: Jul 03 '13