Ask Your Question

Revision history [back]

How to calculate pose and transformation for contours and its bounding rect for AR app?

I'm trying to convert the HandPoseEstimation example into AR with openCVforUnity. So when a contour is detected, an AR object will follow the contour. My transformation values don't seem accurate. This is what my debug looks like for the transformation values:

transformationM  1.00000 0.00000 0.00000 0.00000                  0.00000 1.00000 0.00000 0.00000                  0.00000 0.00000 1.00000 0.00000                  0.00000 0.00000 0.00000 1.00000

I am using the bounding rect corners of the contour as my 2D points to solvePNP and the 3d points from the MarkerBasedAR example:

Imgproc.rectangle (rgbaMat, boundRect.tl (), boundRect.br (), CONTOUR_COLOR_WHITE, 2, 8, 0);

rectPoints = new MatOfPoint2f(new Point (boundRect.x, boundRect.y),//l eye
    new Point(boundRect.x + boundRect.width, boundRect.y),
    new Point(boundRect.x, boundRect.y + boundRect.height),
    new Point(boundRect.x + boundRect.width, boundRect.y + boundRect.height)
);

//estimate pose
Mat Rvec = new Mat();
Mat Tvec = new Mat();
Mat raux = new Mat();
Mat taux = new Mat();

List<Point3> m_markerCorners3dList = new List<Point3>();

m_markerCorners3dList.Add(new Point3(-0.5f, -0.5f, 0));
m_markerCorners3dList.Add(new Point3(+0.5f, -0.5f, 0));
m_markerCorners3dList.Add(new Point3(+0.5f, +0.5f, 0));
m_markerCorners3dList.Add(new Point3(-0.5f, +0.5f, 0));

m_markerCorners3d.fromList(m_markerCorners3dList);

Calib3d.solvePnP(m_markerCorners3d, rectPoints, camMatrix, distCoeff, raux, taux);

raux.convertTo(Rvec, CvType.CV_32F);
taux.convertTo(Tvec, CvType.CV_32F);

rotMat = new Mat(3, 3, CvType.CV_64FC1);
Calib3d.Rodrigues(Rvec, rotMat);

transformationM.SetRow(0, new Vector4((float)rotMat.get(0, 0)[0], (float)rotMat.get(0, 1)[0], (float)rotMat.get(0, 2)[0], (float)Rvec.get(0, 0)[0]));
transformationM.SetRow(1, new Vector4((float)rotMat.get(1, 0)[0], (float)rotMat.get(1, 1)[0], (float)rotMat.get(1, 2)[0], (float)Rvec.get(1, 0)[0]));
transformationM.SetRow(2, new Vector4((float)rotMat.get(2, 0)[0], (float)rotMat.get(2, 1)[0], (float)rotMat.get(2, 2)[0], (float)Rvec.get(2, 0)[0]));
transformationM.SetRow(3, new Vector4(0, 0, 0, 1));
Debug.Log ("transformationM " + transformationM.ToString ());

Rvec.Dispose();
Tvec.Dispose();
raux.Dispose();
taux.Dispose();
rotMat.Dispose();

Does anyone know why the transformation isn't calculating correctly?