-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (84 loc) · 3.37 KB
/
Copy pathmain.cpp
File metadata and controls
100 lines (84 loc) · 3.37 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
#include <chrono>
#include <opencv2/opencv.hpp>
#include <vector>
#include <cmath>
#include <iostream>
#include <omp.h>
using namespace cv;
using namespace std;
// distanza euclidea √((x₁-x₂)² + (y₁-y₂)² + (z₁-z₂)²)
inline float euclideanDistance(const Vec3f& a, const Vec3f& b) {
return sqrt((a[0] - b[0]) * (a[0] - b[0]) +
(a[1] - b[1]) * (a[1] - b[1]) +
(a[2] - b[2]) * (a[2] - b[2]));
}
void meanShift(const Mat& input, Mat& output, float radius, int max_iter = 15, float epsilon = 1e-3) {
//Conversione in floating point perchè sennò con gli interi impazzisco
Mat data;
input.convertTo(data, CV_32FC3);
// Converto immagine in vettore dove ogni elemento contiene i colori RGB
vector<Vec3f> points;
for (int y = 0; y < data.rows; ++y) {
for (int x = 0; x < data.cols; ++x) {
points.push_back(data.at<Vec3f>(y, x));
}
}
// alloco il vettore per i punti spostati
vector<Vec3f> shiftedPoints(points.size());
// eseguo mean-shift in parallelo, ciclo su ogni pixel e lo prendo come punto di partenza
#pragma omp parallel for
for (size_t i = 0; i < points.size(); ++i) {
Vec3f currentPoint = points[i];
for (int iter = 0; iter < max_iter; ++iter) {
Vec3f newPoint = Vec3f(0, 0, 0);
float totalWeight = 0;
for (const auto& neighbor : points) {
float distance = euclideanDistance(currentPoint, neighbor);
if (distance < radius) {
float weight = (distance < radius) ? 1.0 : 0.0;
newPoint += weight * neighbor;
totalWeight += weight;
}
}
newPoint /= totalWeight;
if (euclideanDistance(currentPoint, newPoint) < epsilon) break;
currentPoint = newPoint;
}
shiftedPoints[i] = currentPoint;
}
// ricostruisco l'immagine
output = Mat(data.rows, data.cols, CV_32FC3);
int index = 0;
for (int y = 0; y < output.rows; ++y) {
for (int x = 0; x < output.cols; ++x) {
output.at<Vec3f>(y, x) = shiftedPoints[index++];
}
}
// riporto l'immagine in 8 bit
output.convertTo(output, CV_8UC3);
}
int main() {
//carico l'immagine di input
Mat input = imread("/home/edoardo/CLionProjects/MeanShiftClusterOpenMP/test_images/paesaggio-grande.jpg");
Mat originalInput = imread("/home/edoardo/CLionProjects/MeanShiftClusterOpenMP/test_images/paesaggio-grande.jpg");
resize(input, input, Size(), 0.5, 0.5, INTER_AREA);
resize(originalInput, originalInput, Size(), 0.5, 0.5, INTER_AREA);
//converto da BGR a RGB e da RGB a Lab
cvtColor(input, input, COLOR_BGR2RGB);
cvtColor(input, input, COLOR_RGB2Lab);
Mat output;
//raggio di ricerca dei vicini
float radius = 5.0;
//eseguo mean-shift e misuro tempo di esecuzione
auto start = std::chrono::high_resolution_clock::now();
meanShift(input, output, radius);
auto end = std::chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "Tempo di esecuzione con OpenMP: " << elapsed.count() << " secondi" << endl;
cvtColor(output, output, COLOR_Lab2RGB);
cvtColor(output, output, COLOR_RGB2BGR);
imshow("Processata", input);
imshow("Originale", output);
waitKey(0);
return 0;
}