Ask Your Question
0

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

asked 2013-07-01 11:11:04 -0600

lenteken gravatar image

updated 2020-06-07 21:00:14 -0600

supra56 gravatar image

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.

image description 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;
}
edit retag flag offensive close merge delete

Comments

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

wuling gravatar imagewuling ( 2013-07-01 11:19:35 -0600 )edit

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.

lenteken gravatar imagelenteken ( 2013-07-02 01:05:08 -0600 )edit
1

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 same

wuling gravatar imagewuling ( 2013-07-02 08:52:27 -0600 )edit

Thanks for the formula, How can I apply it to my codes?

lenteken gravatar imagelenteken ( 2013-07-02 09:37:35 -0600 )edit

Snippet:

 Point2f rect_pointsA[4][contours.size()];
 malloc(rect_points....)
for( int i = 0; i< contours.size(); i++ )
     {
      ..............
       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 );
       rect_points[j][i]=rect_points[j];
} 
     }

then handle with it.............

wuling gravatar imagewuling ( 2013-07-02 10:28:26 -0600 )edit

I tried it but it has lot of error.

lenteken gravatar imagelenteken ( 2013-07-02 10:52:47 -0600 )edit

no, i think you do not pay attention some rule.

But you can do that:

Point2f rect_points[4];
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

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

{
implement it..................
}
     }
wuling gravatar imagewuling ( 2013-07-02 11:02:20 -0600 )edit

Thank you very much that you are giving me attention. How can I measure the object same as the video I shown to you?

lenteken gravatar imagelenteken ( 2013-07-02 11:16:37 -0600 )edit

well,{ implement it.................. }, you should handle by yourself. It's very easy. believe me

wuling gravatar imagewuling ( 2013-07-02 11:19:42 -0600 )edit

I am stuck on this problem almost 3 days and I don't know how to do it. Can you do it for me?

lenteken gravatar imagelenteken ( 2013-07-02 11:22:05 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2013-12-09 20:34:45 -0600

Dragoen gravatar image

updated 2020-06-07 20:51:42 -0600

supra56 gravatar image

I rewrite this program ...! I am using OpenCV 2.4.3

see this code:

#include <opencv/ highgui.h>
#include <opencv/ cv.h>
#include <opencv/ cxcore.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int main(int, char**) { 
    Mat src; Mat src_gray;
    int thresh = 100;
    int max_thresh = 255;
    RNG rng(12345);

    cv::Mat frame; 
    VideoCapture capture;
    capture.open(0);

    while(true) { 

        capture.read(frame);
        Mat threshold_output;
        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;

        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, thresh, max_thresh, 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 );

    int c= cvWaitKey(10);
    if ((char) c==72) break;
}
return 0;
}

C:\fakepath\main.jpg I hope this can help anyone who needs ..! : D

edit flag offensive delete link more
-1

answered 2013-10-02 11:27:53 -0600

updated 2020-06-07 20:52:55 -0600

supra56 gravatar image

I am guessing your threshold detects an almost perfectly shaped white circle? If so, the width of the rectangle is almost the same as the circle diameter.

double area = contourArea(contours[0]); //0 because you only have 1 contour
double side = sqrt((area * 4) / CV_PI) ;

Important, this will only work if your object is circle shaped.

edit flag offensive delete link more
-3

answered 2013-10-02 02:23:43 -0600

Dragoen gravatar image

I'm having the same problem as you, so I wonder, has this problem been solved?

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-01 11:11:04 -0600

Seen: 30,584 times

Last updated: Jun 07 '20