-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodometryFeaturesPrQueue.cpp
More file actions
187 lines (169 loc) · 7.24 KB
/
odometryFeaturesPrQueue.cpp
File metadata and controls
187 lines (169 loc) · 7.24 KB
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
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <vector>
using namespace std;
using namespace cv;
const int MAX_INT = 1<<30;
bool NonMaxSupression(Mat m, int x, int y, int nonMaxRadius){
for(int i = -nonMaxRadius; i <= nonMaxRadius; i++)
for(int j = -nonMaxRadius; j <= nonMaxRadius; j++){
int nx = x + i;
int ny = y + j;
if (nx < 0 || ny < 0 || nx >= m.rows || ny >= m.cols || (nx == x && ny == y))
continue;
if (m.at<float>(nx,ny) > m.at<float>(x,y))
return false;
}
return true;
}
vector<pair<int,int> > GenFeature(Mat img,int blockSize = 2, int apertureSize = 3, double k = 0.04, int nonMaxRadius = 8, int upperLimitNormalization = 5000){
// se asume que img esta en escala de grises
Mat dst = Mat::zeros( img.size(), CV_32FC1 );
Mat dst_norm, dst_norm_scaled;
cornerHarris( img, dst, blockSize, apertureSize, k, BORDER_DEFAULT );
normalize( dst, dst_norm, 0, upperLimitNormalization, NORM_MINMAX, CV_32FC1, Mat() );
convertScaleAbs( img, dst_norm_scaled );
//seleccionando el Threshold adaptativamente
int histogram[upperLimitNormalization + 1];
for(int i = 0; i < upperLimitNormalization + 1; i++)
histogram[i] = 0;
for( int i = 0; i < dst_norm.rows ; i++ )
for( int j = 0; j < dst_norm.cols; j++ )
histogram[(int)dst_norm.at<float>(i,j)]++;
int cumulativesum = 0;
int total = dst_norm.rows * dst_norm.cols;
int aim = 7*(total / 10); // Este parametro es fundamental para ajustar el numero de caracteristicas obtenidas al final.
int thresh = upperLimitNormalization / 3;
for(int i = 0; i < upperLimitNormalization; i++){
cumulativesum += histogram[i];
if ((total - cumulativesum) <= aim){
thresh = i + 1;
break;
}
}
vector<pair<int,int> > features;
for( int i = 0; i < dst_norm.rows ; i++ ){
for( int j = 0; j < dst_norm.cols; j++ ){
if ( NonMaxSupression(dst_norm, i, j, nonMaxRadius) && ( ((int)dst_norm.at<float>(i,j)) >= thresh) ) {
features.push_back(make_pair(i,j));
circle( dst_norm_scaled, Point( j, i ), 5, Scalar(0), 2, 8, 0 ); //
}
}
}
// db = dst_norm_scaled;
return features;
}
/*Matching*/
int SumofAbsoluteDifferences(Mat img1, Mat img2, pair<int,int> f1, pair<int,int> f2, int w = 10){
if ( (img1.rows != img2.rows ) || (img1.cols != img2.cols)){
printf("Las dimensiones de las imagenes no coinciden");
return 0;
}
int limitx = img1.rows;
int limity = img1.cols;
int response = 0;
for(int i = -w; i <= w; i++){
for(int j = -w; j <= w; j++){
int nx1 = f1.first + i;
int ny1 = f1.second + j;
int nx2 = f2.first + i;
int ny2 = f2.second + j;
bool c1 = (nx1 < 0 || ny1 < 0 || nx2 < 0 || ny2 < 0);
bool c2 = (nx1 >= limitx || ny1 >= limity || nx2 >= limitx || ny2 >= limity);
if (c1 || c2) continue;
response += abs(img1.at<int>(nx1,ny1) - img2.at<int>(nx2,ny2));
}
}
return response;
}
void DeterminingFavorites(Mat img1, Mat img2, vector< pair<int,int> > &f1, vector< pair<int,int> > &f2, int *favorites, int delta = 15){
for(int i = 0; i < f1.size(); i++){
pair<int, int> current1 = f1[i];
int menor = MAX_INT;
int idMenor = -1;
for(int j = 0; j < f2.size(); j++){
pair<int, int> current2 = f2[j];
int dx = abs(current1.first - current2.first);
int dy = abs(current1.second - current2.second);
if(dx > delta || dy > delta) continue;
int similarity = SumofAbsoluteDifferences(img1, img2, current1, current2);
if(similarity < menor){
menor = similarity;
idMenor = j;
}
}
favorites[i] = idMenor;
}
return;
}
vector<pair<int,int> > harrisFeatureMatcherMCC(Mat img1, Mat img2, vector< pair<int,int> > featuresImg1, vector< pair<int,int> > featuresImg2){
int favoritesfromimg1[featuresImg1.size()]; // en la posicion i se guarda el favorito de la característica i de la imagen 1.
int favoritesfromimg2[featuresImg2.size()]; // en la posicion i se guarda el favorito de la característica i de la imagen 2.
vector< pair<int,int> > correspondences;
DeterminingFavorites(img1, img2, featuresImg1, featuresImg2, favoritesfromimg1);
DeterminingFavorites(img2, img1, featuresImg2, featuresImg1, favoritesfromimg2);
for(int i = 0; i < featuresImg1.size(); ++i){
if(favoritesfromimg2[favoritesfromimg1[i]] == i) correspondences.push_back(make_pair(i, favoritesfromimg1[i]));
}
return correspondences;
}
/*end Matching*/
void debugging(Mat img1, Mat img2, vector<pair<int,int> > &fts1, vector<pair<int,int> > &fts2, vector< pair<int,int> > correspondences){
Mat new_image;
new_image.create(img1.rows *2, img1.cols, img1.type());
for(int i = 0; i < img1.rows; i++){
for(int j = 0; j < img1.cols; j++)
new_image.at<int>(i,j) = img2.at<int>(i, j);
}
for(int i = img1.rows; i < img1.rows*2; i++){
for(int j = 0; j < img1.cols; j++)
new_image.at<int>(i, j) = img1.at<int>(i - img2.rows, j);
}
namedWindow("correspondences",1);
int buenas = 0;
for(int i = 0; i < correspondences.size(); i++){
pair<int,int> feature1 = fts1[correspondences[i].first];
pair<int,int> feature2 = fts2[correspondences[i].second];
//Point p1(feature1.second, feature1.first);
Point p1(feature1.second, feature1.first);
Point p2(feature2.second, feature2.first + img1.rows);
if(feature1.second == feature2.second) buenas += 1;
Scalar color(25.0);
circle(new_image, p1, 5, color, 2);
circle(new_image, p2, 5, color, 2);
line(new_image, p1, p2, color);
imshow("correspondences", new_image);
waitKey(0);
}
cout<<"buenas: "<<buenas<<" malas: "<<correspondences.size() - buenas<<endl;
return;
}
int main(int argc, char** argv){
Mat img1, img2, imgray1, imgray2;
if( argc != 3){
cout <<"Pasar el nombre de la imagen" << endl;
return -1;
}
namedWindow("ventana",1);//
img1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
img2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);
cvtColor( img1, imgray1, CV_BGR2GRAY );
cvtColor( img2, imgray2, CV_BGR2GRAY );
vector<pair<int,int> > fts1 = GenFeature(imgray1);
vector<pair<int,int> > fts2 = GenFeature(imgray2);
cout<<"cantidad características imagen 1: "<<fts1.size()<<" cantidad características imagen 2: "<<fts2.size()<<endl;
//namedWindow("edges",1);
//imshow("edges", db);
//cout<<fts.size()<<endl;
//waitKey(0);
//for(int i = 0; i < fts.size(); ++i) cout<< fts[i] << end;
vector< pair<int,int> > correspondences = harrisFeatureMatcherMCC(imgray1, imgray2, fts1, fts2);
debugging(imgray1, imgray2, fts1, fts2, correspondences);
//cout<<correspondences.size()<<endl;
return 0;
}