-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbproject.cpp
More file actions
202 lines (157 loc) · 5.49 KB
/
bproject.cpp
File metadata and controls
202 lines (157 loc) · 5.49 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
PROJETO FINAL B
SCC0251 PDI PROFESSOR : MOACIR P.
Leonardo Fachetti #USP 6878870
Anna Paula P. Maule #USP 4624650
**/
#include <string>
#include <stdio.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace std;
using namespace cv;
/*
Comparação para a função de ordenação sort
*/
bool mySort(pair<Point2f, double> a, pair<Point2f, double> b) {
return a.second > b.second;
}
/*
Encontra o ponto central dado um conunto de pontos.
Na segunda iteração exclui os pontos de maior distancia desse ponto central previamente calculado e o re-calcula
Entrada
Vetor de pontos
Numero de iterações
Saida
Ponto central
*/
Point2f average(vector<Point2f> v, int it) {
Point2f av;
vector< pair<Point2f, double> > points;
while(it--) {
if(!points.empty()) {
v.clear();
for (int i = points.size()*0.4; i < (signed) points.size(); i++) {
v.push_back(Point2f(points[i].first.x, points[i].first.y));
}
} else {
points.clear();
}
int sumX = 0, sumY = 0;
// Calcula o valor médio de x e y
for(int i = 0 ; i < (signed) v.size(); i++) {
sumX +=v[i].x;
sumY +=v[i].y;
}
av.x = sumX/v.size();
av.y = sumY/v.size();
if(!it) {
break;
}
for (int i = 0; i < (signed) v.size(); i++) {
points.push_back(make_pair(v[i], sqrt(pow(abs(v[i].x - av.x), 2) + pow(abs(v[i].y - av.y), 2))));
}
sort(points.begin(), points.end(), mySort);
}
return av;
}
void readme() {
cout << "Entrada invalida. \n\tUso: ./PROG_NAME <OBJ_BUSCADO_1> <OBJ_BUSCADO_2> <PASTA_COM_AS_CENAS> <NUM_IMAGENS_NA_PASTA>" << endl;
}
int main(int argc, char** argv){
Mat output, rec, descriptor, descriptor1, descriptor1_2, descriptor2;
FlannBasedMatcher matcher;
vector<vector<DMatch > > matches;
vector<DMatch > good_matches;
vector<Point2f> obj, scene;
if(argc != 5) {
readme();
return -1;
}
Mat img1 = imread(argv[1], 1), img1_2 = imread(argv[2], 1), img2;
// Borra as imagem
GaussianBlur(img1, img1, Size(3,3), 0, 0, BORDER_DEFAULT);
GaussianBlur(img1_2, img1_2, Size(3,3), 0, 0, BORDER_DEFAULT);
vector<Mat> channels;
// Separa os canais HSV para a imagem 1
split(img1, channels);
// Usa o canal H
img1 = channels[0].clone();
split(img1_2, channels);
// Usa o canal H
img1_2 = channels[0].clone();
int minHessian = 400;
vector<KeyPoint> keypoints1, keypoints1_2, keypoints2;
SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(minHessian);
//Detecta os keypoints da primeira imagem usando SURF
detector.detect(img1, keypoints1);
//Calcula o descritor pra primeira imagem
extractor.compute(img1, keypoints1, descriptor1);
//Detecta os keypoints da primeira imagem usando SURF
detector.detect(img1_2, keypoints1_2);
//Calcula o descritor pra primeira imagem
extractor.compute(img1, keypoints1_2, descriptor1_2);
for (int img = 1; img <= atoi(argv[4]); img++) {
string sceneName = argv[3];
sceneName += "/test";
if(img < 10)
sceneName += "0";
sceneName += to_string(img) + ".jpg";
img2 = imread(sceneName, 1);
if (img1.empty() || img1_2.empty() || img2.empty()) {
printf("Can't read one of the images 1\n");
return -1;
}
rec = img2.clone();
// Borra a imagem
GaussianBlur(img2, img2, Size(3,3), 0, 0, BORDER_DEFAULT);
// Separa os canais HSV para a imagem 2
split(img2, channels);
// Usa o canal H
img2 = channels[0].clone();
//Detecta os keypoints da segunda imagem usando SURF
detector.detect(img2, keypoints2);
//Calcula o descritor pra segunda imagem
extractor.compute(img2, keypoints2, descriptor2);
matcher.knnMatch(descriptor1, descriptor2, matches, 2);
for (int i = 0; i < min(descriptor2.rows - 1, (int)matches.size()); i++) {
if ((matches[i][0].distance < 0.6*(matches[i][4].distance)) && ((int)matches[i].size() <= 2 && (int)matches[i].size()>0)) {
good_matches.push_back(matches[i][0]);
}
}
matcher.knnMatch(descriptor1_2, descriptor2, matches, 2);
for (int i = 0; i < min(descriptor2.rows - 1, (int)matches.size()); i++) {
if ((matches[i][0].distance < 0.6*(matches[i][4].distance)) && ((int)matches[i].size() <= 2 && (int)matches[i].size()>0)) {
good_matches.push_back(matches[i][0]);
}
}
//Draw only "good" matches
drawMatches(img1, keypoints1, img2, keypoints2, good_matches, output, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
if (good_matches.size() >= 4) {
for (int i = 0; i < (signed) good_matches.size(); i++) {
//Get the keypoints from the good matches
obj.push_back(keypoints1[good_matches[i].queryIdx].pt);
scene.push_back(keypoints2[good_matches[i].trainIdx].pt);
}
}
Point2f av = average(scene, 3); // Calcula o ponto medio
// Desenha um retangulo em torno do ponto medio
rectangle(rec, Point2f(av.x - 150, av.y - 150), Point2f(av.x + 150, av.y + 150), Scalar(0,255,0), 3);
// namedWindow("KeyPoints", CV_WINDOW_NORMAL); // Cria uma janela para mostrar a imagem
// imshow("KeyPoints", output); // Mostra as duas imagens e seus keypoints
// waitKey();
namedWindow("Find Image", CV_WINDOW_NORMAL); // Cria uma janela para mostrar a imagem
imshow("Find Image", rec); // Mostra a imagem com o retangulo sobre o objeto
waitKey();
good_matches.clear();
obj.clear();
scene.clear();
}
return 0;
}