Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Measure the Length and Width of the bounding Box

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;

} `