-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerspective.cpp
193 lines (165 loc) · 4.19 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <opencv2/core/utility.hpp>
#include <opencv/highgui.h>
#include <cstdlib>
#include <iostream>
#include "Perspective.h"
using namespace cv;
using namespace std;
void Perspective::TransformImage(cv::Mat & image)
{
warpPerspective(image, image, lambda, image.size());
}
Perspective::Perspective(cv::Mat image, Perspective::InitializationType type, Perspective::TransformMatrixType way)
{
if (way == Perspective::TransformMatrixType::FromGUI)
{
if (type == Perspective::InitializationType::Manual)
{
Mat image_for_text;
setOutputQuad(image);
vector<Point2f> input;
image.copyTo(image_for_text);
namedWindow("Source window", WINDOW_NORMAL);
resizeWindow("Source window", 400, 300);
setMouseCallback("Source window", mouseCallback, &input);
while (1)
{
imshow("Source window", image_for_text);
image.copyTo(image_for_text);
switch (input.size())
{
case 0:
putText(image_for_text, "Choose 4 points", Point2f(30, 30),
FONT_HERSHEY_COMPLEX_SMALL, 2, cvScalar(0, 0, 0), 1, CV_AA);
break;
case 1:
putText(image_for_text, "Choose 3 points", Point2f(30, 30),
FONT_HERSHEY_COMPLEX_SMALL, 2, cvScalar(0, 0, 0), 1, CV_AA);
break;
case 2:
putText(image_for_text, "Choose 2 points", Point2f(30, 30),
FONT_HERSHEY_COMPLEX_SMALL, 2, cvScalar(0, 0, 0), 1, CV_AA);
break;
case 3:
putText(image_for_text, "Choose 1 point", Point2f(30, 30),
FONT_HERSHEY_COMPLEX_SMALL, 2, cvScalar(0, 0, 0), 1, CV_AA);
break;
default:
putText(image_for_text, "Please click space", Point2f(30, 30),
FONT_HERSHEY_COMPLEX_SMALL, 2, cvScalar(0, 0, 0), 1, CV_AA);
break;
}
if (input.size() > 0) //we have 2 points
{
for (auto it = input.begin(); it != input.end(); ++it)
{
circle(image, Point2f((*it).x, (*it).y), 10, Scalar(0, 0, 0), -2);
}
}
if (waitKey(20) == 32)
{
destroyWindow("Source window");
break;
}
//niepotrzebne - waitKey jest wywolywane juz w ifie
//else
//waitKey(10);
}
setInputQuad(input);
}
else if (type == Perspective::InitializationType::Auto)
{
setOutputQuad(image);
}
sortQuads();
lambda = Mat::zeros(2, 4, CV_32FC1);
lambda = getPerspectiveTransform(inputQuad, outputQuad);
WriteToFile();
}
else if (way == Perspective::TransformMatrixType::FromFile)
{
ReadFromFile();
}
}
void mouseCallback(int event, int x, int y, int, void * param)
{
vector<Point2f> *p = static_cast<vector<Point2f> *>(param);
if (p->size() == 4)
{
setMouseCallback("Source Window", NULL, NULL);
}
else if (event == EVENT_LBUTTONDOWN)
{
vector<Point2f> *p = static_cast<vector<Point2f> *>(param);
p->push_back(Point2f(x, y));
}
}
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);
}
}
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];
}
}
void Perspective::WriteToFile()
{
FileStorage fs("lambda.xml", FileStorage::WRITE);
if (fs.isOpened())
{
fs << "lambda" << lambda;
fs.release();
}
else
cout << "Error: file cannot be opened" << endl;
}
void Perspective::ReadFromFile()
{
FileStorage fs2("lambda.xml", FileStorage::READ);
if (fs2.isOpened())
{
fs2["lambda"] >> lambda;
fs2.release();
}
else
cout << "Error: file cannot be opened" << endl;
}
Perspective::~Perspective()
{
}