-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_image.c
318 lines (298 loc) · 8.46 KB
/
match_image.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <SDL/SDL.h>
#define POINT_SIZE 5
SDL_Surface *screen, *leftscreen, *rightscreen;
bool mouseDrag = false;
int previousX,previousY;
SDL_Rect leftrect,rightrect;
int targetIndex = 0;
int width,height;
float cx,cy;
FILE* target_point;
typedef struct {
int width,height;
unsigned char* data;
} Image;
typedef struct {
unsigned char r,g,b;
} Color;
typedef struct {
float x1,y1,x2,y2;
} Match;
typedef struct {
int size,capacity;
void* data;
} List;
int compare_float (const void * a, const void * b) {
float c = *(float*) a - *(int*) b;
if (c > 0) return 1;
else if (c < 0) return -1;
else return 0;
}
void drawRect(SDL_Surface *surf,SDL_Rect rect,Color color) {
int top = rect.y < 0 ? 0 : rect.y ;
int bottom = rect.y + rect.h >= surf->h ? surf->h-1 : rect.y + rect.h;
int left = rect.x < 0 ? 0 : rect.x;
int right = rect.x + rect.w >= surf->w ? surf->w-1 : rect.x + rect.w;
//TOP
int i=left;
unsigned char* c = surf->pixels + top * surf->pitch + left*3;
while (i++ < right) {
*c++ = color.b;
*c++ = color.g;
*c++ = color.r;
}
//BOTTOM
i=left;
c = surf->pixels + bottom * surf->pitch + left*3;
while (i++ < right) {
*c++ = color.b;
*c++ = color.g;
*c++ = color.r;
}
//LEFT
i=top;
c = surf->pixels + top * surf->pitch + left*3;
while (i++ < bottom) {
c[0] = color.b;
c[1] = color.g;
c[2] = color.r;
c += surf->pitch;
}
//RIGHT
i=top;
c = surf->pixels + top * surf->pitch + right*3;
while (i++ < bottom) {
c[0] = color.b;
c[1] = color.g;
c[2] = color.r;
c += surf->pitch;
}
}
SDL_Rect getVarRect(int x1,int y1,int x2,int y2) {
int left = x1 < x2 ? x1 : x2;
int right = x1 > x2 ? x1 : x2;
int top = y1 < y2 ? y1 : y2;
int bottom = y1 > y2 ? y1 : y2;
SDL_Rect r = {left,top,right-left,bottom-top};
return r;
}
void drawKeyPoint(SDL_Surface *surf,int x,int y,Color color) {
SDL_Rect r = {x-POINT_SIZE,y-POINT_SIZE,POINT_SIZE*2,POINT_SIZE*2};
drawRect(surf,r,color);
}
void imgcpy(Image image,SDL_Surface* surf) {
unsigned char* src = image.data, *dst = surf->pixels;
for (int i=0;i<image.height;i++) {
memcpy(dst,src,image.width*3);
src += image.width*3;
dst += surf->pitch;
}
}
bool showRightImage(int index,List* matches,Image rightimage) {
char buffer[128];
char filename[128];
sprintf(filename,"%d.match",index);
FILE* key_match = fopen(filename,"r");
if (!key_match) {
printf("Cannot open %s\n",filename);
return false;
}
matches->size=0;
while (fgets(buffer,128,key_match)) {
Match m;
int id1,id2;
if (sscanf(buffer,"%d %f %f %d %f %f",&id1,&m.x1,&m.y1,&id2,&m.x2,&m.y2)==6) {
if (matches->size == matches->capacity) {
matches->data = realloc(matches->data,matches->capacity*2*sizeof(Match));
matches->capacity *= 2;
}
((Match*)matches->data)[matches->size++] = m;
}
}
printf("Loaded %d matches from %s\n",matches->size,filename);
fclose(key_match);
sprintf(filename,"%d.pgm",index);
FILE* ppm = fopen(filename,"r");
fgets(buffer,128,ppm); //P5 or P6
fgets(buffer,128,ppm);
fgets(buffer,128,ppm); //255
fread(rightimage.data,1,rightimage.width*rightimage.height,ppm);
//populate gray level
for (int i=rightimage.width*rightimage.height;i>=0;i--) {
rightimage.data[i*3+2] = rightimage.data[i*3+1] = rightimage.data[i*3] = rightimage.data[i];
}
imgcpy(rightimage,rightscreen);
fclose(ppm);
return true;
}
void findCorrespondence(List matches,List rl) {
Match* m = (Match*)matches.data;
Color red = {255,0,0};
fprintf(target_point,"%d ",rl.size);
for (int j=0;j<rl.size;j++) {
SDL_Rect rect = ((SDL_Rect*)rl.data)[j];
if (rect.w <= 0 || rect.h <= 0)
continue;
List xl = {0,8,NULL};
List yl = {0,8,NULL};
xl.data = malloc(8*sizeof(float));
yl.data = malloc(8*sizeof(float));
for (int i=0;i<matches.size;i++) {
if (m[i].x1 > rect.x && m[i].x1 < rect.x + rect.w &&
m[i].y1 > rect.y && m[i].y1 < rect.y + rect.h) {
drawKeyPoint(rightscreen,m[i].x2,m[i].y2,red);
if (xl.size == xl.capacity) {
xl.data = realloc(xl.data,xl.capacity*2*sizeof(float));
xl.capacity *= 2;
yl.data = realloc(yl.data,yl.capacity*2*sizeof(float));
yl.capacity *= 2;
}
((float*)xl.data)[xl.size++] = m[i].x2;
((float*)yl.data)[yl.size++] = m[i].y2;
}
}
qsort(xl.data,xl.size,sizeof(float),compare_float);
qsort(yl.data,yl.size,sizeof(float),compare_float);
float x_med = ((float*)xl.data)[xl.size/2];
float y_med = ((float*)yl.data)[yl.size/2];
fprintf(target_point,"%f %f %d ",x_med,y_med,xl.size);
free(xl.data);
free(yl.data);
}
fprintf(target_point,"\n");
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("./match_image target_point.txt target.pgm [0.pgm ..]\n");
return 1;
}
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("match_image",NULL);
char buffer[128];
target_point = fopen(argv[1],"w");
FILE* ppm = fopen(argv[2],"r");
if (!ppm) {
printf("%s not found\n",argv[2]);
return 1;
}
fgets(buffer,128,ppm); //P5 or P6
do {
fgets(buffer,128,ppm);
} while (buffer[0]=='#'); //remove comments
char *c = buffer;
width = strtol(c,&c,10);
height = strtol(c,&c,10);
fgets(buffer,128,ppm); //255
screen = SDL_SetVideoMode(width*2,height,24,SDL_SWSURFACE);
leftscreen = SDL_CreateRGBSurface(0,width,height,24,0xFF0000,0xFF00,0xFF,0);
rightscreen = SDL_CreateRGBSurface(0,width,height,24,0xFF0000,0xFF00,0xFF,0);
Image leftimage = {width,height,NULL};
Image rightimage = {width,height,NULL};
leftimage.data = malloc(width*height*3);
rightimage.data = malloc(width*height*3);
leftrect.x = 0; leftrect.y = 0; leftrect.w = width; leftrect.h = height;
rightrect.x = width; rightrect.y = 0; rightrect.w = width; rightrect.h = height;
cx = 0.5 * (width-1);
cy = 0.5 * (height-1);
fread(leftimage.data,1,width*height,ppm);
//populate gray level
for (int i=width*height;i>=0;i--) {
leftimage.data[i*3+2] = leftimage.data[i*3+1] = leftimage.data[i*3] = leftimage.data[i];
}
fclose(ppm);
Color yellow = {255,255,0};
List matches = {0,8,NULL};
List rectList = {0,8,NULL};
matches.data = malloc(8*sizeof(Match));
rectList.data = calloc(8,sizeof(SDL_Rect));
SDL_Rect* currentRect = rectList.data;
imgcpy(leftimage,leftscreen);
SDL_BlitSurface(leftscreen,NULL,screen,&leftrect);
showRightImage(targetIndex++,&matches,rightimage);
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
SDL_Flip(screen);
SDL_Event event;
while (true) {
while (SDL_PollEvent(&event)) {
switch(event.type){
case SDL_KEYDOWN:
switch( event.key.keysym.sym ){
case 'b':
if (showRightImage(targetIndex--,&matches,rightimage)) {
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
SDL_Flip(screen);
}
break;
case 'n':
if (showRightImage(targetIndex++,&matches,rightimage)) {
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
SDL_Flip(screen);
}
break;
case 'm':
findCorrespondence(matches,rectList);
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
SDL_Flip(screen);
break;
case 'v':
findCorrespondence(matches,rectList);
while (showRightImage(targetIndex++,&matches,rightimage)) {
findCorrespondence(matches,rectList);
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
SDL_Flip(screen);
}
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
mouseDrag = true;
previousX = event.button.x;
previousY = event.button.y;
rectList.size++;
}
break;
case SDL_MOUSEMOTION:
if (mouseDrag) {
imgcpy(leftimage,leftscreen);
*currentRect = getVarRect(previousX,previousY,event.motion.x,event.motion.y);
for (int i=0;i<rectList.size;i++)
drawRect(leftscreen,((SDL_Rect*)rectList.data)[i],yellow);
SDL_BlitSurface(leftscreen,NULL,screen,&leftrect);
SDL_Flip(screen);
}
break;
case SDL_MOUSEBUTTONUP:
imgcpy(rightimage,rightscreen);
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
if (mouseDrag) {
mouseDrag = false;
SDL_BlitSurface(rightscreen,NULL,screen,&rightrect);
currentRect++;
} else {
imgcpy(leftimage,leftscreen);
SDL_BlitSurface(leftscreen,NULL,screen,&leftrect);
currentRect->w = 0;
currentRect->h = 0;
}
SDL_Flip(screen);
break;
case SDL_QUIT:
exit(0);
break;
}
}
usleep(1000);
}
fclose(target_point);
free(leftimage.data);
free(rightimage.data);
}