基於opencv透視變換

using namespace std;
using namespace cv;

vector<cv::Point3f> Generate3DPoints();

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	const string fileName = "cam.xml";
	FileStorage fs(fileName, FileStorage::READ);
	if (!fs.isOpened())
		cout << "can't open txt document" << endl;

	FileNode n = fs.getFirstTopLevelNode();
	if (n.type() != FileNode::SEQ)
		cout << "node false" << endl;

	FileNodeIterator it = n.begin(), it_end = n.end();
	for (; it != it_end; ++it)
	{
		string fname = (string)*it;

		string fpath = samples::findFile(fileName.substr(0, 1 + 1) + fname, false);
		if (fpath.empty())
			fpath = samples::findFile(fname);

		fname = fpath;


	}

#if 0
	// Read 3D points
	vector<cv::Point3f> objectPoints = Generate3DPoints();
	vector<cv::Point2f> imagePoints;

	// Intrisic matrix
	Mat intrisicMat = (Mat_<float>(3, 3) <<
											1.6415318549788924e+003, 0, 0,
											0, 1.7067753507885654e+003, 0,
											5.3262822453148601e+002, 3.8095355839052968e+002, 1);

	// Rotation vector
	Mat rVec = (Mat_<float>(3, 1) << 
		-3.9277902400761393e-002,
		3.7803824407602084e-002,
		2.6445674487856268e-002
		);	

    // Translation vector
	cv::Mat tVec = (Mat_<float>(3, 1) << 
										 2.1158489381208221e+000,
										 -7.6847683212704716e+000,
										 2.6169795190294256e+001);

	// Distortion vector
	cv::Mat distCoeffs = (Mat_<float>(5,1) <<  
											-7.9134632415085826e-001,
											1.5623584435644169e+000,
											-3.3916502741726508e-002,
											-1.3921577146136694e-002,
											1.1430734623697941e-002);

	std::vector<cv::Point2f> projectedPoints;
	cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints);

	for (unsigned int i = 0; i < projectedPoints.size(); ++i)
	{
		cout << "Image point: " << objectPoints[i] << " Projected to " << projectedPoints[i] << endl;
	}


	cout << "Press any key to exit.";
	cin.ignore();
	cin.get();
#endif

	return a.exec();
}


vector<cv::Point3f> Generate3DPoints()
{
	vector<cv::Point3f> 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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章