-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeDetector.cpp
More file actions
75 lines (59 loc) · 2.2 KB
/
EdgeDetector.cpp
File metadata and controls
75 lines (59 loc) · 2.2 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
// EdgeDetector.cpp
#include <iostream>
#include "EdgeDetector.h"
#include <cmath>
// Default constructor
EdgeDetector::EdgeDetector() {
height=width=3;
Gx = new double*[height];
Gx[0]=new double[width]{-1, 0,1};
Gx[1]=new double[width]{-2, 0,2};
Gx[2]=new double[width]{-1, 0,1};
Gy = new double*[height];
Gy[0]=new double[width]{-1, -2,-1};
Gy[1]=new double[width]{0, 0,0};
Gy[2]=new double[width]{1, 2,1};
}
//Destructor
EdgeDetector::~EdgeDetector() {
for (int i = 0; i < height; ++i) {
delete[] Gx[i];
delete[] Gy[i];
}
delete[] Gx;
delete[] Gy;
}
//Detect Edges using the given algorithm
std::vector<std::pair<int, int>> EdgeDetector::detectEdges(const ImageMatrix& input_image) {
Convolution Gx_kernel(Gx,height,width,1,true);
Convolution Gy_kernel(Gy,height,width,1,true);
//Convolution
ImageMatrix Ix= Gx_kernel.convolve(input_image);
ImageMatrix Iy= Gy_kernel.convolve(input_image);
//Getting the magnitude, and putting it in a matrix (while also getting the average)
int input_image_height=input_image.get_height();
int input_image_width=input_image.get_width();
ImageMatrix MagnitudeMatrix(input_image_height,input_image_width);
double summation=0;
for (int i = 0; i < input_image_height; ++i) {
for (int j = 0; j < input_image_width; ++j) {
double Gradient=std::sqrt((Ix.get_data(i,j)*Ix.get_data(i,j))+(Iy.get_data(i,j)*Iy.get_data(i,j)));
MagnitudeMatrix.set_data(i,j,Gradient);
summation=summation+Gradient;
}
};
//Getting the threshold
double average= summation/(input_image_width*input_image_height);
//Making the vector:
std::vector<std::pair<int, int>> EdgePixels;
//Looping over the graient matrix to find the ones over the threshold, those will be placed in the vector:
for (int i = 0; i < input_image_height; ++i) {
for (int j = 0; j < input_image_width; ++j) {
if (MagnitudeMatrix.get_data(i,j)>average){
//Pushing the pair to edge Pixels and returning them:
EdgePixels.push_back(std::make_pair(i,j));
};
}
};
return EdgePixels;
}