-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
283 lines (227 loc) · 9.53 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <opencv2/opencv.hpp>
#include <fstream>
#include <assert.h>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <experimental/filesystem>
#include <climits>
#include <list>
#include "GraphSegmentation/lib/graph_segmentation.h"
using namespace std;
using namespace cv;
using namespace experimental::filesystem;
// Gets all files path that are in a given dirictory
vector<string> getAllImagesInDirectory(string path) {
vector<string> imagesPath;
cout << path << endl;
for (const auto & entry : directory_iterator(path)) {
imagesPath.push_back(entry.path().string());
}
return imagesPath;
}
// Normalizes a vector of doubles
vector<double> normalizeData(vector<int> data) {
int max = INT_MIN;
int min = INT_MAX;
for (int i = 0; i < data.size(); i++) {
if (data[i] >= max) max = data[i];
if (data[i] < min) min = data[i];
}
vector<double> normalizedVector;
for (int i = 0; i < data.size(); i++) {
double normalized_value = (double)(data[i] - min) / (max - min);
normalizedVector.push_back(normalized_value);
}
return normalizedVector;
}
// Normalizes a vector of doubles
vector<double> normalizeData(vector<double> data) {
double max = INT_MIN;
double min = INT_MAX;
for (int i = 0; i < data.size(); i++) {
if (data[i] >= max) max = data[i];
if (data[i] < min) min = data[i];
}
vector<double> normalizedVector;
for (int i = 0; i < data.size(); i++) {
double normalized_value = (double)(data[i] - min) / (max - min);
normalizedVector.push_back(normalized_value);
}
return normalizedVector;
}
// Computes area
int computeArea(int w, int h) {
return w * h;
}
// Computes area for each hypothesis
vector<int> computeAllHypothesisCovagere(vector<vector<int>> hypothesis) {
vector<int> coverageHypothesis;
for (int i = 0; i < hypothesis[0].size(); i++) {
int height_hypothesis = abs(hypothesis[0][i] - hypothesis[1][i]);
int width_hypothesis = abs(hypothesis[2][i] - hypothesis[3][i]);
int coverage = computeArea(width_hypothesis, height_hypothesis);
coverageHypothesis.push_back(coverage);
}
return coverageHypothesis;
}
// Computes Local Refinement of all hypothesis
vector<int> localHypothesisRefinement(vector<int> area_hypothesis, vector<int> area_segments, double threshold1 = 0.5, double threshold2 = 0.5) {
vector<double> ratios;
// Computing the ratio between the segment area and the hipothesis area
for (int i = 0; i < area_segments.size(); i++) {
double ratio = (double)area_segments[i] / area_hypothesis[i];
ratios.push_back(ratio);
}
// Normalize everything that needs to be normalized
vector<double> ratios_normalized = normalizeData(ratios);
vector<double> area_hypothesis_normalized = normalizeData(area_hypothesis);
vector<double> area_segments_normalized = normalizeData(area_segments);
// Computing the mean between ratio, segment area and hipothesis area
vector<double> scores;
for (int j = 0; j < ratios_normalized.size(); j++) {
double score = (area_hypothesis_normalized[j] + ratios_normalized[j] + area_segments_normalized[j]) / 3;
scores.push_back(score);
}
// Creating a vector of indexes of all hipothesis after refinement
vector<int> refined_indexes;
for (int i = 0; i < scores.size(); i++) {
if (scores[i] > threshold1 && ratios_normalized[i] > threshold2 && area_hypothesis_normalized[i] > 0.03 && area_hypothesis_normalized[i] < 0.1) {
refined_indexes.push_back(i);
}
}
return refined_indexes;
}
// Checks whether or not two given bounding boxes are intersecting each other
bool checkOverlappaing(int top1, int bottom1, int left1, int right1, int top2, int bottom2, int left2, int right2, int tolerance = 5000) {
Rect bb1(top1, left1, right1 - left1, bottom1 - top1); // Bounding box 1
Rect bb2(top2, left2, right2 - left2, bottom2 - top2); // Bounding box 2
bool intersects = ((bb1 & bb2).area() > tolerance);
return intersects;
}
// Computes Global Refinement of all hypothesis
vector<int> globalHypothesisRefinement(vector<vector<int>> hypothesis, vector<int> local_refined_hypothesis_indexes, vector<int> area_hypothesis, vector<int> area_segments) {
vector<int> indexes_to_be_removed;
// Finding all hipothesis that need to be removed
for (int i = 0; i < local_refined_hypothesis_indexes.size(); i++) {
int index_i = local_refined_hypothesis_indexes[i];
for (int j = 1; j < local_refined_hypothesis_indexes.size(); j++) {
int index_j = local_refined_hypothesis_indexes[j];
if (index_i == index_j) continue;
// If there is an overlapping
if (checkOverlappaing(hypothesis[0][index_i], hypothesis[1][index_i], hypothesis[2][index_i], hypothesis[3][index_i], hypothesis[0][index_j], hypothesis[1][index_j], hypothesis[2][index_j], hypothesis[3][index_j])) {
// Save the largest hipothesis to be removed
if (area_hypothesis[index_i] > area_hypothesis[index_j]) {
indexes_to_be_removed.push_back(index_j);
}else {
indexes_to_be_removed.push_back(index_i);
}
}
}
}
// Removing all hipothesis that are set to be removed
vector<int> global_refined_hypothesis_indexes;
for (int i = 0; i < local_refined_hypothesis_indexes.size(); i++) {
int index = local_refined_hypothesis_indexes[i];
bool isIndexToBeRemoved = find(indexes_to_be_removed.begin(), indexes_to_be_removed.end(), index) != indexes_to_be_removed.end();
if (!isIndexToBeRemoved) {
global_refined_hypothesis_indexes.push_back(local_refined_hypothesis_indexes[i]);
}
}
return global_refined_hypothesis_indexes;
}
// Draws and displays a bounding box
void drawingHypothesis(Mat img, int top, int bottom, int left, int right, int line = -1) {
Point pt1(left, bottom);
Point pt2(right, top);
rectangle(img, pt1, pt2, Scalar(0, 255, 0), line);
namedWindow("Hipoteses", WINDOW_AUTOSIZE);
imshow("Hipoteses", img);
waitKey(0);
}
// Draws and displays a bounding box
void drawingAllHypothesis(Mat img, vector<int> indexes, vector<int> tops, vector<int> bottoms, vector<int> lefts, vector<int> rights, int line = -1, bool showAll = false) {
Mat img1 = img.clone();
for (int i = 0; i < indexes.size(); i++) {
int index = indexes[i];
int top = tops[index];
int bottom = bottoms[index];
int left = lefts[index];
int right = rights[index];
Point pt1(left, bottom);
Point pt2(right, top);
rectangle(img, pt1, pt2, Scalar(0, 255, 0), line);
}
namedWindow("Hipoteses", WINDOW_AUTOSIZE);
imshow("Hipoteses", img);
if (showAll) {
for (int i = 0; i < tops.size(); i++) {
int top = tops[i];
int bottom = bottoms[i];
int left = lefts[i];
int right = rights[i];
Point pt1(left, bottom);
Point pt2(right, top);
rectangle(img1, pt1, pt2, Scalar(0, 255, 0), line);
}
namedWindow("Hipoteses2", WINDOW_AUTOSIZE);
imshow("Hipoteses2", img1);
}
waitKey(0);
}
// Draws and displays a bounding box
void saveHypothesis(Mat img, string path, vector<int> indexes, vector<int> tops, vector<int> bottoms, vector<int> lefts, vector<int> rights) {
Mat img_bw = Mat(img.rows, img.cols, CV_64F, cvScalar(0.));
for (int i = 0; i < indexes.size(); i++) {
int index = indexes[i];
int top = tops[index];
int bottom = bottoms[index];
int left = lefts[index];
int right = rights[index];
Point pt1(left, bottom);
Point pt2(right, top);
rectangle(img_bw, pt1, pt2, Scalar(255, 255, 255), FILLED);
}
imwrite(path, img_bw);
}
int main() {
string africanos_path = "dataset_satelite/africanos/";
string nhozinho_path = "dataset_satelite/nhozinho/";
vector<string> imagesPaths = getAllImagesInDirectory(africanos_path);
float sigma = 0.3;
int k = 100;
int min_size = 1000;
for (int i = 0; i < imagesPaths.size(); i++) {
cout << i + 1 << "/" << imagesPaths.size() << endl;
string imagePath = imagesPaths.at(i);
Mat src = imread(imagePath);
if (!src.data) return -1;
GraphSegmentation segmenter;
vector<vector<int>> hypothesis = segmenter.executeGraphSegmentation(imagePath, sigma, k, min_size);
vector<int> area_hypothesis = computeAllHypothesisCovagere(hypothesis);
//cout << "Computing Local Hypothesis Refinement..." << endl;
// Refinamento local das hipoteses geradas
vector<int> hypothesis_locally_refined_indexes = localHypothesisRefinement(area_hypothesis, hypothesis[4], 0.2, 0.6);
/*for (int j = 0; j < hypothesis_locally_refined_indexes.size(); j++) {
int index = hypothesis_locally_refined_indexes[j];
cout << "Index (" << index << ") -->" << area_hypothesis[index] << endl;
drawingHypothesis(src.clone(), hypothesis[0][index], hypothesis[1][index], hypothesis[2][index], hypothesis[3][index]);
}*/
//cout << "Local Hypothesis Refinement has finished!" << endl;
string imageName = imagePath.substr(imagePath.find_last_of("/\\") + 1);
//cout << "Computing Global Hypothesis Refinement..." << endl;
// Refinamento global das hipoteses geradas
vector<int> hypothesis_globaly_refined_indexes = globalHypothesisRefinement(hypothesis, hypothesis_locally_refined_indexes, area_hypothesis, hypothesis[4]);
cout << "Hipoteses Detectadas(" << i + 1 << "): " << hypothesis_globaly_refined_indexes.size() << " / " << hypothesis[0].size() << endl;
saveHypothesis(src.clone(), "./Output/" + imageName, hypothesis_globaly_refined_indexes, hypothesis[0], hypothesis[1], hypothesis[2], hypothesis[3]);
/*for (int j = 0; j < hypothesis_globaly_refined_indexes.size(); j++) {
int index = hypothesis_globaly_refined_indexes[j];
cout << "Index (" << index << ") -->" << area_hypothesis[index] << endl;
drawingHypothesis(src.clone(), hypothesis[0][index], hypothesis[1][index], hypothesis[2][index], hypothesis[3][index], -1);
}*/
//cout << "Global Hypothesis Refinement has finished!" << endl;
}
system("pause");
return 0;
}