|
8 | 8 | #include "opencv2/imgproc.hpp"
|
9 | 9 | #include "opencv2/imgcodecs.hpp"
|
10 | 10 | #include "opencv2/highgui.hpp"
|
11 |
| -#include "opencv2/calib3d.hpp" |
12 | 11 | #include <iostream>
|
13 | 12 |
|
14 | 13 | using namespace std;
|
@@ -36,6 +35,7 @@ Mat warping(Mat image, Size warped_image_size, vector< Point2f> srcPoints, vecto
|
36 | 35 | String windowTitle = "Perspective Transformation Demo";
|
37 | 36 | String labels[4] = { "TL","TR","BR","BL" };
|
38 | 37 | vector< Point2f> roi_corners;
|
| 38 | +vector< Point2f> midpoints(4); |
39 | 39 | vector< Point2f> dst_corners(4);
|
40 | 40 | int roiIndex = 0;
|
41 | 41 | bool dragging;
|
@@ -99,21 +99,26 @@ int main(int argc, char** argv)
|
99 | 99 |
|
100 | 100 | imshow( windowTitle, image );
|
101 | 101 |
|
| 102 | + midpoints[0] = (roi_corners[0] + roi_corners[1]) / 2; |
| 103 | + midpoints[1] = (roi_corners[1] + roi_corners[2]) / 2; |
| 104 | + midpoints[2] = (roi_corners[2] + roi_corners[3]) / 2; |
| 105 | + midpoints[3] = (roi_corners[3] + roi_corners[0]) / 2; |
| 106 | + |
102 | 107 | dst_corners[0].x = 0;
|
103 | 108 | dst_corners[0].y = 0;
|
104 |
| - dst_corners[1].x = (float)std::max(norm(roi_corners[0] - roi_corners[1]), norm(roi_corners[2] - roi_corners[3])); |
| 109 | + dst_corners[1].x = (float)norm(midpoints[1] - midpoints[3]); |
105 | 110 | dst_corners[1].y = 0;
|
106 |
| - dst_corners[2].x = (float)std::max(norm(roi_corners[0] - roi_corners[1]), norm(roi_corners[2] - roi_corners[3])); |
107 |
| - dst_corners[2].y = (float)std::max(norm(roi_corners[1] - roi_corners[2]), norm(roi_corners[3] - roi_corners[0])); |
| 111 | + dst_corners[2].x = dst_corners[1].x; |
| 112 | + dst_corners[2].y = (float)norm(midpoints[0] - midpoints[2]); |
108 | 113 | dst_corners[3].x = 0;
|
109 |
| - dst_corners[3].y = (float)std::max(norm(roi_corners[1] - roi_corners[2]), norm(roi_corners[3] - roi_corners[0])); |
| 114 | + dst_corners[3].y = dst_corners[2].y; |
110 | 115 |
|
111 | 116 | Size warped_image_size = Size(cvRound(dst_corners[2].x), cvRound(dst_corners[2].y));
|
112 | 117 |
|
113 |
| - Mat H = findHomography(roi_corners, dst_corners); //get homography |
| 118 | + Mat M = getPerspectiveTransform(roi_corners, dst_corners); |
114 | 119 |
|
115 | 120 | Mat warped_image;
|
116 |
| - warpPerspective(original_image, warped_image, H, warped_image_size); // do perspective transformation |
| 121 | + warpPerspective(original_image, warped_image, M, warped_image_size); // do perspective transformation |
117 | 122 |
|
118 | 123 | imshow("Warped Image", warped_image);
|
119 | 124 | }
|
|
0 commit comments