-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagefilteroperations.cpp
130 lines (96 loc) · 3.57 KB
/
imagefilteroperations.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
#include "imagefilteroperations.h"
using namespace cv;
using namespace std;
imageFilterOperations::imageFilterOperations()
{
}
void imageFilterOperations::setElement(int element){
this->element=element;
}
void imageFilterOperations::setKernelSize(int kernel_size){
this->kernel_size=kernel_size;
}
int imageFilterOperations::getKernelSize(){
return kernel_size;
}
int imageFilterOperations::getElement(){
return element;
}
Mat imageFilterOperations::getImage(){
return this->selectedImage;
}
void imageFilterOperations::setImage(Mat selectedImage){
this->selectedImage=selectedImage;
}
Mat imageFilterOperations::blurFilter(){
for(int i = 1; i <getKernelSize()+2; i = i + 2){
blur(selectedImage, resultImage, Size( i, i ),Point(-1,-1));
}
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}
Mat imageFilterOperations::gaussianFilter(){
for ( int i = 1; i <getKernelSize()+2; i = i + 2 ){
GaussianBlur( selectedImage, resultImage, Size( i, i ), 0, 0 );
}
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}
Mat imageFilterOperations::sobelFilter(){
Mat grayImage,grad_x,grad_y,abs_grad_x,abs_grad_y;
int scale=1;
int delta=0;
int ddepth=CV_16S;
GaussianBlur(selectedImage,selectedImage,Size(3,3),0,0,BORDER_DEFAULT);
cvtColor(selectedImage,grayImage,COLOR_BGR2GRAY);
Sobel(grayImage,grad_x,ddepth,1,0,3,scale,delta,BORDER_DEFAULT);
Sobel( grayImage, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
convertScaleAbs( grad_y, abs_grad_y );
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, resultImage );
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}
Mat imageFilterOperations::laplacianFilter(){
int kernel = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Mat grayImage;
/// Remove noise by blurring with a Gaussian filter
GaussianBlur( selectedImage, selectedImage, Size(3,3), 0, 0, BORDER_DEFAULT );
/// Convert the image to grayscale
cvtColor( selectedImage, grayImage, COLOR_BGR2GRAY );
Laplacian( grayImage, resultImage, ddepth, kernel, scale, delta, BORDER_DEFAULT );
convertScaleAbs( resultImage, resultImage );
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}
Mat imageFilterOperations::dilationFilter(){
int dilation_type;
if(getElement()== 0)
dilation_type = MORPH_RECT;
else if(getElement()== 1)
dilation_type = MORPH_CROSS;
else if(getElement()== 2)
dilation_type = MORPH_ELLIPSE;
Mat structuringElement = getStructuringElement( dilation_type,
Size( 2*getKernelSize() + 1, 2*getKernelSize()+1 ),
Point( getKernelSize(), getKernelSize() ) );
dilate(selectedImage,resultImage,structuringElement);
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}
Mat imageFilterOperations::erosionFilter(){
int erosion_type;
if(getElement()== 0)
erosion_type = MORPH_RECT;
else if(getElement()== 1)
erosion_type = MORPH_CROSS;
else if(getElement()== 2)
erosion_type = MORPH_ELLIPSE;
Mat structuringElement = getStructuringElement( erosion_type,Size( 2*getKernelSize() + 1, 2*getKernelSize()+1 ),Point(getKernelSize(), getKernelSize() ));
erode( selectedImage, resultImage, structuringElement );
cvtColor(resultImage, resultImage, COLOR_BGR2RGB);
return resultImage;
}