-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cpp
More file actions
68 lines (61 loc) · 2.18 KB
/
transform.cpp
File metadata and controls
68 lines (61 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<vector>
#include<opencv2/opencv.hpp>
std::vector<cv::Point> order_points(std::vector<cv::Point> pts);
cv::Mat perspectiveTransform(cv::Mat image, std::vector <cv::Point> pts)
{
//for (int i = 0; i != pts.size(); ++i) std::cout << pts[i] << " "; std::cout<<std::endl;
std::vector<cv::Point> rect = order_points(pts);
for (int i = 0; i != rect.size(); ++i) std::cout << rect[i] << " "; std::cout << std::endl;
float points[8], tl[2], tr[2], br[2], bl[2];
points[0] = tl[0] = rect.at(0).x;
points[1] = tl[1] = rect.at(0).y;
points[2] = tr[0] = rect.at(1).x;
points[3] = tr[1] = rect.at(1).y;
points[4] = br[0] = rect.at(2).x;
points[5] = br[1] = rect.at(2).y;
points[6] = bl[0] = rect.at(3).x;
points[7] = bl[1] = rect.at(3).y;
cv::Mat src = cv::Mat(4, 2, CV_32F, &points);
//std::cout << "done\n";
src.reshape(4, 2);
float a, b, maxWidth, maxHeight;
a = sqrt(pow((br[0] - bl[0]), 2) + pow((br[1] - bl[1]), 2));
b = sqrt(pow((tr[0] - tl[0]), 2) + pow((tr[1] - tl[1]), 2));
maxWidth = std::max(int(a), int(b));
a = sqrt(pow((tr[0] - br[0]), 2) + pow((tr[1] - br[1]), 2));
b = sqrt(pow((tl[0] - bl[0]), 2) + pow((tl[1] - bl[1]), 2));
maxHeight = std::max(int(a), int(b));
float dstdata[8] = { 0, 0,maxWidth - 1, 0 , maxWidth - 1, maxHeight - 1,0, maxHeight - 1 };
cv::Mat dst = cv::Mat(4, 2, CV_32F, &dstdata);
dst.reshape(4, 2);
// compute the perspective transform cv::Matrix and then apply it
cv::Mat M, warped;
M = getPerspectiveTransform(src, dst);
warpPerspective(image, warped, M, cv::Size(maxWidth, maxHeight));
// return the warped image
//std::cout << cv::Size(maxWidth, maxHeight);
return warped;
}
std::vector<cv::Point> order_points(std::vector<cv::Point> pts)
{
std::vector<cv::Point> rect;
//cout << "order_points_start";
sort(pts.begin(), pts.end(), [](cv::Point &a, cv::Point &b){return ((a.x + a.y) < (b.x + b.y));});
//cout << "sort done";
rect.push_back(pts.at(0));
if (pts.at(1).x > pts.at(2).x)
{
rect.push_back(pts.at(1));
rect.push_back(pts.at(3));
rect.push_back(pts.at(2));
}
else
{
rect.push_back(pts.at(2));
rect.push_back(pts.at(3));
rect.push_back(pts.at(1));
}
//cout << "order_points_end";
return rect;
}