为AR应用程序计算轮廓的姿势和变换及其边界矩形

时间:2019-05-20 20:35:31

标签: opencv opencv-contour

我正在尝试将HandPoseEstimation示例转换为AR。因此,当检测到轮廓时,AR对象将跟随轮廓。我的转换值似乎不准确,所以我认为我做错了什么。这是我的调试对转换值的外观:

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

我正在使用轮廓的边界直角作为我的2D点,以解决MarkerBasedAR示例中的PNP和3d点

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

0 个答案:

没有答案