Dlib C++可以非常好地檢測標記和估計人臉姿態。但是,如何獲得頭部姿態的三維座標軸方向(x,y,z)?如何獲得3D座標Dlib中的頭部姿態估計軸C++
回答
我還面臨同樣的問題,前一段時間,搜索和找到1-2有用的博客文章,這link會讓你對所涉及的技術的概述,如果你只需要計算三維姿勢在十進制那麼你可以跳過OpenGL渲染部分,但是如果你想直觀地獲得反饋,那麼你也可以嘗試OpenGL,但是我建議你忽略OpenGL作爲初學者的一部分,所以從中提取了最小的工作代碼片段github頁面,會是這個樣子:
// Reading image using OpenCV, you may use dlib as well.
cv::Mat img = cv::imread(imagePath);
std::vector<double> rv(3), tv(3);
cv::Mat rvec(rv),tvec(tv);
cv::Vec3d eav;
// Labelling the 3D Points derived from a 3D model of human face.
// You may replace these points as per your custom 3D head model if any
std::vector<cv::Point3f > modelPoints;
modelPoints.push_back(cv::Point3f(2.37427,110.322,21.7776)); // l eye (v 314)
modelPoints.push_back(cv::Point3f(70.0602,109.898,20.8234)); // r eye (v 0)
modelPoints.push_back(cv::Point3f(36.8301,78.3185,52.0345)); //nose (v 1879)
modelPoints.push_back(cv::Point3f(14.8498,51.0115,30.2378)); // l mouth (v 1502)
modelPoints.push_back(cv::Point3f(58.1825,51.0115,29.6224)); // r mouth (v 695)
modelPoints.push_back(cv::Point3f(-61.8886f,127.797,-89.4523f)); // l ear (v 2011)
modelPoints.push_back(cv::Point3f(127.603,126.9,-83.9129f)); // r ear (v 1138)
// labelling the position of corresponding feature points on the input image.
std::vector<cv::Point2f> srcImagePoints = {cv::Point2f(442, 442), // left eye
cv::Point2f(529, 426), // right eye
cv::Point2f(501, 479), // nose
cv::Point2f(469, 534), //left lip corner
cv::Point2f(538, 521), // right lip corner
cv::Point2f(349, 457), // left ear
cv::Point2f(578, 415) // right ear};
cv::Mat ip(srcImagePoints);
cv::Mat op = cv::Mat(modelPoints);
cv::Scalar m = mean(cv::Mat(modelPoints));
rvec = cv::Mat(rv);
double _d[9] = {1,0,0,
0,-1,0,
0,0,-1};
Rodrigues(cv::Mat(3,3,CV_64FC1,_d),rvec);
tv[0]=0;tv[1]=0;tv[2]=1;
tvec = cv::Mat(tv);
double max_d = MAX(img.rows,img.cols);
double _cm[9] = {max_d, 0, (double)img.cols/2.0,
0 , max_d, (double)img.rows/2.0,
0 , 0, 1.0};
cv::Mat camMatrix = cv::Mat(3,3,CV_64FC1, _cm);
double _dc[] = {0,0,0,0};
solvePnP(op,ip,camMatrix,cv::Mat(1,4,CV_64FC1,_dc),rvec,tvec,false,CV_EPNP);
double rot[9] = {0};
cv::Mat rotM(3,3,CV_64FC1,rot);
Rodrigues(rvec,rotM);
double* _r = rotM.ptr<double>();
printf("rotation mat: \n %.3f %.3f %.3f\n%.3f %.3f %.3f\n%.3f %.3f %.3f\n",
_r[0],_r[1],_r[2],_r[3],_r[4],_r[5],_r[6],_r[7],_r[8]);
printf("trans vec: \n %.3f %.3f %.3f\n",tv[0],tv[1],tv[2]);
double _pm[12] = {_r[0],_r[1],_r[2],tv[0],
_r[3],_r[4],_r[5],tv[1],
_r[6],_r[7],_r[8],tv[2]};
cv::Mat tmp,tmp1,tmp2,tmp3,tmp4,tmp5;
cv::decomposeProjectionMatrix(cv::Mat(3,4,CV_64FC1,_pm),tmp,tmp1,tmp2,tmp3,tmp4,tmp5,eav);
printf("Face Rotation Angle: %.5f %.5f %.5f\n",eav[0],eav[1],eav[2]);
輸出:
**X** **Y** **Z**
Face Rotation Angle: 171.44027 -8.72583 -9.90596
我很感謝您的幫助,我將嘗試今晚的代碼:D –
我試着你的解決方案,但我堅持在獲取點的位置的眼睛,鼻子,從dlib鏈接提供的3D模型的步驟:http://sourceforge.net /projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2。 .dat文件非常通用。我試圖改變文件擴展名,以便從一些3D軟件中讀取,但沒有用。你有什麼建議嗎? –
您不必重寫這些3D點,只需要相應地更新'srcImagePoints'。 – ZdaR
- 1. 姿態估計
- 2. 姿態估計?
- 3. 2個攝像頭的頭部姿態估計
- 4. OpenCV的姿態估計
- 5. OpenCV:相機姿態估計
- 6. 相機姿態估計
- 7. 如何在Android中執行3D對象渲染的姿態估計
- 8. OpenCV:使用StereoCamera系統進行色彩標記的3D姿態估計
- 9. 如何獲得兩個動態座標軸的矩陣
- 10. 如何使用2D跟蹤和初始3D姿勢估算3D姿勢
- 11. 帶有一個攝像頭的實時姿態估計(6DoF)opencv
- 12. 相機姿態估計是倒置的
- 13. Python的姿態估計示例錯誤
- 14. opencv的相機姿態估計
- 15. 在C++中檢測dlib矩形座標
- 16. 攝像機姿態估計(OpenCV PnP)
- 17. 四邊形檢測和姿態估計
- 18. 相機姿態估計合成數據
- 19. 用emgu進行姿態估計
- 20. 使用OpenCV,C++和圖像2D進行頭部姿態估計 - 幾何方法 - 滾動,偏航和俯仰
- 21. 如何在MATLAB圖上獲取座標軸上的箭頭?
- 22. 如何隱藏matplotlib 3d中的座標軸?
- 23. 使用solvePnP攝像機姿態估計,C++
- 24. 使用外部姿態估計來改善固定標記輪廓跟蹤
- 25. 在Kinect中獲取頭部座標
- 26. 如何在3D場景中使用和設置座標軸
- 27. 如何更改matplotlib 3D圖的座標軸的顏色?
- 28. 如何獲得座標?
- 29. 姿態估計中人工神經網絡的適用性
- 30. 如何獲得cocos2d中的座標?
這個問題已經有一個公認的答案。儘管如此,爲了將來的參考,這個主題也有很棒的博客文章:http://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/ –