-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedian_filter.cpp
More file actions
171 lines (142 loc) · 3.55 KB
/
median_filter.cpp
File metadata and controls
171 lines (142 loc) · 3.55 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
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
#include <iostream>
#include <string>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
// These variables hold the minimum and maximum percentages,
// respectivley
int noise_level = 0;
// Matrix of the input image
Mat input_image;
// This function takes in a sigma and then creates a noisy image
Mat add_noise(int sigma, Mat input)
{
Mat noise_filter = input.clone();
RNG rng;
// Create a random noise image based on the sigma
rng.fill(input, RNG::NORMAL, 10, sigma);
// Overlay random noise image onto the input image
add(input, noise_filter, input);
// Display new noisy image
imshow("Noise Image", input);
return input;
}
void median_filter(Mat input)
{
int column_limit = input.cols;
int row_limit = input.rows;
int median_array[9];
// pixel_scalar is for storing the pixel
Scalar pixel_scalar;
// pixel_value is for storing the intensity value at a pixel
int pixel_value;
// Loops through every pixel and uses a 3x3 Kernel median filter
for (int i = 0; i < row_limit; i++)
{
for (int j = 0; j < column_limit; j++)
{
// Top left square
if (j - 1 >= 0 && i - 1 >= 0)
{
pixel_scalar = input.at<uchar>(Point(j - 1, i - 1));
median_array[0] = (pixel_scalar.val[0]);
}
else
{
median_array[0] = 0;
}
// Top middle square
if (i - 1 >= 0)
{
pixel_scalar = input.at<uchar>(Point(j, i - 1));
median_array[1] = pixel_scalar.val[0];
}
else
{
median_array[1] = 0;
}
// Top right square
if (i - 1 >= 0 && j + 1 < column_limit)
{
pixel_scalar = input.at<uchar>(Point(j + 1, i - 1));
median_array[2] = pixel_scalar.val[0];
}
else
{
median_array[2] = 0;
}
// Middle left square
if (j - 1 >= 0)
{
pixel_scalar = input.at<uchar>(Point(j - 1, i));
median_array[3] = pixel_scalar.val[0];
}
else
{
median_array[3] = 0;
}
// Center square
pixel_scalar = input.at<uchar>(Point(j, i));
median_array[4] = pixel_scalar.val[0];
// Middle right square
if (j + 1 < column_limit)
{
pixel_scalar = input.at<uchar>(Point(j + 1, i));
median_array[5] = pixel_scalar.val[0];
}
else
{
median_array[5] = 0;
}
// Bottom left square
if (j - 1 >= 0 && i + 1 < row_limit)
{
pixel_scalar = input.at<uchar>(Point(j - 1, i + 1));
median_array[6] = pixel_scalar.val[0];
}
else
{
median_array[6] = 0;
}
// Bottom middle square
if (i + 1 < row_limit)
{
pixel_scalar = input.at<uchar>(Point(j, i + 1));
median_array[7] = pixel_scalar.val[0];
}
else
{
median_array[7] = 0;
}
// Bottom right square
if (i + 1 < row_limit && j + 1 < column_limit)
{
pixel_scalar = input.at<uchar>(Point(j + 1, i + 1));
median_array[8] = pixel_scalar.val[0];
}
else
{
median_array[8] = 0;
}
sort(median_array, median_array + 9);
input.at<uchar>(Point(j, i)) = median_array[4];
}
}
imshow("Denoised", input);
}
void on_trackbar(int, void *)
{
Mat noise_image = add_noise(noise_level, input_image.clone());
median_filter(noise_image);
}
void main()
{
input_image = imread("", CV_LOAD_IMAGE_GRAYSCALE);
imshow("Noise Image", input_image.clone());
createTrackbar("Sigma", "Noise Image", &noise_level, 256, on_trackbar);
imshow("Input Image", input_image);
waitKey(0);
}