-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathMedianSideWindowFilter.cpp
182 lines (174 loc) · 4.76 KB
/
MedianSideWindowFilter.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
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
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <iostream>
#include <immintrin.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//针对灰度图的中值滤波+CVPR 2019的SideWindowFilter
//其他种类的滤波直接换核即可
//记录每一个方向的核的不为0的元素个数
int cnt[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
// 记录每一个方向的滤波器
vector <int> filter[8];
//初始化半径为radius的滤波器,原理可以看https://mp.weixin.qq.com/s/vjzZjRoQw7MnkqAfvwBUNA
void InitFilter(int radius) {
int n = radius * 2 + 1;
for (int i = 0; i < 8; i++) {
cnt[i] = 0;
filter[i].clear();
}
for (int i = 0; i < 8; i++) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (i == 0 && x <= radius && y <= radius) {
filter[i].push_back(1);
}
else if (i == 1 && x <= radius && y >= radius) {
filter[i].push_back(1);
}
else if (i == 2 && x >= radius && y <= radius) {
filter[i].push_back(1);
}
else if (i == 3 && x >= radius && y >= radius) {
filter[i].push_back(1);
}
else if (i == 4 && x <= radius) {
filter[i].push_back(1);
}
else if (i == 5 && x >= radius) {
filter[i].push_back(1);
}
else if (i == 6 && y >= radius) {
filter[i].push_back(1);
}
else if (i == 7 && y <= radius) {
filter[i].push_back(1);
}
else {
filter[i].push_back(0);
}
}
}
}
for (int i = 0; i < 8; i++) {
int sum = 0;
for (int j = 0; j < filter[i].size(); j++) sum += filter[i][j] == 1;
cnt[i] = sum;
}
}
//实现Side Window Filter的中值滤波,强制保边
Mat MedianSideWindowFilter(Mat src, int radius = 1) {
int row = src.rows;
int col = src.cols;
int channels = src.channels();
InitFilter(radius);
//针对灰度图
vector <int> now;
if (channels == 1) {
Mat dst(row, col, CV_8UC1);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i < radius || i + radius >= row || j < radius || j + radius >= col) {
dst.at<uchar>(i, j) = src.at<uchar>(i, j);
continue;
}
int minn = 256;
int pos = 0;
for (int k = 0; k < 8; k++) {
int val = 0;
int id = 0;
now.clear();
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
if (filter[k][id]) now.push_back(src.at<uchar>(i + x, j + y) * filter[k][id]);
id++;
//val += src.at<uchar>(i + x, j + y) * filter[k][id++];
}
}
sort(now.begin(), now.end());
int mid = (int)(now.size());
val = now[mid / 2];
if (abs(val - src.at<uchar>(i, j)) < minn) {
minn = abs(val - src.at<uchar>(i, j));
pos = k;
}
}
int val = 0;
int id = 0;
now.clear();
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
if(filter[pos][id]) now.push_back(src.at<uchar>(i + x, j + y) * filter[pos][id]);
id++;
//val += src.at<uchar>(i + x, j + y) * filter[k][id++];
}
}
sort(now.begin(), now.end());
int mid = (int)(now.size());
val = now[mid / 2];
dst.at<uchar>(i, j) = val;
}
}
return dst;
}
//针对RGB图
Mat dst(row, col, CV_8UC3);
for (int c = 0; c < 3; c++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i < radius || i + radius >= row || j < radius || j + radius >= col) {
dst.at<Vec3b>(i, j)[c] = src.at<Vec3b>(i, j)[c];
continue;
}
int minn = 256;
int pos = 0;
for (int k = 0; k < 8; k++) {
int val = 0;
int id = 0;
now.clear();
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
//val += src.at<Vec3b>(i + x, j + y)[c] * filter[k][id++];
if (filter[k][id]) now.push_back(src.at<Vec3b>(i + x, j + y)[c] * filter[k][id]);
id++;
}
}
sort(now.begin(), now.end());
int mid = (int)(now.size());
val = now[mid / 2];
if (abs(val - src.at<Vec3b>(i, j)[c]) < minn) {
minn = abs(val - src.at<Vec3b>(i, j)[c]);
pos = k;
}
}
int val = 0;
int id = 0;
now.clear();
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
//val += src.at<Vec3b>(i + x, j + y)[c] * filter[k][id++];
if (filter[pos][id]) now.push_back(src.at<Vec3b>(i + x, j + y)[c] * filter[pos][id]);
id++;
}
}
sort(now.begin(), now.end());
int mid = (int)(now.size());
val = now[mid / 2];
dst.at<Vec3b>(i, j)[c] = val;
}
}
}
return dst;
}
int main() {
Mat src = imread("F:\\1.jpg");
for (int i = 0; i < 10; i++) {
src = MedianSideWindowFilter(src, 3);
}
imwrite("F:\\res.jpg", src);
return 0;
}