forked from liuliu/ccv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siftmatch.c
81 lines (78 loc) · 2.1 KB
/
siftmatch.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
#include "ccv.h"
#include <sys/time.h>
unsigned int get_current_time()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main(int argc, char** argv)
{
assert(argc == 3);
ccv_enable_default_cache();
ccv_dense_matrix_t* object = 0;
ccv_dense_matrix_t* image = 0;
ccv_read(argv[1], &object, CCV_IO_GRAY | CCV_IO_ANY_FILE);
ccv_read(argv[2], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE);
unsigned int elapsed_time = get_current_time();
ccv_sift_param_t params = {
.noctaves = 3,
.nlevels = 6,
.up2x = 1,
.edge_threshold = 10,
.norm_threshold = 0,
.peak_threshold = 0,
};
ccv_array_t* obj_keypoints = 0;
ccv_dense_matrix_t* obj_desc = 0;
ccv_sift(object, &obj_keypoints, &obj_desc, 0, params);
ccv_array_t* image_keypoints = 0;
ccv_dense_matrix_t* image_desc = 0;
ccv_sift(image, &image_keypoints, &image_desc, 0, params);
elapsed_time = get_current_time() - elapsed_time;
int i, j, k;
int match = 0;
for (i = 0; i < obj_keypoints->rnum; i++)
{
float* odesc = obj_desc->data.f32 + i * 128;
int minj = -1;
double mind = 1e6, mind2 = 1e6;
for (j = 0; j < image_keypoints->rnum; j++)
{
float* idesc = image_desc->data.f32 + j * 128;
double d = 0;
for (k = 0; k < 128; k++)
{
d += (odesc[k] - idesc[k]) * (odesc[k] - idesc[k]);
if (d > mind2)
break;
}
if (d < mind)
{
mind2 = mind;
mind = d;
minj = j;
} else if (d < mind2) {
mind2 = d;
}
}
if (mind < mind2 * 0.36)
{
ccv_keypoint_t* op = (ccv_keypoint_t*)ccv_array_get(obj_keypoints, i);
ccv_keypoint_t* kp = (ccv_keypoint_t*)ccv_array_get(image_keypoints, minj);
printf("%f %f => %f %f\n", op->x, op->y, kp->x, kp->y);
match++;
}
}
printf("%dx%d on %dx%d\n", object->cols, object->rows, image->cols, image->rows);
printf("%d keypoints out of %d are matched\n", match, obj_keypoints->rnum);
printf("elpased time : %d\n", elapsed_time);
ccv_array_free(obj_keypoints);
ccv_array_free(image_keypoints);
ccv_matrix_free(obj_desc);
ccv_matrix_free(image_desc);
ccv_matrix_free(object);
ccv_matrix_free(image);
ccv_disable_cache();
return 0;
}