/
opencv_023.cpp
261 lines (247 loc) · 5.86 KB
/
opencv_023.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
using namespace cv;
void addSoaltAndPepperNoise(Mat &images, int numberOfNoise);
void adaptiveMedianBlur(Mat &images);
int getBordValue(int Length, int step, int kernelSize);
void autoCopyMakeBorder(Mat &images, int borderType, int step, int kernelSize);
void getMinMaxSplitChannel(Mat &images, double &min_val, double &max_val);
void getMedianSplitChannel(Mat &images, double &median_val);
double process_B(double Z_xy, double min_val, double max_val, double median_val);
double dealSplitSubImages(Mat &split_images,int &S_now);
void dealMainSplitImages(Mat &split_images, Mat &split_images_clone);
void dealDstEdges(Mat &src, int edges);
int main(int argc, char** argv)
{
Mat src = imread("D:/images/test.jpg", -1);
if (src.empty())
{
cout << " can`t open this ph" << endl;
}
imshow("src_win", src);
Mat images_src = src.clone();
/*
* 添加全图0.25比例的椒盐噪声
*/
int row = images_src.rows;
int col = images_src.cols;
addSoaltAndPepperNoise(images_src, row*col*0.25);
adaptiveMedianBlur(images_src);
waitKey(0);
destroyAllWindows();
return 0;
}
/*
* add salt and pepper noise into source images
* cyssmile
* 2020/3/15
*/
void addSoaltAndPepperNoise(Mat &images, int numberOfNoise)
{
RNG rng(12345);
int row = images.rows;
int col = images.cols;
for (int i = 0; i < numberOfNoise; i++)
{
int x = rng.uniform(0, row);
int y = rng.uniform(0, col);
if (i % 2 == 0)
{
images.at<Vec3b>(x, y) = Vec3b(255, 255, 255);
}
else
{
images.at<Vec3b>(x, y) = Vec3b(0, 0, 0);
}
}
imshow("salt and pepper noise", images);
}
/*
* get the value of copyMakeBorder
* cyssmile
* 2020/3/19
*/
int getBordValue(int Length, int step, int kernelSize)
{
return (Length*(step - 1) - step + kernelSize) / 2;
}
/*
* auto fill picture
* cyssmile
* 2020/3/19
*/
void autoCopyMakeBorder(Mat &images, int borderType, int step, int kernelSize)
{
int row = images.rows;
int col = images.cols;
int hightValue = getBordValue(row, step, kernelSize);
int widthValue = getBordValue(col, step, kernelSize);
copyMakeBorder(images, images, hightValue, hightValue, widthValue, widthValue, borderType);
}
/*
* adaptive median filtering
* Anchor Point at top left corner
* S_Min =3,S_Max=5
* cyssmile
* 2020/3/19
*/
void adaptiveMedianBlur(Mat &images)
{
int S_Max = 5;
int S_Min = 3;
int row = images.rows;
int col = images.cols;
Mat images_clone = images.clone();
autoCopyMakeBorder(images, BORDER_DEFAULT, 1, S_Min);
vector<Mat> sub_images;
split(images,sub_images);
vector<Mat> sub_images_clone;
split(images_clone, sub_images_clone);
for (int i=0;i<images.channels();i++)
{
dealMainSplitImages(sub_images[i], sub_images_clone[i]);
}
Mat dst;
merge(sub_images_clone,dst);
dealDstEdges(dst, S_Max);
imshow("dst_output", dst);
}
/*
* get the min and max value in images
* cyssmile
* 2020/03/19
*/
void getMinMaxSplitChannel(Mat &images,double &min_val,double &max_val)
{
Point minloc, maxloc;
minMaxLoc(images, &min_val, &max_val, &minloc, &maxloc);
}
/*
* get the median value in images(roi)
* cyssmile
* 2020/03/19
*/
void getMedianSplitChannel(Mat &images,double &median_val)
{
vector<double> images_data;
for (int i =0;i<images.rows;i++)
{
for (int j = 0; j < images.cols; j++)
{
images_data.push_back(images.at<uchar>(i, j));
}
}
sort(images_data.begin(),images_data.end());
median_val = images_data[images.rows*images.cols/2];
}
/*
* process_B
* cyssmile
* 2020/03/19
*/
double process_B(double Z_xy,double min_val,double max_val,double median_val)
{
if (Z_xy-min_val>0 && Z_xy-max_val<0)
{
return Z_xy;
}
else
{
return median_val;
}
}
/*
* give a roi images and then return in (x,y) its (maybe) value
* cyssmile
* 20/03/19
*/
double dealSplitSubImages(Mat &split_images,int &S_now)
{
double min = split_images.at<uchar>(0, 0);
double &min_val = min;
double max = split_images.at<uchar>(0, 0);
double &max_val = max;
getMinMaxSplitChannel(split_images, min_val, max_val);
double median = split_images.at<uchar>(0, 0);
double &median_val = median;
getMedianSplitChannel(split_images, median_val);
double result_piexl= split_images.at<uchar>(0, 0);
if (median - min_val > 0 && median - max_val < 0)
{ // turn process B
result_piexl = process_B(split_images.at<uchar>(0, 0), min_val, max_val, median_val);
}else {
S_now = S_now + 2;
}
return result_piexl;
}
/*
* deal a splited channel images
* cyssmile
* 20/03/19
*/
void dealMainSplitImages(Mat &split_images,Mat &split_images_clone)
{
int S_Min = 3, S_Max = 7;
for (int i = 0; i < split_images_clone.rows; i++)
{
for (int j = 0; j < split_images_clone.cols; j++)
{
int S_now = S_Min;
double median;
double result_piexl = split_images_clone.at<uchar>(i, j);
if ((i + S_Max < split_images.rows )&& (j + S_Max < split_images.cols))
{
while (S_now <= S_Max)
{ //重复A处理过程
Rect rec;
rec.x = j;
rec.y = i;
rec.width = S_now;
rec.height = S_now;
if ((rec.x + S_now >= split_images.rows) && (rec.y + S_now >= split_images.cols))
{
break;
}
Mat sub = split_images(rec);
int S_old = S_now;
result_piexl = dealSplitSubImages(sub, S_now);
if (S_old == S_now)
{
break;
}
else
{
getMedianSplitChannel(sub, median);
result_piexl = median;
}
}
}
split_images_clone.at<uchar>(i, j) = result_piexl;
}
}
}
/*
* deal edges int dst images
* cyssmile
* 20/03/19
*/
void dealDstEdges(Mat &src,int edges)
{
Rect rec;
rec.x = src.cols - edges;
rec.y = 0 ;
rec.width = edges;
rec.height = src.rows;
Mat NeedDealMat = src(rec);
medianBlur(NeedDealMat, NeedDealMat, 3);
Rect rec_bottom;
rec_bottom.x = 0;
rec_bottom.y = src.rows - edges;
rec_bottom.width = src.cols;
rec_bottom.height = edges;
Mat NeedDealMat_bottom = src(rec_bottom);
medianBlur(NeedDealMat_bottom, NeedDealMat_bottom, 3);
}