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