-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerspective.cpp
76 lines (62 loc) · 1.52 KB
/
Perspective.cpp
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
69
70
71
72
73
74
75
76
#include "Perspective.h"
using namespace cv;
using namespace std;
//Get perspective transform
void Perspective::TransformImage(cv::Mat & image)
{
sortQuads();
lambda = Mat::zeros(2, 4, CV_32FC1);
lambda = getPerspectiveTransform(inputQuad, outputQuad);
warpPerspective(image, image, lambda, image.size());
}
Perspective::Perspective()
{
}
void Perspective::setOutputQuad(const cv::Mat & image)
{
outputQuad[0] = Point2f(0, 0);
outputQuad[1] = Point2f(0, float(image.rows - 1));
outputQuad[2] = Point2f(float(image.cols - 1), 0);
outputQuad[3] = Point2f(float(image.cols - 1), float(image.rows - 1));
}
void Perspective::setInputQuad(std::vector<cv::Point2f> points)
{
if (points.size() < 4)
{
cout << "Not enough points" << endl;
return;
}
for (int i = 0; i < 4; i++)
{
inputQuad[i] = points.at(i);
}
}
//Sort input points
void Perspective::sortQuads()
{
Point2f sortedQuad[4];
float distance;
float min = 0;
for (int i = 0; i < 4; i++)
{
min = sqrt(pow((outputQuad[0].x - inputQuad[i].x), 2) + pow((outputQuad[0].y - inputQuad[i].y), 2));
int best = 0;
for (int j = 0; j < 4; j++)
{
distance = sqrt(pow((outputQuad[j].x - inputQuad[i].x), 2) + pow((outputQuad[j].y - inputQuad[i].y), 2));
if (distance < min)
{
min = distance;
best = j;
}
}
sortedQuad[best] = inputQuad[i];
}
for (int i = 0; i < 4; i++)
{
inputQuad[i] = sortedQuad[i];
}
}
Perspective::~Perspective()
{
}