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/... . I want to measure it base on the contour I detect and rectangular box.
Code:
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;
}
I am confused at your question. The max height & width of the object is rotation rectangle.But it seems you don't want that.So do you want rectangle but does not any rotate??
Please watch this video http://www.youtube.com/watch?v=bcswZLwhTUI. This video show what I want. I think the measurement of the object based on the rectangle.
well, in program,rect_points[j] is the rotation rectangle corner, so
width1=abs(rect_points[j].x-rect_points[j+1].x), width2=abs(rect_points[j+1].x-rect_points[j+2].x),
then compare width1 with width2,which is max width, also, the height is the sameThanks for the formula, How can I apply it to my codes?
Snippet:
then handle with it.............
I tried it but it has lot of error.
no, i think you do not pay attention some rule.
But you can do that:
Thank you very much that you are giving me attention. How can I measure the object same as the video I shown to you?
well,{ implement it.................. }, you should handle by yourself. It's very easy. believe me
I am stuck on this problem almost 3 days and I don't know how to do it. Can you do it for me?