forked from cristi-zz/pi_2020_vc_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenCVApplication.cpp
332 lines (254 loc) · 10.4 KB
/
OpenCVApplication.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "stdafx.h"
#include "common.h"
#include "algorithms.h"
#include "custom_glcm.h"
#include "OpenCVApplication.h"
#include "file_tester.h"
#include <random>
#include <functional>
std::random_device dev;
std::mt19937 rng(dev());
// Function for showing the histogram in a visual form
void tiger_detection::showHistogram(const std::string& name, std::vector<int> hist, const int hist_cols, const int hist_height) {
Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
//computes histogram maximum
int max_hist = 0;
for (int i = 0; i < hist_cols; i++)
if (hist[i] > max_hist)
max_hist = hist[i];
double scale = 1.0;
scale = (double)hist_height / max_hist;
int baseline = hist_height - 1;
for (int x = 0; x < hist_cols; x++) {
Point p1 = Point(x, baseline);
Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
}
imshow(name, imgHist);
}
// Function for creating patches of a given size, from a given Mat src
std::vector<std::tuple<cv::Rect, cv::Mat, std::vector<double>>> tiger_detection::createPatches(Mat src, Mat src_hue, int patchSize) {
int height = src.rows;
int width = src.cols;
std::vector<std::tuple<cv::Rect, cv::Mat, std::vector<double>>> patches;
cv::Size patch_size(patchSize, patchSize); // Specify the patch size
for (int i = 0; i < src.rows; i += patch_size.height) {
for (int j = 0; j < src.cols; j += patch_size.width) {
cv::Rect patch_rect(j, i, min(patch_size.width, width - j - 1), min(patch_size.height, height - i - 1));
cv::Mat patch = src(patch_rect);
std::vector<double> features = custom_glcm::getFeatures(patch);
std::vector<int> histoPatch = algorithms::binnedHistogram(src_hue(patch_rect), patchSize);
std::vector<int> histoPatchNorm;
for (int i = 0; i < histoPatch.size(); i++) {
histoPatchNorm.push_back(histoPatch.at(i) / (patchSize * patchSize));
}
features.insert(features.end(), histoPatch.begin(), histoPatch.end());
patches.push_back(std::make_tuple(patch_rect, patch, features));
}
}
return patches;
}
// Function for showing the clusters of an image in a visual form
void tiger_detection::showClusters(int iterations, int Kclusters, int patchSize, double(*heuristicFunc)(algorithms::Point p, algorithms::Point other)) {
char fname[MAX_PATH];
while (openFileDlg(fname))
{
Mat_<Vec3b> src_color = imread(fname);
Mat_<Vec3b> dst = tiger_detection::computeTigerImageClusters(fname, iterations, Kclusters, patchSize, heuristicFunc);
imshow("original image", src_color);
imshow("clustered image", dst);
waitKey();
}
}
// Function for showing the binned histogram in a visual form
void tiger_detection::showBinnedHistogram(int numberOfBins) {
char fname[MAX_PATH];
while (openFileDlg(fname)) {
Mat_<uchar> src = imread(fname, IMREAD_GRAYSCALE);
int x = 0, y = 0, height = 8, width = 8;
std::cout << "Image height: " << src.rows << std::endl;
std::cout << "Image width: " << src.cols << std::endl;
std::cout << "Patch x:\n";
std::cin >> x;
std::cout << "Patch y:\n";
std::cin >> y;
std::cout << "Patch height:\n";
std::cin >> height;
std::cout << "Patch width:\n";
std::cin >> width;
cv::Rect patch_rect(max(y, 0), max(x, 0), min(width, src.cols - y - 1), min(height, src.rows - x - 1));
std::vector<int> hist = algorithms::binnedHistogram(src(patch_rect), numberOfBins);
std::vector<int> showedHist;
for (int h : hist) {
for (int i = 0; i < 256 / hist.size(); i++) {
showedHist.push_back(h);
}
}
imshow("Original Image", src);
imshow("Image Patch", src(patch_rect));
tiger_detection::showHistogram("Binned Histogram for given patch", showedHist, showedHist.size(), 100);
waitKey();
}
}
// GLCM features info from a given image
void tiger_detection::showImageFeatures() {
char fname[MAX_PATH];
while (openFileDlg(fname)) {
double t = (double)getTickCount(); // Get the current time [s]
Mat_<uchar> src = imread(fname, IMREAD_GRAYSCALE);
for (const auto feature : custom_glcm::getFeatures(src)) {
std::cout << feature << std::endl;
}
waitKey();
}
}
// Image clustering funcion
cv::Mat_<Vec3b> tiger_detection::computeTigerImageClusters(const cv::String fname, int iterations, int Kclusters, int patchSize, double(*heuristicFunc)(algorithms::Point p, algorithms::Point other)) {
Mat_<uchar> src = imread(fname, IMREAD_GRAYSCALE);
Mat_<Vec3b> src_color = imread(fname);
Mat_<Vec3b> src_hsv = imread(fname);
std::vector<Mat> hsv_planes;
Mat_<uchar> src_hue;
cv::cvtColor(src_color, src_hsv, COLOR_BGR2HSV);
split(src_hsv, hsv_planes);
src_hue = hsv_planes[0]; // hue channel
int height = src.rows;
int width = src.cols;
if (patchSize > max(height, width) || patchSize <= 0) {
patchSize = max(height, width);
}
Mat_<Vec3b> dst(height, width);
dst.setTo(cv::Scalar(255, 255, 255));
std::vector<algorithms::Point> points;
std::vector<std::tuple<cv::Rect, cv::Mat, std::vector<double>>> patches = tiger_detection::createPatches(src, src_hue, patchSize);
for (const auto patch : patches) {
double centerX = (double)std::get<0>(patch).width / 2.0;
double centerY = (double)std::get<0>(patch).height / 2.0;
algorithms::Point point = algorithms::Point(centerX, centerY, std::get<0>(patch));
point.features = std::get<2>(patch);
points.push_back(point);
}
std::vector<algorithms::Point> centroids;
centroids = algorithms::kMeansClustering(&points, iterations, Kclusters, heuristicFunc);
int markSize = 20;
int markThickness = 2;
cv::Scalar markColor = cv::Scalar(0, 0, 0);
std::vector<int> clusterIds;
std::transform(points.begin(), points.end(), std::back_inserter(clusterIds), [](algorithms::Point x) {
return x.cluster;
});
int maxClusterId = *std::max_element(clusterIds.begin(), clusterIds.end());
std::vector<Vec3b> randomColors = algorithms::getRandomColors(maxClusterId + 1);
for (auto const& point : points) {
dst(point.patchRect).setTo(cv::Scalar(randomColors[point.cluster]));
}
return dst;
}
/* Threaded testing.... */
// Function used for the testing threads
void thThreadedTestImages(int testNumber, std::string fname, int iterations, int Kclusters, int patchSize, double(*heuristicFunc)(algorithms::Point p, algorithms::Point other)) {
Mat_<Vec3b> dstImage = tiger_detection::computeTigerImageClusters(fname, iterations, Kclusters, patchSize, heuristicFunc);
if (!imwrite(std::string(OUTPUT_TEST_DIR) + std::to_string(testNumber) + fname.substr(INPUT_TEST_DIR_LEN), dstImage)) {
std::cout << "Error: " << "Test #" << std::to_string(testNumber) << ": Destination image not created: " << fname << "\n";
}
else {
std::cout << "Test #" << std::to_string(testNumber) << ": Destination image created: " << fname << "\n";
}
}
// Function used for testing a single file (generating the image clusters)
std::vector<std::thread> tiger_detection::threadedTestImages(int testNumber, int iterations, int Kclusters, int patchSize, double(*heuristicFunc)(algorithms::Point p, algorithms::Point other), std::string heuristicFuncName) {
std::pair<std::vector<std::string>, std::vector<std::string>> dirsAndFiles = file_tester::obtainFileNames();
file_tester::makeTestDirs(testNumber, dirsAndFiles.first, iterations, Kclusters, patchSize, heuristicFuncName);
std::cout << "Test #" << std::to_string(testNumber) << ": Clustering images..." << "\n";
std::vector<std::thread> threads;
for (auto const& fileName : dirsAndFiles.second) {
std::thread th(thThreadedTestImages, testNumber, fileName, iterations, Kclusters, patchSize, heuristicFunc);
threads.push_back(std::move(th));
}
return threads;
}
// Function used for testing multiple files with randomized arguments values
void tiger_detection::randomizedTesting(int numberOfTests) {
std::uniform_int_distribution<std::mt19937::result_type> distIterations(15, 60);
std::uniform_int_distribution<std::mt19937::result_type> distKclusters(2, 20);
std::vector<int> defaultPatchSizes{ 8, 16 };
std::uniform_int_distribution<std::mt19937::result_type> distPatchSize(0, 1);
std::uniform_int_distribution<std::mt19937::result_type> distFunctionNumber(0, 1);
std::vector<std::thread> threads;
for (int i = 1; i <= numberOfTests; i++) {
std::vector<std::thread> localThreads;
std::cout << "Starting test #" << i << "\n";
int funcNum = distFunctionNumber(rng);
if (funcNum == 0) {
localThreads = tiger_detection::threadedTestImages(i, distIterations(rng), distKclusters(rng), defaultPatchSizes[distPatchSize(rng)], algorithms::euclidianHeuristic, "Euclidian Distance");
}
else {
localThreads = tiger_detection::threadedTestImages(i, distIterations(rng), distKclusters(rng), defaultPatchSizes[distPatchSize(rng)], algorithms::cosineSimilarityHeuristic, "Cosine Similarity Distance");
}
for (auto& th : localThreads) {
threads.push_back(std::move(th));
}
}
for (auto& th : threads) {
th.join();
}
}
int main()
{
// 1 - K-means clustering example
int iterations = 0;
int Kclusters = 0;
int patchSize = 8;
// 2 - Binned histogram
int bins = 1;
// 5 - Number of tests
int numberOfTests = 1;
//----------------------------------------------------------------------
int op;
do
{
system("cls");
destroyAllWindows();
printf("Menu:\n");
printf(" 1 - K-means clustering example\n");
printf(" 2 - Binned histogram\n");
printf(" 3 - Show image features\n");
printf(" 4 - EXAMPLE: Test folder of tiger images (parallel testing)\n");
printf(" 5 - Test folder of tiger images (parallel testing)\n");
printf(" 0 - Exit\n\n");
printf("Option: ");
if (scanf("%d", &op) == 1) {
switch (op)
{
case 1:
std::cout << "Iterations:\n";
std::cin >> iterations;
std::cout << "Number of clusters:\n";
std::cin >> Kclusters;
std::cout << "Patch size:\n";
std::cin >> patchSize;
tiger_detection::showClusters(iterations, Kclusters, patchSize, algorithms::cosineSimilarityHeuristic);
break;
case 2:
std::cout << "Number of bins:\n";
std::cin >> bins;
tiger_detection::showBinnedHistogram(bins);
break;
case 3:
tiger_detection::showImageFeatures();
break;
case 4:
tiger_detection::threadedTestImages(1, 10, 5, 8, algorithms::cosineSimilarityHeuristic, std::string("cosine similarity"));
break;
case 5:
std::cout << "Number of randomized tests: \n";
std::cin >> numberOfTests;
tiger_detection::randomizedTesting(numberOfTests);
break;
default:
break;
}
}
} while (op != 0);
return 0;
}