-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·248 lines (210 loc) · 6.84 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<iostream>
#include"include/parameter.h"
#include"include/utility.h"
#include"include/stdGpt.h"
#include"include/acclGpt.h"
#include "include/acclGpt_cuda.h"
using namespace std;
/* Image storage arrays */
unsigned char image1[MAX_IMAGESIZE][MAX_IMAGESIZE];
unsigned char image2[MAX_IMAGESIZE][MAX_IMAGESIZE];
int x_size1 = COL, y_size1 = ROW; /* width & height of image1*/
int x_size2, y_size2; /* width & height of image2 */
double H1[ROW][COL * 162], Ht1[ROW][COL * 27];
double H2[ROW - 4][(COL - 4) * 6 * 64 * 3], Ht2[ROW - 4][(COL - 4) * 64 * 3];
double H3[ROW - 4][(COL - 4) * 6 * 64 * 3], Ht3[ROW - 4][(COL - 4) * 64 * 3];
double D1[ROW][COL * 8];
double D2[ROW - 4][(COL - 4) * 64];
double ndis[(2 * ROW - 1) * (2 * COL - 1)];
int coor[(2 * ROW - 1) * (2 * COL - 1)][2];
int main() {
int image3[ROW2][COL2], image4[ROW][COL]; // image3: test image image4: training image
int x, y, iter;
char csvname[MAX_FILENAME], foldername[MAX_FILENAME]; // GAT, NGAT, GPT, NGPT, name of .csv file, foldername
double gk[ROW][COL], gwt[ROW][COL], dnn, temp_dnn, var; // Gaussian window initial, Gaussian window, window size, variance
int g_ang1[ROW][COL], g_ang2[ROW][COL]; // direction of gradients
char g_HoG1[ROW][COL][8], g_HoG2[ROW][COL][8]; // HoG feature of the images
char sHoG1[ROW - 4][COL - 4], sHoG2[ROW - 4][COL - 4];
double g_nor1[ROW][COL], g_nor2[ROW][COL]; // norm of gradients
double g_can1[ROW][COL], g_can2[ROW][COL]; // canonicalized images
double g_can11[ROW - CANMARGIN][COL - CANMARGIN], g_can22[ROW - CANMARGIN][COL - CANMARGIN];
// canonicalized images center
double old_cor0, old_cor1, new_cor1; //
double org_cor, gat_corf, gat_corb;
double gpt0[3][3], gpt1[3][3], gptInv[3][3];
clock_t start, end;
double elapse;
char fileName[128];
cuda_init_parameter();
/* Initialize the GPT matrix */
initGpt(gpt0);
initGpt2(gpt1, ZOOM, ZOOM*BETA, B1, B2, ROT);
/* initialize Gauss window function */
for (y = 0; y < ROW; y++)
for (x = 0; x < COL; x++)
gk[y][x] = exp(-(x*x+y*y)/2.0);
/* Load template image and save it to image4, the local memory */
sprintf(fileName, "%s/%s.pgm", IMGDIR, RgIMAGE);
load_image_file(fileName, image1, COL, ROW);
for (y = 0; y < ROW; y++)
for (x = 0; x < COL; x++)
image4[y][x] = image1[y][x];
procImg(g_can2, g_ang2, g_nor2, g_HoG2, sHoG2, image1);
/* Make template tables if required */
#if MAKETEMP != 0
sprintf(fileName, "%s/%s", IMGDIR, RgIMAGE);
makeTemp(g_ang2, g_can2, gk, H1, fileName);
makeTemp64(sHoG2, g_can2, gk, H2, fileName);
// makeTemp64_far(sHoG2, g_can2, gk, H3, fileName);
winTbl(g_ang2, D1, fileName);
winTbl64(sHoG2, D2, fileName);
searchTbl(ROW, COL, fileName);
return 0;
#else
loadTbls(D1, D2, ndis, coor);
loadTemp(H1);
loadTemp64(H2);
loadTemp64_far(H3);
#endif
copy_initial_parameters(gk,g_can2,g_ang2,H1,H2,H3,D1,D2,sHoG2,ndis, coor);
/* Load test image and save it to image3, the local memory */
sprintf(fileName, "%s/%s.pgm", IMGDIR, TsIMAGE);
load_image_file(fileName, image1, COL2, ROW2);
for (y = 0; y < ROW2; y++)
for (x = 0; x < COL2; x++)
image3[y][x] = image1[y][x];
/* save the initial image */
for (y = 0; y < ROW2; y++)
for (x = 0; x < COL2; x++)
image2[y][x] = image1[y][x];
bilinear_normal_projection(gpt1, COL, ROW, COL2, ROW2, image1, image2);
sprintf(fileName, "%s/%s_init.pgm", IMGDIR, RgIMAGE);
save_image_file(fileName, image2, COL, ROW);
procImg(g_can1, g_ang1, g_nor1, g_HoG1, sHoG1, image2);
/***************Pre-setting finish***************/
/* calculate the initial correlation */
old_cor1 = 0.0;
for (y = MARGINE ; y < ROW - MARGINE ; y++)
for (x = MARGINE ; x < COL - MARGINE ; x++)
old_cor1 += g_can1[y][x] * g_can2[y][x];
org_cor = old_cor1;
printf("Original cor. = %f\n", org_cor);
old_cor0 = old_cor1;
/* calculate the initial dnn */
switch (DISTANCETYPE) {
case 0:
dnn = winpat(g_ang1, g_ang2);
if (dnn > DNNSWITCHTHRE)
dnn = sHoGpat(sHoG1, sHoG2);
break;
case 1:
dnn = winpat(g_ang1, g_ang2);
break;
case 2:
dnn = fwinpat(g_ang1, g_ang2, D1, ndis, coor);
break;
case 3:
dnn = sHoGpat(sHoG1, sHoG2);
break;
case 4:
dnn = fsHoGpat(sHoG1, sHoG2, D2, ndis, coor);
break;
case 10:
dnn = fsHoGpat(sHoG1, sHoG2, D2, ndis, coor);
if (dnn <= DNNSWITCHTHRE)
dnn = fwinpat(g_ang1, g_ang2, D1, ndis, coor);
break;
}
/***************Main iteration loop*************/
/* lap the start time */
start = clock();
for (iter = 0 ; iter < MAXITER ; iter++) {
/* update gauss window function */
var = pow(WGT * dnn, 2);
#if isGPU == 0
for (y = 0; y < ROW; y++)
for (x = 0; x < COL; x++)
gwt[y][x] = pow(gk[y][x], 1.0 / var);
#elif isGPU == 1
calc_gwt(var, gwt);
#endif
/* select matching method */
switch (MATCHMETHOD) {
case 1:
break;
case 6:
nsgptcor(g_ang1, g_can1, g_ang2, g_can2, gwt, gpt1, dnn);
break;
case 7:
nsgptcorSpHOG5x5(g_ang1, sHoG1, g_can1, g_ang2, sHoG2, g_can2, gwt, gpt1, dnn);
break;
case 16:
fnsgptcor(g_ang1, g_can1, gpt1, dnn, H1, Ht1);
break;
case 17:
if(dnn < 8.0)
fnsgptcorSpHOG5x5(g_ang1, sHoG1, g_can1, gpt1, dnn, H2, Ht2);
else
fnsgptcorSpHOG5x5_far(g_ang1, sHoG1, g_can1, gpt1, dnn, H3, Ht3);
}
/* transform the test image and update g_can1, g_ang1, g_nor1, g_HoG1, sHoG1 */
for (y = 0; y < ROW2; y++)
for (x = 0; x < COL2; x++)
image1[y][x] = (unsigned char)image3[y][x];
bilinear_normal_projection(gpt1, COL, ROW, COL2, ROW2, image1, image2);
procImg(g_can1, g_ang1, g_nor1, g_HoG1, sHoG1, image2);
/* update correlation */
#if isGPU == 0
new_cor1 = 0.0;
for (y = MARGINE ; y < ROW - MARGINE ; y++){
for (x = MARGINE ; x < COL - MARGINE ; x++){
new_cor1 += g_can1[y][x] * g_can2[y][x];
}
}
#elif isGPU == 1
new_cor1 = calc_new_cor1();
#endif
/* Calculation distance */
switch (DISTANCETYPE) {
case 0:
if (dnn < DNNSWITCHTHRE) {
dnn = winpat(g_ang1, g_ang2);
} else {
dnn = sHoGpat(sHoG1, sHoG2);
}
break;
case 1:
dnn = winpat(g_ang1, g_ang2);
break;
case 2:
dnn = fwinpat(g_ang1, g_ang2, D1, ndis, coor);
break;
case 3:
dnn = sHoGpat(sHoG1, sHoG2);
break;
case 4:
dnn = fsHoGpat(sHoG1, sHoG2, D2, ndis, coor);
break;
case 10:
dnn = fwinpat(g_ang1, g_ang2, D1, ndis, coor);
if (dnn > DNNSWITCHTHRE)
dnn = fsHoGpat(sHoG1, sHoG2, D2, ndis, coor);
break;
}
/* display message */
printf("iter = %d, new col. = %f dnn = %f var = %f\n", iter, new_cor1, dnn, 1 / var);
}
/* display the calculation time */
end = clock();
elapse = (double)(end - start) / CLOCKS_PER_SEC;
string device = isGPU?"GPU":"CPU";
printf("\n%s elapsed time = %.3f sec\n\n", device.c_str(),elapse);
}