2
    I want to find object's real height and width using android camera, I am using OpenCv library for Image Processing. I can get height and width in the Image by finding contour , but it is not real height and width it changes when the object's move far from the device. My main purpose is to measure actual width and height of wounds. 

using below code I can mark the wound location using below code. Easily I can mark the wounds but the width and the height I am getting form the processed image is wrong. It is no the actual width and height of the marked location. I think I need some more details of the images and the distance from the camera etc.

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    contours = new ArrayList<MatOfPoint>();
    hierarchy = new Mat();
    Imgproc.GaussianBlur(mRgba,mIntermediateMat,new Size(9,9),2,2);
    Imgproc.Canny(mRgba, mIntermediateMat, 80, 100);
    Imgproc.findContours(mIntermediateMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
/* Mat drawing = Mat.zeros( mIntermediateMat.size(), CvType.CV_8UC3 );
 for( int i = 0; i< contours.size(); i++ )
 {
Scalar color =new Scalar(Math.random()*255, Math.random()*255, Math.random()*255);
 Imgproc.drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, new Point() );
 }*/
    hierarchy.release();
            // Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4)
/* Mat drawing = Mat.zeros( mIntermediateMat.size(), CvType.CV_8UC3 );
 for( int i = 0; i< contours.size(); i++ )
 {`enter code here`
Scalar color =new Scalar(Math.random()*255, Math.random()*255, Math.random()*255);
 Imgproc.drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, new Point() );
 }*/
    for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
    {
        // Minimum size allowed for consideration
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(contourIdx).toArray() );
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);

            Core.rectangle(mRgba, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255), 3);



    }
    return mRgba;
}
Sujith Ks
  • 360
  • 2
  • 10

1 Answers1

3

Your question is very general.You did not tell what it is you are trying to measure, so I will answer generally.

You are actually not finding width and height of any real objects using camera in computer vision. You are modeling them with some of their parameters extracted from a frame captured from camera, which is a 2d digital image, represented in latest opencv's by Mat - simply put, Matrix with width, height of image for 2d-array of number(s) representing data for pixels and some metadata. Since you catch that frame and return processed frame back to the android part(if needed - e.q. you need to stream the camera) Android and OpenCV are completely separate things. You can capture frames from camera either using Google Camera API and converting it to Mat or implementing opencv View(this way is simpler, buggy and, honestly, sucks).
On this matrix, or other matrix(like points of polygon contours) that get from image Mat you apply methods from opencv modules. Some of them can draw/return rectangle(usually || to the image borders) over specific area of image. Generally, you can find contours of smth using methods family

 void findContours(.., List < MatOfPoint> countors, ..)  

and then use

Rect rect = boundingRect(MatOfPoint contour);

on a contour from that List(if any) to get Rect around smth. Obviously that Rect will have width and height you were looking for, if you got contours rights. These are almost "pixel height and width" on 2d-object in image.


To get the dimensions of real physical object you must have some physical-geometrical model and use formulas from that model on found numbers. For example, if your object is parallel to the camera(phone) and centered u can

  final double realWorldHeight = rect.size().height / imageMat.size().height *
  realWorldDistanceToObject * Math.cos( realWorldAngleOfObjectHeight / 2);

The angle of object is how much radians object is occupying vertically from the look-point of camera.

sravs
  • 330
  • 2
  • 14
iantonuk
  • 1,178
  • 8
  • 28
  • how can I find radiance if my camera focusing downward,please give me some code to find the radiance. – Sujith Ks Feb 26 '16 at 12:23
  • here: http://stackoverflow.com/a/12118760/5059838 As you see use Math.toRadians(degrees) to get radians instead degrees. You should mark the answer if it was helpful. – iantonuk Feb 26 '16 at 12:42
  • This is great answer I can calculate the width and height now.But I want to give realWorldDistanceToObject manually,I want this also to be find by our program? can you have any Idea. – Sujith Ks Feb 29 '16 at 07:39
  • please give me one more good answer to find realWorldDistanceToObject ? – Sujith Ks Feb 29 '16 at 10:49
  • there is no way calculate all physical measures from inside your phone. You need to know at least 1 physical dimension to get all other parameters. So you either know the exact size(1 dimension of the object, like height) or real world distance (u measure it with meter or use predefined system). look this my answer for clarification why: http://stackoverflow.com/a/35652500/5059838 – iantonuk Feb 29 '16 at 12:39
  • that geometry base formula can never have less then 2 unknown physical parameters. Because all that the camera frame really gives are angles in certain perspective. The only trick is to use the phone height(known physical parameter), but then u need to modify the formula based on how u use it. That's really elementary school stuff https://en.wikipedia.org/wiki/Trigonometry – iantonuk Feb 29 '16 at 12:45
  • when I calculate width by using this formula, I am getting wrong values. Shall I have to make any modification in this formula for width calculation? – Sujith Ks Mar 03 '16 at 06:49
  • because that formula is for **centered** object. the camera must focus right in the middle of that object. Pls do yourself a favor and learn some elementary school geometry and you'll be able to produce all the formulas you need yourself. that what I posted links for – iantonuk Mar 25 '16 at 02:29