-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeDetector.cpp
More file actions
96 lines (65 loc) · 2.59 KB
/
EdgeDetector.cpp
File metadata and controls
96 lines (65 loc) · 2.59 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
// EdgeDetector.cpp
#include "EdgeDetector.h"
#include <cmath>
#include "EdgeDetector.h"
#include <cmath>
#include "Convolution.h"
using namespace std;
// Default constructor
EdgeDetector::EdgeDetector() = default;
// Destructor
EdgeDetector::~EdgeDetector() = default;
// Detect Edges using the given algorithm
vector<pair<int, int>> EdgeDetector::detectEdges(const ImageMatrix& input_image) {
int Gx[3][3] = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};
int Gy[3][3] = {
{-1, -2, -1},
{0, 0, 0},
{1, 2, 1}
};
double** GxDouble = new double*[3];
double** GyDouble = new double*[3];
for (int i = 0; i < 3; i++) {
GxDouble[i] = new double[3];
GyDouble[i] = new double[3];
for (int j = 0; j < 3; j++) {
GxDouble[i][j] = static_cast<double>(Gx[i][j]); // Casting our int matrices to double here.
GyDouble[i][j] = static_cast<double>(Gy[i][j]);
}
}
Convolution convolutionGx(GxDouble, 3, 3, 1, true);
Convolution convolutionGy(GyDouble, 3, 3, 1, true);
ImageMatrix Ix = convolutionGx.convolve(input_image); // Calling the convolve function again here like in the Image Sharpening class.
ImageMatrix Iy = convolutionGy.convolve(input_image);
int width=input_image.get_width();
int height=input_image.get_height();
vector<vector<double>> magnitude(height, vector<double>(width, 0.0));
for (int y = 0; y < height; y++) { // Doing the Gradient calculating process here.
for (int x = 0; x < width; x++) {
double gradient = sqrt(pow(Ix.get_data(y, x), 2) + pow(Iy.get_data(y, x), 2));
magnitude[y][x] = gradient; // Adding the gradient values into the mangitude's values.
}
}
double sum_magnitude = 0.0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
sum_magnitude += magnitude[y][x];
}
}
double average_magnitude = sum_magnitude / (height * width); // Calculating the threshold by the formula. (height * width=total number of elements)
int threshold = static_cast<int>(average_magnitude);
vector<pair<int, int>> edgePixels; // Using the edge_pixels vector for pushing the values as appropriate threshold.
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (magnitude[y][x] > threshold) { // Checking fo threshold value.
edgePixels.emplace_back(y, x);
//edgePixels.push_back(make_pair(y, x));
}
}
}
return edgePixels;
}