forked from stupel/Preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 2
/
orientationmap.cpp
executable file
·264 lines (209 loc) · 10.6 KB
/
orientationmap.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "orientationmap.h"
OrientationMap::OrientationMap(QObject *parent) : QObject(parent)
{
this->duration = 0;
}
void OrientationMap::setParams(const cv::Mat &imgFingerprint, OMAP_PARAMS omapParams)
{
this->imgInput = imgFingerprint;
this->omap = omapParams;
}
void OrientationMap::computeBasicMapCPU()
{
this->timer.start();
cv::Mat Gx, Gy;
int height, width;
float Vx, Vy;
height = floor(this->imgInput.rows / this->omap.blockSize);
width = floor(this->imgInput.cols / this->omap.blockSize);
int paddingX = this->imgInput.cols - width*this->omap.blockSize;
int paddingY = this->imgInput.rows - height*this->omap.blockSize;
// BASIC smerova mapa
this->oMap_basic = cv::Mat(height, width, CV_32F);
// vypocet gradientov x a y
cv::Sobel(this->imgInput, Gx, CV_32FC1, 1, 0);
cv::Sobel(this->imgInput, Gy, CV_32FC1, 0, 1);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Vx = 0.0; Vy = 0.0;
for (int i = y * this->omap.blockSize + paddingY / 2; i < y * this->omap.blockSize + paddingY / 2 + this->omap.blockSize; i++){
for (int j = x * this->omap.blockSize + paddingX / 2; j < x * this->omap.blockSize + paddingX / 2 + this->omap.blockSize; j++){
Vx += (2 * (Gx.at<float>(i,j) * Gy.at<float>(i, j)));
Vy += pow(Gx.at<float>(i,j),2) - pow(Gy.at<float>(i, j), 2);
}
}
this->oMap_basic.at<float>(y, x) = 0.5 * atan2(Vx, Vy);
}
}
//vyhladenie smerovej mapy
cv::Mat sinTheta(this->oMap_basic.size(), CV_32F);
cv::Mat cosTheta(this->oMap_basic.size(), CV_32F);
for(int i = 0; i < this->oMap_basic.rows; i++){
for(int j = 0; j < this->oMap_basic.cols; j++){
cosTheta.at<float>(i, j) = cos(2 * this->oMap_basic.at<float>(i, j));
sinTheta.at<float>(i, j) = sin(2 * this->oMap_basic.at<float>(i, j));
}
}
cv::GaussianBlur(cosTheta, cosTheta, cv::Size(this->omap.gaussBlurBasic.blockSize, this->omap.gaussBlurBasic.blockSize), this->omap.gaussBlurBasic.sigma,this->omap.gaussBlurBasic.sigma);
cv::GaussianBlur(sinTheta, sinTheta, cv::Size(this->omap.gaussBlurBasic.blockSize, this->omap.gaussBlurBasic.blockSize), this->omap.gaussBlurBasic.sigma,this->omap.gaussBlurBasic.sigma);
for(int i = 0; i < this->oMap_basic.rows;i++) {
for(int j = 0; j < this->oMap_basic.cols; j++) {
this->oMap_basic.at<float>(i, j) = 0.5 * atan2(sinTheta.at<float>(i, j), cosTheta.at<float>(i, j));
}
}
this->duration = this->timer.elapsed();
}
void OrientationMap::computeBasicMapGPU()
{
af::timer::start();
this->imgInputAF = Helper::mat_uchar2array_uchar(this->imgInput);
af::array Gx, Gy;
int height, width;
af::array Vx, Vy;
height = floor(this->imgInputAF.dims(0) / this->omap.blockSize);
width = floor(this->imgInputAF.dims(1) / this->omap.blockSize);
int paddingX = this->imgInputAF.dims(1) - width * this->omap.blockSize;
int paddingY = this->imgInputAF.dims(0) - height * this->omap.blockSize;
// vypocet gradientov x a y
af::sobel(Gy, Gx, this->imgInputAF,3); // neuveritelna frajerina, po novom musi byt Gx a Gy v opacnom poradi
// vypocet Vx,Vy a Theta
af::array GxCut = Gx(af::seq(paddingY / 2, height * this->omap.blockSize + paddingY / 2 - 1), af::seq(paddingX / 2, width * this->omap.blockSize+paddingX / 2 - 1));
af::array GyCut = Gy(af::seq(paddingY / 2, height * this->omap.blockSize + paddingY / 2 - 1), af::seq(paddingX / 2, width * this->omap.blockSize + paddingX / 2 - 1));
GxCut = af::unwrap(GxCut, this->omap.blockSize, this->omap.blockSize, this->omap.blockSize, this->omap.blockSize);
GyCut = af::unwrap(GyCut, this->omap.blockSize, this->omap.blockSize, this->omap.blockSize, this->omap.blockSize);
Vx = af::sum(2 * GxCut * GyCut);
Vy = af::sum(af::pow(GxCut, 2) - af::pow(GyCut, 2));
this->oMapAF_basic = 0.5* af::atan2(Vx.as(f32), Vy.as(f32));
this->oMapAF_basic = af::moddims(this->oMapAF_basic, height, width);
this->oMap_basic = Helper::array_float2mat_float(this->oMapAF_basic);
// vyhladenie smerovej mapy
af::array sinTheta = af::sin(2 * this->oMapAF_basic);
af::array cosTheta = af::cos(2 * this->oMapAF_basic);
af::array gk = af::gaussianKernel(this->omap.gaussBlurBasic.blockSize, this->omap.gaussBlurBasic.blockSize, this->omap.gaussBlurBasic.sigma, this->omap.gaussBlurBasic.sigma);
sinTheta = af::convolve(sinTheta, gk);
cosTheta = af::convolve(cosTheta, gk);
this->oMapAF_basic = 0.5* af::atan2(sinTheta, cosTheta);
this->oMap_basic = Helper::array_float2mat_float(this->oMapAF_basic);
this->duration = af::timer::stop() * 1000;
}
void OrientationMap::computeAdvancedMapCPU()
{
this->computeBasicMapCPU();
this->timer.start();
// expanzia smerovej mapy
this->oMap_advanced = cv::Mat(this->imgInput.rows, this->imgInput.cols, this->oMap_basic.type());
cv::Mat blk;
for(int i = 0; i < this->oMap_basic.rows; i++) {
for(int j = 0; j < this->oMap_basic.cols; j++) {
blk = this->oMap_advanced.rowRange(i * this->omap.blockSize, i * this->omap.blockSize + this->omap.blockSize).colRange(j * this->omap.blockSize, j * this->omap.blockSize + this->omap.blockSize);
blk.setTo(cv::Scalar(this->oMap_basic.at<float>(i, j)));
}
}
// vyhladenie expandovanej smerovej mapy
cv::Mat sinTheta_Advanced(this->oMap_advanced.size(), CV_32F);
cv::Mat cosTheta_Advanced(this->oMap_advanced.size(), CV_32F);
for(int i = 0; i < this->oMap_advanced.rows; i++) {
for(int j = 0; j < this->oMap_advanced.cols; j++) {
cosTheta_Advanced.at<float>(i, j) = cos(2 * this->oMap_advanced.at<float>(i, j));
sinTheta_Advanced.at<float>(i, j) = sin(2 * this->oMap_advanced.at<float>(i, j));
}
}
cv::GaussianBlur(cosTheta_Advanced, cosTheta_Advanced, cv::Size(this->omap.gaussBlurAdvanced.blockSize, this->omap.gaussBlurAdvanced.blockSize), this->omap.gaussBlurAdvanced.sigma, this->omap.gaussBlurAdvanced.sigma);
cv::GaussianBlur(sinTheta_Advanced, sinTheta_Advanced, cv::Size(this->omap.gaussBlurAdvanced.blockSize, this->omap.gaussBlurAdvanced.blockSize), this->omap.gaussBlurAdvanced.sigma, this->omap.gaussBlurAdvanced.sigma);
for(int i = 0; i < this->oMap_advanced.rows; i++) {
for(int j = 0; j < this->oMap_advanced.cols; j++) {
this->oMap_advanced.at<float>(i, j) = 0.5 * atan2(sinTheta_Advanced.at<float>(i, j), cosTheta_Advanced.at<float>(i, j));
}
}
this->duration += this->timer.elapsed();
}
void OrientationMap::computeAdvancedMapGPU()
{
af::timer::start();
// compute the basic O-Map first
this->computeBasicMapGPU();
// basic O-Map expansion
this->oMapAF_advanced = af::moddims(this->oMapAF_basic, 1, this->oMapAF_basic.dims(0) * this->oMapAF_basic.dims(1));
this->oMapAF_advanced = af::tile(this->oMapAF_advanced, this->omap.blockSize * this->omap.blockSize);
this->oMapAF_advanced = af::wrap(this->oMapAF_advanced,
this->oMapAF_basic.dims(0) * this->omap.blockSize,
this->oMapAF_basic.dims(1) * this->omap.blockSize,
this->omap.blockSize,
this->omap.blockSize,
this->omap.blockSize,
this->omap.blockSize);
// smoothing the expanded O-Map
af::array sinTheta = af::sin(2 * this->oMapAF_advanced);
af::array cosTheta = af::cos(2 * this->oMapAF_advanced);
af::array gk = af::gaussianKernel(this->omap.gaussBlurAdvanced.blockSize,
this->omap.gaussBlurAdvanced.blockSize,
this->omap.gaussBlurAdvanced.sigma,
this->omap.gaussBlurAdvanced.sigma);
sinTheta = af::convolve(sinTheta, gk);
cosTheta = af::convolve(cosTheta, gk);
this->oMapAF_advanced = 0.5* af::atan2(sinTheta, cosTheta);
this->oMap_advanced = Helper::array_float2mat_float(this->oMapAF_advanced);
this->duration += af::timer::stop();
}
void OrientationMap::drawBasicMap(const cv::Mat &imgOriginal)
{
// farebny obrazok smerovej mapy po vyhladeni
this->imgOMap_basic = cv::Mat(imgOriginal.rows, imgOriginal.cols, CV_8UC3);
cv::cvtColor(imgOriginal, this->imgOMap_basic, cv::COLOR_GRAY2RGB);
int height = floor(this->imgInput.rows / this->omap.blockSize);
int width = floor(this->imgInput.cols / this->omap.blockSize);
int paddingX = this->imgInput.cols - width * this->omap.blockSize;
int paddingY = this->imgInput.rows - height * this->omap.blockSize;
int rowsMat = this->oMap_basic.rows;
int colsMat = this->oMap_basic.cols;
float row1, col1, row2, col2, row3, col3, direction;
for (int y = 0; y<rowsMat; y++){
for(int x =0; x<colsMat; x++){
direction = this->oMap_basic.at<float>(y,x) + CV_PI / 2;
row1 = y * this->omap.blockSize + this->omap.blockSize / 2 + paddingY / 2;
col1 = x * this->omap.blockSize + this->omap.blockSize / 2 + paddingX / 2;
if(this->qMap.at<uchar>(row1,col1) == 1){
continue;
}
row2 = row1 - sin(direction) * this->omap.blockSize / 2;
col2 = col1 - cos(direction) * this->omap.blockSize / 2;
row3 = row1 + sin(direction) * this->omap.blockSize / 2;
col3 = col1 + cos(direction) * this->omap.blockSize / 2;
cv::Point endPoint(col2, row2);
cv::Point endPoint2(col3, row3);
cv::line(this->imgOMap_basic, endPoint, endPoint2, cv::Scalar(255,255,0), 1, 4, 0);
}
}
}
cv::Mat OrientationMap::getImgOMap_basic() const
{
return imgOMap_basic;
}
cv::Mat OrientationMap::getOMap_basic() const
{
return oMap_basic;
}
cv::Mat OrientationMap::getOMap_advanced() const
{
return oMap_advanced;
}
af::array OrientationMap::getOMapAF_advanced() const
{
return oMapAF_advanced;
}
const cv::Mat &OrientationMap::getQMap() const
{
return qMap;
}
void OrientationMap::setQMap(const cv::Mat &newQMap)
{
qMap = newQMap;
}
af::array OrientationMap::getOMapAF_basic() const
{
return oMapAF_basic;
}
float OrientationMap::getDuration() const
{
return duration;
}