Ask Your Question
1

How to get auto generate measurement on object with openCV?

asked 2014-05-27 22:45:22 -0600

norzanbzura gravatar image

Here is a code where I want to implement the object measurement, but I don't have any idea on what function that I've to use and how to code it and how to make it auto generate. There's anyone can help me? Thank you in advanced.

cv::VideoCapture cap;

    if (!cap.isOpened())  // check if succeeded to connect to the camera
        CV_Assert("Can't open Web-Cam");

    double CamHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    double CamWidth  = cap.get(CV_CAP_PROP_FRAME_WIDTH);

    //Display text on the screen function
    int imgW = 650;
    int imgH = 50;
    int fontFace = FONT_HERSHEY_PLAIN;
    double fontScale = 1.5;
    int thickness = 2;
    Point textOrg(imgW/5, imgH/1.3);

    int value=10;
    string someText = format("Dimension: %d ", value); //Display the dimension (output)
    putText(frame, someText, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);
    imshow("Object Measurement",frame);;
edit retag flag offensive close merge delete

Comments

You will get some idea from here.

Haris gravatar imageHaris ( 2014-05-27 22:58:08 -0600 )edit

Hi haris, actually here is the code that I've referred just now. I got a lot of error. There's anything wrong from what I did?

    for(int i=0; i<countours.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);
            rect_points[j][i]=rect_points[j];
        }
    }
norzanbzura gravatar imagenorzanbzura ( 2014-05-28 02:07:33 -0600 )edit

From where did you get the code?, what are the errors?

Haris gravatar imageHaris ( 2014-05-28 02:46:02 -0600 )edit

I got that code from the link that u asked me to refer. The error is just undefined 'j'. But I don't know whether I arrange the code correct or not.

norzanbzura gravatar imagenorzanbzura ( 2014-05-28 04:18:26 -0600 )edit

I didn’t find any code there, and the link will give you an idea how to do it according to the scenario.

Haris gravatar imageHaris ( 2014-05-28 04:58:12 -0600 )edit

Yes. But sorry. I still didn't get the idea of how to measure the object..

norzanbzura gravatar imagenorzanbzura ( 2014-05-28 05:19:51 -0600 )edit

Please explain your scenario....

Haris gravatar imageHaris ( 2014-05-28 08:04:12 -0600 )edit
norzanbzura gravatar imagenorzanbzura ( 2014-05-28 08:46:49 -0600 )edit

In that probably the camera in fixed distance and the object to measure always will be in same plane(same distance from the camera), so first place reference object on the plane and calculate pixel/size(m or mm) ratio, and when object to measure come to the scene find it's length in pixels and convert it to metre or mm using the ratio. For finding the object bounding box you can use contour processing.

Haris gravatar imageHaris ( 2014-05-29 00:03:51 -0600 )edit

How to implement it on the code? Sorry, but I don't know how. Hope you can help me. Thank you in advance.

norzanbzura gravatar imagenorzanbzura ( 2014-06-01 22:50:40 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2014-05-28 16:02:53 -0600

Witek gravatar image

What you need to do this is:

  1. Camera calibration
  2. Perspective transformation
  3. Bounding box of your objects

First you calibrate your camera and undistort the grabbed image. Then you find the perspective transformation between your camera and the plane onto which you will put objects. Use getPerspectiveTransform with just 4 corners of a rectangle of known dimensions that you put on your plane (for best precision make it big and mark corners exactly - or use a checkerboard and findHomography instead). The perspective transform found this way will be used to convert screen coordinates of a pixel into Cartesian coordinates local to your rectangular pattern. Once you have found it, you ma remove the pattern. It is no more necessary. Just do not move tha camera with respect to the plane it is pointed at. If you do so, you will have to calculate the perspective transform once again.

Now, any pixel coordinate in the screen can be expressed as X,Y pair in your "plane" coordinate system with a simple call of perspectiveTransform. Or, if you want to transform the image itself, instead of recalculationg pixel position, use warpPerspective. This way it will be even easier. In the warped image (it will look as if you took the picture holding the camera perpendicularly to your plane) find the bounding box of your object (with simple thresholding and contour finding). The size of the bounding box (minAreaRect) will be close to the real size of your object, provided the objects are relatively flat and there is almost no parallax error and thresholding is not corrupted (for example due to shadows). You will best results with diffuse light camera-plane angle close to 90 degrees and flat objects (as compared to the camera-plane distance).

edit flag offensive delete link more

Comments

Thank you for your attention. But how to implement this into a code? Because I've no idea on it. Thank you in advance.

norzanbzura gravatar imagenorzanbzura ( 2014-06-01 19:51:34 -0600 )edit

Question Tools

Stats

Asked: 2014-05-27 22:45:22 -0600

Seen: 1,492 times

Last updated: May 28 '14