OpenCVprojectPoints関数の使用法

Opencv Projectpoints Function Usage

関数cvProjectPoints2は、指定された内部パラメーターと外部パラメーターによって、2D画像平面への3D点投影の座標を計算します。

#include 'opencv2/core/core.hpp' #include 'opencv2/imgproc/imgproc.hpp' #include 'opencv2/calib3d/calib3d.hpp' #include 'opencv2/highgui/highgui.hpp' #include #include using namespace std vector Generate3DPoints() int main(int argc, char* argv[]) { // Read 3D points vector objectPoints = Generate3DPoints() vector imagePoints cv::Mat intrisicMat(3, 3, cv::DataType::type) // Intrisic matrix intrisicMat.at(0, 0) = 1.6415318549788924e+003 intrisicMat.at(1, 0) = 0 intrisicMat.at(2, 0) = 0 intrisicMat.at(0, 1) = 0 intrisicMat.at(1, 1) = 1.7067753507885654e+003 intrisicMat.at(2, 1) = 0 intrisicMat.at(0, 2) = 5.3262822453148601e+002 intrisicMat.at(1, 2) = 3.8095355839052968e+002 intrisicMat.at(2, 2) = 1 cv::Mat rVec(3, 1, cv::DataType::type) // Rotation vector rVec.at(0) = -3.9277902400761393e-002 rVec.at(1) = 3.7803824407602084e-002 rVec.at(2) = 2.6445674487856268e-002 cv::Mat tVec(3, 1, cv::DataType::type) // Translation vector tVec.at(0) = 2.1158489381208221e+000 tVec.at(1) = -7.6847683212704716e+000 tVec.at(2) = 2.6169795190294256e+001 cv::Mat distCoeffs(5, 1, cv::DataType::type) // Distortion vector distCoeffs.at(0) = -7.9134632415085826e-001 distCoeffs.at(1) = 1.5623584435644169e+000 distCoeffs.at(2) = -3.3916502741726508e-002 distCoeffs.at(3) = -1.3921577146136694e-002 distCoeffs.at(4) = 1.1430734623697941e-002 cout << 'Intrisic matrix: ' << intrisicMat << endl << endl cout << 'Rotation vector: ' << rVec << endl << endl cout << 'Translation vector: ' << tVec << endl << endl cout << 'Distortion coef: ' << distCoeffs << endl << endl std::vector projectedPoints cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints) /*for (unsigned int i = 0 i < projectedPoints.size() ++i) { cout << 'Image point: ' << imagePoints[i] << ' Projected to ' << projectedPoints[i] << endl }*/ cout << 'Press any key to exit.' cin.ignore() cin.get() return 0 } vector Generate3DPoints() { vector points float x, y, z x = .5 y = .5 z = -.5 points.push_back(cv::Point3f(x, y, z)) x = .5 y = .5 z = .5 points.push_back(cv::Point3f(x, y, z)) x = -.5 y = .5 z = .5 points.push_back(cv::Point3f(x, y, z)) x = -.5 y = .5 z = -.5 points.push_back(cv::Point3f(x, y, z)) x = .5 y = -.5 z = -.5 points.push_back(cv::Point3f(x, y, z)) x = -.5 y = -.5 z = -.5 points.push_back(cv::Point3f(x, y, z)) x = -.5 y = -.5 z = .5 points.push_back(cv::Point3f(x, y, z)) for(unsigned int i = 0 i < points.size() ++i) { cout << points[i] << endl << endl } return points }

参照: http://www.itkeyword.com/doc/8898489402460001x144/opencvs-projectpoints-function