-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.c
167 lines (136 loc) · 4.76 KB
/
match.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
/************************************************************************
Demo software: Invariant keypoint matching.
Author: David Lowe
match.c:
This file contains a sample program to read images and keypoints, then
draw lines connecting matched keypoints.
*************************************************************************/
#include "defs.h"
/* -------------------- Local function prototypes ------------------------ */
void FindMatches(Image im1, Keypoint keys1, Image im2, Keypoint keys2);
Keypoint CheckForMatch(Keypoint key, Keypoint klist);
int DistSquared(Keypoint k1, Keypoint k2);
Image CombineImagesVertically(Image im1, Image im2);
/*----------------------------- Routines ----------------------------------*/
/* Top level routine. Read PGM images and keypoints from files given
in command line arguments, then call FindMatches.
*/
int main (int argc, char **argv)
{
int arg = 0;
Image im1 = NULL, im2 = NULL;
Keypoint k1 = NULL, k2 = NULL;
/* Parse command line arguments and read given files. The command
line must specify two input images and two files of keypoints
using command line arguments as follows:
match -im1 i1.pgm -k1 k1.key -im2 i2.pgm -k2 k2.key > result.v
*/
while (++arg < argc) {
if (! strcmp(argv[arg], "-im1"))
im1 = ReadPGMFile(argv[++arg]);
else if (! strcmp(argv[arg], "-im2"))
im2 = ReadPGMFile(argv[++arg]);
else if (! strcmp(argv[arg], "-k1"))
k1 = ReadKeyFile(argv[++arg]);
else if (! strcmp(argv[arg], "-k2"))
k2 = ReadKeyFile(argv[++arg]);
else
FatalError("Invalid command line argument: %s", argv[arg]);
}
if (im1 == NULL || im2 == NULL || k1 == NULL || k2 == NULL)
FatalError("Command line does not specify all images and keys.");
FindMatches(im1, k1, im2, k2);
exit(0);
}
/* Given a pair of images and their keypoints, pick the first keypoint
from one image and find its closest match in the second set of
keypoints. Then write the result to a file.
*/
void FindMatches(Image im1, Keypoint keys1, Image im2, Keypoint keys2)
{
Keypoint k, match;
Image result;
int count = 0;
/* Create a new image that joins the two images vertically. */
result = CombineImagesVertically(im1, im2);
/* Match the keys in list keys1 to their best matches in keys2.
*/
for (k= keys1; k != NULL; k = k->next) {
match = CheckForMatch(k, keys2);
/* Draw a line on the image from keys1 to match. Note that we
must add row count of first image to row position in second so
that line ends at correct location in second image.
*/
if (match != NULL) {
count++;
DrawLine(result, (int) k->row, (int) k->col,
(int) (match->row + im1->rows), (int) match->col);
}
}
/* Write result image to standard output. */
WritePGM(stdout, result);
fprintf(stderr,"Found %d matches.\n", count);
}
/* This searches through the keypoints in klist for the two closest
matches to key. If the closest is less than 0.6 times distance to
second closest, then return the closest match. Otherwise, return
NULL.
*/
Keypoint CheckForMatch(Keypoint key, Keypoint klist)
{
int dsq, distsq1 = 100000000, distsq2 = 100000000;
Keypoint k, minkey = NULL;
/* Find the two closest matches, and put their squared distances in
distsq1 and distsq2.
*/
for (k = klist; k != NULL; k = k->next) {
dsq = DistSquared(key, k);
if (dsq < distsq1) {
distsq2 = distsq1;
distsq1 = dsq;
minkey = k;
} else if (dsq < distsq2) {
distsq2 = dsq;
}
}
/* Check whether closest distance is less than 0.6 of second. */
if (10 * 10 * distsq1 < 6 * 6 * distsq2)
return minkey;
else return NULL;
}
/* Return squared distance between two keypoint descriptors.
*/
int DistSquared(Keypoint k1, Keypoint k2)
{
int i, dif, distsq = 0;
unsigned char *pk1, *pk2;
pk1 = k1->descrip;
pk2 = k2->descrip;
for (i = 0; i < 128; i++) {
dif = (int) *pk1++ - (int) *pk2++;
distsq += dif * dif;
}
return distsq;
}
/* Return a new image that contains the two images with im1 above im2.
*/
Image CombineImagesVertically(Image im1, Image im2)
{
int rows, cols, r, c;
Image result;
rows = im1->rows + im2->rows;
cols = MAX(im1->cols, im2->cols);
result = CreateImage(rows, cols);
/* Set all pixels to 0,5, so that blank regions are grey. */
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
result->pixels[r][c] = 0.5;
/* Copy images into result. */
for (r = 0; r < im1->rows; r++)
for (c = 0; c < im1->cols; c++)
result->pixels[r][c] = im1->pixels[r][c];
for (r = 0; r < im2->rows; r++)
for (c = 0; c < im2->cols; c++)
result->pixels[r + im1->rows][c] = im2->pixels[r][c];
return result;
}