单应性矩阵

单应性矩阵的实质就是图片仿射变换,采用了RANSAC多组点对进行计算最优H矩阵方法。

 

 1 #include "opencv2/core/core.hpp"
 2 #include "opencv2/imgproc/imgproc.hpp"
 3 #include "opencv2/calib3d/calib3d.hpp"
 4 #include "opencv2/highgui/highgui.hpp"
 5 #include <iostream>
 6 #include <fstream>
 7 
 8 using namespace cv;
 9 using namespace std;
10 
11 int main()
12 {
13     vector<Point2f> Pix_Point;
14     vector<Point3f>Real_Point;
15     Point adjust_view = Point(200, 200);
16     FileStorage fs2("./img/frontPoint.xml", FileStorage::READ);
17 
18     FileNode PixPoint = fs2["PixPoint"];
19     FileNodeIterator it = PixPoint.begin(), it_end = PixPoint.end();
20 
21     int idx = 1;
22     //使用FileNodeIterator遍历序列
23     for (; it != it_end; ++it, ++idx)
24     {
25         Point2f P_Point;
26         cout << "PixPoint" << idx << ":";
27         cout << "x=" << (int)(*it)["x"] << ", y = " << (int)(*it)["y"] << endl;
28         P_Point.x = (int)(*it)["x"];
29         P_Point.y = (int)(*it)["y"];
30         Pix_Point.push_back(P_Point);
31     }
32 
33     FileNode RealPoint = fs2["RealPoint"];
34     FileNodeIterator it1 = RealPoint.begin(), it1_end = RealPoint.end();
35 
36     int idx1 = 1;
37     //使用FileNodeIterator遍历序列
38     for (; it1 != it1_end; ++it1, ++idx1) 
39     {
40         Point3f R_Point;
41         cout << "RealPoint" << idx1 << ":";
42         cout << "x=" << (int)(*it1)["x"] << ", y = " << (int)(*it1)["y"] << endl;
43         R_Point.x = (int)(*it1)["x"] + adjust_view.x;
44         R_Point.y = (int)(*it1)["y"] + adjust_view.y;
45         R_Point.z = 0;
46         Real_Point.push_back(R_Point);
47     }
48 
49     fs2.release();
50 
51     Size image_size;
52     vector<vector<Point2f>> image_points_seq;
53     string filename;
54     int i = 2;
55     int count;
56     filename = "./img/front.jpg";
57     Mat imageInput = imread(filename);
58     imshow("imageInput", imageInput);
59 
60     image_size.width = imageInput.cols;
61     image_size.height = imageInput.rows;
62     cout << "image_size.width = " << image_size.width << endl;
63     cout << "image_size.height = " << image_size.height << endl;
64 
65     Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
66     Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0));
67     Mat tvecsMat;
68     Mat rvecsMat;
69     Mat mask;
70 
71     Mat affine = findHomography(Pix_Point, Real_Point, CV_RANSAC, 3, mask);
72     ofstream fout("./caliberat3_result.txt");
73     fout << "单应性矩阵:" << endl;
74     fout << affine << endl;
75     fout << "掩码矩阵:" << endl;
76     fout << mask << endl;
77     fout << endl;
78 
79     FileStorage fs1("./img/Mat.xml", FileStorage::WRITE);
80     cameraMatrix = (Mat_<double>(3, 3) << 329, 0, 640, 0, 329, 400, 0, 0, 1);
81     fs1 << "cameraMatrix" << cameraMatrix;
82     fs1 << "H_Mat" << affine;
83     fs1.release();
84 
85     Mat imageOutput;
86     warpPerspective(imageInput, imageOutput, affine, imageInput.size(), INTER_LINEAR);
87     //transpose(imageOutput, imageOutput);
88     imshow("imageOutput", imageOutput);
89     //Mat TestImg, TestOut;
90     //TestImg = imread("./img/unfront.jpg");
91     //warpPerspective(TestImg, TestOut, affine, imageInput.size(), INTER_LINEAR);
92     //imshow("TestOut", TestOut);
93 
94     waitKey(0);
95 
96     return 0;
97 }

 

 

 

 

posted @ 2023-05-09 17:11  量子与太极  阅读(83)  评论(0)    收藏  举报