-
Notifications
You must be signed in to change notification settings - Fork 281
/
BoxSideWindowFilter.cpp
160 lines (155 loc) · 3.91 KB
/
BoxSideWindowFilter.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
#include <stdio.h>
#include <iostream>
#include <immintrin.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//针对灰度图的均值滤波+CVPR 2019的SideWindowFilter
//其他种类的滤波直接换核即可
int cnt[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
vector <int> filter[8];
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;
}
}
Mat SideWindowFilter(Mat src, int radius = 1) {
int row = src.rows;
int col = src.cols;
int channels = src.channels();
InitFilter(radius);
for (int i = 0; i < 8; i++) {
printf("%d ", cnt[i]);
}
printf("\n");
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;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
val += src.at<uchar>(i + x, j + y) * filter[k][id++];
}
}
val /= cnt[k];
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;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
//if (x == 0 && y == 0) continue;
val += src.at<uchar>(i + x, j + y) * filter[pos][id++];
}
}
dst.at<uchar>(i, j) = val / cnt[pos];
}
}
return dst;
}
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;
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++];
}
}
val /= cnt[k];
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;
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[pos][id++];
}
}
dst.at<Vec3b>(i, j)[c] = val / cnt[pos];
}
}
}
return dst;
}
int main() {
Mat src = imread("F:\\panda.jpg");
cv::imshow("origin", src);
for (int i = 0; i < 9; i++) {
src = SideWindowFilter(src, 3);
//medianBlur(src, src, 3);
}
//Mat dst;
//medianBlur(src, dst, 3);
Mat dst = SideWindowFilter(src, 3);
imshow("result", dst);
imwrite("F:\\res.jpg", dst);
waitKey(0);
}