我一直在使用Apple Vision框架中的功能/关键点,通过OpenCV的cv :: solvePNP进行姿势估计(将3D模型中的关键点与图像上的2D点进行校正以匹配姿势)。
TL-DR:
我的场景工具包模型正在平移,并且从solvePnP内省平移和旋转矢量时,单位看起来正确(即,它们是正确的量级),但是平移的坐标系似乎消失了:
我正在尝试通过金属/ OpenGL坐标系的solvePnP wrt和相机投影矩阵来了解坐标系要求。
我的SCNCamera需要什么'projectionMatrix'来匹配传递到SolvePnP中的基于图像的坐标系?
一些事情我已经读过/相信我正在考虑在内。
更长的代码:
我的工作流程是这样的
步骤1-使用3D模型工具对我的3D模型上的点进行内省,并获取2D检测到的要素中主要关键点的对象顶点位置。我正在使用左瞳孔,右瞳孔,鼻尖,下巴尖,左外唇角,右外唇角。
第2步-运行视觉请求,并提取图像空间中的点列表(转换为OpenCV的左上角坐标系),并提取相同顺序的2D点列表。
第3步-使用输入图像的大小构造一个相机矩阵。
第4步-运行cv :: solvePnP,然后使用cv :: Rodrigues将旋转矢量转换为矩阵
第5步-将生成的变换的坐标系转换为适合GPU的坐标-反转y和z轴,并将平移和旋转组合为单个4x4矩阵,然后将其转置为OpenGL的合适主体/金属
第6步-通过以下方式将结果转换应用于Scenekit:
let faceNodeTransform = openCVWrapper.transform(for: landmarks, imageSize: size)
self.destinationView.pointOfView?.transform = SCNMatrix4Invert(faceNodeTransform)
下面是我的Obj-C ++ OpenCV包装器,其中包含视觉地标的子集和所查看图像的真实像素大小:
/ https://answers.opencv.org/question/23089/opencv-opengl-proper-camera-pose-using-solvepnp/
- (SCNMatrix4) transformFor:(VNFaceLandmarks2D*)landmarks imageSize:(CGSize)imageSize
{
// 1 convert landmarks to image points in image space (pixels) to vector of cv::Point2f's :
// Note that this translates the point coordinate system to be top left oriented for OpenCV's image coordinates:
std::vector<cv::Point2f > imagePoints = [self imagePointsForLandmarks:landmarks imageSize:imageSize];
// 2 Load Model Points
std::vector<cv::Point3f > modelPoints = [self modelPoints];
// 3 create our camera extrinsic matrix
// TODO - see if this is sane?
double max_d = fmax(imageSize.width, imageSize.height);
cv::Mat cameraMatrix = (cv::Mat_<double>(3,3) << max_d, 0, imageSize.width/2.0,
0, max_d, imageSize.height/2.0,
0, 0, 1.0);
// 4 Run solvePnP
double distanceCoef[] = {0,0,0,0};
cv::Mat distanceCoefMat = cv::Mat(1 ,4 ,CV_64FC1,distanceCoef);
// Output Matrixes
std::vector<double> rv(3);
cv::Mat rotationOut = cv::Mat(rv);
std::vector<double> tv(3);
cv::Mat translationOut = cv::Mat(tv);
cv::solvePnP(modelPoints, imagePoints, cameraMatrix, distanceCoefMat, rotationOut, translationOut, false, cv::SOLVEPNP_EPNP);
// 5 Convert rotation matrix (actually a vector)
// To a real 4x4 rotation matrix:
cv::Mat viewMatrix = cv::Mat::zeros(4, 4, CV_64FC1);
cv::Mat rotation;
cv::Rodrigues(rotationOut, rotation);
// Append our transforms to our matrix and set final to identity:
for(unsigned int row=0; row<3; ++row)
{
for(unsigned int col=0; col<3; ++col)
{
viewMatrix.at<double>(row, col) = rotation.at<double>(row, col);
}
viewMatrix.at<double>(row, 3) = translationOut.at<double>(row, 0);
}
viewMatrix.at<double>(3, 3) = 1.0f;
// Transpose OpenCV to OpenGL coords
cv::Mat cvToGl = cv::Mat::zeros(4, 4, CV_64FC1);
cvToGl.at<double>(0, 0) = 1.0f;
cvToGl.at<double>(1, 1) = -1.0f; // Invert the y axis
cvToGl.at<double>(2, 2) = -1.0f; // invert the z axis
cvToGl.at<double>(3, 3) = 1.0f;
viewMatrix = cvToGl * viewMatrix;
// Finally transpose to get correct SCN / OpenGL Matrix :
cv::Mat glViewMatrix = cv::Mat::zeros(4, 4, CV_64FC1);
cv::transpose(viewMatrix , glViewMatrix);
return [self convertCVMatToMatrix4:glViewMatrix];
}
- (SCNMatrix4) convertCVMatToMatrix4:(cv::Mat)matrix
{
SCNMatrix4 scnMatrix = SCNMatrix4Identity;
scnMatrix.m11 = matrix.at<double>(0, 0);
scnMatrix.m12 = matrix.at<double>(0, 1);
scnMatrix.m13 = matrix.at<double>(0, 2);
scnMatrix.m14 = matrix.at<double>(0, 3);
scnMatrix.m21 = matrix.at<double>(1, 0);
scnMatrix.m22 = matrix.at<double>(1, 1);
scnMatrix.m23 = matrix.at<double>(1, 2);
scnMatrix.m24 = matrix.at<double>(1, 3);
scnMatrix.m31 = matrix.at<double>(2, 0);
scnMatrix.m32 = matrix.at<double>(2, 1);
scnMatrix.m33 = matrix.at<double>(2, 2);
scnMatrix.m34 = matrix.at<double>(2, 3);
scnMatrix.m41 = matrix.at<double>(3, 0);
scnMatrix.m42 = matrix.at<double>(3, 1);
scnMatrix.m43 = matrix.at<double>(3, 2);
scnMatrix.m44 = matrix.at<double>(3, 3);
return (scnMatrix);
}
一些问题:
一个SCNNode没有modelViewMatrix(据我所知,它是一个转换,即modelMatrix),只能将矩阵投掷到-因此,我已经阅读了SolvePNP流程的逆转换可用于请摆好姿势,似乎可以让我获得关闭效果。我想确保这种方法是正确的。
如果我有modelViewMatrix和projectionMatrix,我应该能够计算出合适的modelMatrix吗?这是我应该采用的方法吗?
我不清楚我应该为我的SceneKit场景使用哪种projectionMatrix,如果对我的结果有影响。我是否需要一个像素以使视口与图像尺寸完全匹配,以及如何正确配置SCNCamera以确保SolvePnP的坐标系一致?
非常感谢您!