-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_Median_Filter.cpp
56 lines (40 loc) · 1.06 KB
/
6_Median_Filter.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
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
using namespace cv;
using namespace std;
int main(int argc, char* argv[]) {
Mat image = imread(argv[1], IMREAD_GRAYSCALE);
if (!image.data) {
cout << "Could not find the image! " << endl;
return -1;
}
Mat medianImg = image.clone();
int k = atoi(argv[2]);
int adj = k / 2;
//---------------------------------------MEDIAN FILTERING------------------------------------
int index;
int middle;
int size = k * k;
int* arr = new int[size];
for (int i = adj; i < image.rows - adj; i++) {
for (int j = adj; j < image.cols - adj; j++) {
index = 0;
for (int g = i - adj; g <= i + adj; g++) {
for (int h = j - adj; h <= j + adj; h++) {
arr[index] = (int)image.at<uchar>(g, h);
index++;
}
}
insertionSort(arr, size);
middle = size / 2;
medianImg.at<uchar>(i, j) = (uchar)arr[middle];
}
}
namedWindow("Image");
imshow("Image", image);
namedWindow("Median Filter");
imshow("Median Filter", medianImg);
waitKey(0);
return 0;
}