-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathhesaff.cpp
180 lines (157 loc) · 5.4 KB
/
hesaff.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
/*
* Copyright (C) 2008-12 Michal Perdoch
* All rights reserved.
*
* This file is part of the HessianAffine detector and is made available under
* the terms of the BSD license (see the COPYING file).
*
*/
#include <iostream>
#include <fstream>
#include "pyramid.h"
#include "helpers.h"
#include "affine.h"
#include "siftdesc.h"
using namespace cv;
using namespace std;
struct HessianAffineParams
{
float threshold;
int max_iter;
float desc_factor;
int patch_size;
bool verbose;
HessianAffineParams()
{
threshold = 16.0f/3.0f;
max_iter = 16;
desc_factor = 3.0f*sqrt(3.0f);
patch_size = 41;
verbose = false;
}
};
int g_numberOfPoints = 0;
int g_numberOfAffinePoints = 0;
struct Keypoint
{
float x, y, s;
float a11,a12,a21,a22;
float response;
int type;
unsigned char desc[128];
};
struct AffineHessianDetector : public HessianDetector, AffineShape, HessianKeypointCallback, AffineShapeCallback
{
const Mat image;
SIFTDescriptor sift;
vector<Keypoint> keys;
public:
AffineHessianDetector(const Mat &image, const PyramidParams &par, const AffineShapeParams &ap, const SIFTDescriptorParams &sp) :
HessianDetector(par),
AffineShape(ap),
image(image),
sift(sp)
{
this->setHessianKeypointCallback(this);
this->setAffineShapeCallback(this);
}
void onHessianKeypointDetected(const Mat &blur, float x, float y, float s, float pixelDistance, int type, float response)
{
g_numberOfPoints++;
findAffineShape(blur, x, y, s, pixelDistance, type, response);
}
void onAffineShapeFound(
const Mat &blur, float x, float y, float s, float pixelDistance,
float a11, float a12,
float a21, float a22,
int type, float response, int iters)
{
// convert shape into a up is up frame
rectifyAffineTransformationUpIsUp(a11, a12, a21, a22);
// now sample the patch
if (!normalizeAffine(image, x, y, s, a11, a12, a21, a22))
{
// compute SIFT
sift.computeSiftDescriptor(this->patch);
// store the keypoint
keys.push_back(Keypoint());
Keypoint &k = keys.back();
k.x = x; k.y = y; k.s = s; k.a11 = a11; k.a12 = a12; k.a21 = a21; k.a22 = a22; k.response = response; k.type = type;
for (int i=0; i<128; i++)
k.desc[i] = (unsigned char)sift.vec[i];
// debugging stuff
if (0)
{
cout << "x: " << x << ", y: " << y
<< ", s: " << s << ", pd: " << pixelDistance
<< ", a11: " << a11 << ", a12: " << a12 << ", a21: " << a21 << ", a22: " << a22
<< ", t: " << type << ", r: " << response << endl;
for (size_t i=0; i<sift.vec.size(); i++)
cout << " " << sift.vec[i];
cout << endl;
}
g_numberOfAffinePoints++;
}
}
void exportKeypoints(ostream &out)
{
out << 128 << endl;
out << keys.size() << endl;
for (size_t i=0; i<keys.size(); i++)
{
Keypoint &k = keys[i];
float sc = AffineShape::par.mrSize * k.s;
Mat A = (Mat_<float>(2,2) << k.a11, k.a12, k.a21, k.a22);
SVD svd(A, SVD::FULL_UV);
float *d = (float *)svd.w.data;
d[0] = 1.0f/(d[0]*d[0]*sc*sc);
d[1] = 1.0f/(d[1]*d[1]*sc*sc);
A = svd.u * Mat::diag(svd.w) * svd.u.t();
out << k.x << " " << k.y << " " << A.at<float>(0,0) << " " << A.at<float>(0,1) << " " << A.at<float>(1,1);
for (size_t i=0; i<128; i++)
out << " " << int(k.desc[i]);
out << endl;
}
}
};
int main(int argc, char **argv)
{
if (argc>1)
{
Mat tmp = imread(argv[1]);
Mat image(tmp.rows, tmp.cols, CV_32FC1, Scalar(0));
float *out = image.ptr<float>(0);
unsigned char *in = tmp.ptr<unsigned char>(0);
for (size_t i=tmp.rows*tmp.cols; i > 0; i--)
{
*out = (float(in[0]) + in[1] + in[2])/3.0f;
out++;
in+=3;
}
HessianAffineParams par;
double t1 = 0;
{
// copy params
PyramidParams p;
p.threshold = par.threshold;
AffineShapeParams ap;
ap.maxIterations = par.max_iter;
ap.patchSize = par.patch_size;
ap.mrSize = par.desc_factor;
SIFTDescriptorParams sp;
sp.patchSize = par.patch_size;
AffineHessianDetector detector(image, p, ap, sp);
t1 = getTime(); g_numberOfPoints = 0;
detector.detectPyramidKeypoints(image);
cout << "Detected " << g_numberOfPoints << " keypoints and " << g_numberOfAffinePoints << " affine shapes in " << getTime()-t1 << " sec." << endl;
char suffix[] = ".hesaff.sift";
int len = strlen(argv[1])+strlen(suffix)+1;
char buf[len];
snprintf(buf, len, "%s%s", argv[1], suffix); buf[len-1]=0;
ofstream out(buf);
detector.exportKeypoints(out);
}
} else {
printf("\nUsage: hesaff image_name.ppm\nDetects Hessian Affine points and describes them using SIFT descriptor.\nThe detector assumes that the vertical orientation is preserved.\n\n");
}
}