Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Measuring the dimension(height and width) of an object.

I want to measure the height and width of an object using OPENCV but didn't know how to do it. I have followed this tutorial http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rotated_ellipses/bounding_rotated_ellipses.html . I want to measure it base on the contour I detect and rectangular box.

image description

int main(int, char**) { Mat threshold_output; int thresh = 100; vector<vector<point> > contours; vector<vec4i> hierarchy; RNG rng(12345);

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

CvCapture* capture = cvCaptureFromCAM(0); 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(5,5) );

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

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

    /// Find the rotated rectangles
    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 );
     }

/// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", frame );

    cvWaitKey(33);
}
return 0;

}

Measuring the dimension(height and width) of an object.

I want to measure the height and width of an object using OPENCV but didn't know how to do it. I have followed this tutorial http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rotated_ellipses/bounding_rotated_ellipses.html . I want to measure it base on the contour I detect and rectangular box.

image description Code:

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

rng(12345);

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

CvCapture* capture = cvCaptureFromCAM(0); 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(5,5) );

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

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

     /// Find the rotated rectangles
     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 );
      }

/// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", frame );

);

        cvWaitKey(33);
 }
 return 0;
}

}