-
Notifications
You must be signed in to change notification settings - Fork 7
/
CH5_spatial_domain_image_enhancement.cpp
393 lines (354 loc) · 17.2 KB
/
CH5_spatial_domain_image_enhancement.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <opencv2/opencv.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/imgproc.hpp> //line
#include <opencv2/imgcodecs.hpp>
#include <opencv2/core/hal/interface.h> //CV_8UC3
#include <iostream>
#include <map>
#include <cmath> //M_PI
#include "CH5.h"
using namespace std;
void FilterOp(cv::Mat& img, vector<Kernel*> kernels, bool padding, float adaptiveThreshold,
float mixRatio){
//p.146
//Template, a.k.a. kernel
/*
mixRatio: for enhance filter, dst = src * mixRatio + gradient_value
*/
//assume all kernel in "kernels"'s following member are the same
int kernelHeight = kernels[0]->kernelHeight;
int kernelWidth = kernels[0]->kernelWidth;
int kernelMiddleY = kernels[0]->kernelMiddleY;
int kernelMiddleX = kernels[0]->kernelMiddleX;
float coef = kernels[0]->coef;
bool isGradient = kernels[0]->isGradient;
// cout << "kh: " << kernelHeight << ", kw: " << kernelWidth << ", kmy: " << kernelMiddleY << ", kmx: " << kernelMiddleX << ", kcoef: " << coef << ", kg: " << isGradient << endl;
vector<vector<float>> arrs;
for(int i = 0; i < kernels.size(); i++){
arrs.push_back(kernels[i]->arr);
}
// cout << "arrs.size(): " << arrs.size() << endl;
//source image
int height = img.rows, width = img.cols;
//destination image
int newHeight = height-kernelHeight+kernelMiddleY;
int newWidth = width-kernelWidth+kernelMiddleX;
// cout << "org size: " << height << " * " << width << endl;
if(padding){
//after filter op, height will be decreased by (height-newHeight)
int padt = (height-newHeight)/2, padb = height-newHeight-padt;
int padl = (width-newWidth)/2, padr = width-newWidth-padl;
pad(img, padt, padb, padl, padr, cv::BORDER_REPLICATE);
//desitnation_image_height = (newHeight+height-newHeight) = height
newHeight = height;
newWidth = width;
//source image
height += (padt + padb);
width += (padl + padr);
}
// cout << "new src size: " << height << " * " << width << endl;
// cout << "new dst size: " << newHeight << " * " << newWidth << endl;
float maxGradient = numeric_limits<float>::lowest(), minGradient = std::numeric_limits<float>::max();
float maxEnhanced = numeric_limits<float>::lowest(), minEnhanced = std::numeric_limits<float>::max();
cv::Mat target(cv::Size(newWidth, newHeight), CV_8UC1, cv::Scalar(0));
//the matrix used for storing the middle result when doing gradient kernel operation
cv::Mat gMiddle(cv::Size(newWidth, newHeight), CV_32FC1, cv::Scalar(0));
/*
kernelHeight - kernelMiddleY: the displacement from middle to bottom
(y + kernelHeight - kernelMiddleY): the y coordinate on image coordinate to the bottom of kernel
*/
for(int y = kernelMiddleY; (y + kernelHeight - kernelMiddleY) < height; y++){
/*
(y + kernelHeight - kernelMiddleY): the pixel from source image that will be
operated with the bottom of the kernel
*/
for(int x = kernelMiddleX; (x + kernelWidth - kernelMiddleX) < width; x++){
//y and i moves along same coordinate, x and j moves along another
if(adaptiveThreshold > 0.0){ //only valid if it's larger than 0
//p.156, algo 5.1
/*
only do operation on the pixels, whose neighborhood:
1. max - min larger than T
or
2. variance larger than T
*/
//find the max and min in its neighborhood
int rangeMin = INT_MAX, rangeMax = INT_MIN;
for(int i = 0; i < kernelHeight; i++){
for(int j = 0; j < kernelWidth; j++){
rangeMin = min(rangeMin, (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j));
rangeMax = max(rangeMax, (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j));
}
}
if(rangeMax - rangeMin <= adaptiveThreshold)continue;
}
float res = 0.0f;
if(arrs.size() == 1){
for(int i = 0; i < kernelHeight; i++){
for(int j = 0; j < kernelWidth; j++){
res += arrs[0][i*kernelWidth+j] * (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j);
// cout << arrs[0][i*kernelWidth+j] << " * " << (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j) << " | ";
}
}
// if(res != 0){
// cout << "(" << y << ", " << x << ") res: " << res << endl;
// }
}else{
//support for overlaying two kernel operation result
for(vector<float>& arr : arrs){
for(int i = 0; i < kernelHeight; i++){
for(int j = 0; j < kernelWidth; j++){
res += arr[i*kernelWidth+j] * img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j);
// cout << arr[i*kernelWidth+j] * img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j) << " ";
}
}
//take abs() here!!, G = abs(Gx) + abs(Gy)!!
res = fabs(res);
// cout << endl;
// if(res != 0){
// cout << "(" << y << ", " << x << ") res: " << res << endl;
// }
}
}
if(!isGradient){
res = (float)res * coef;
res = fabs(res); //res could be negative in sharpening
res = round(res);
res = min(max((int)res, 0), 255);
target.at<uchar>(y, x) = res;
}else if(mixRatio == 0){
//gradient
// take abs() and then normalize the range to [0,255]
//it seems coef is always 1 for graident kernel?
res = (float)res * coef;
res = fabs(res);
//temporarily store it into "target" for now
gMiddle.at<float>(y, x) = res;
//record the range of graident and do normalization later
maxGradient = max(maxGradient, res);
minGradient = min(minGradient, res);
// cout << "minG: " << minGradient << ", maxG: " << maxGradient << ", res: " << res << endl;
}else{
//gradient, enhance filter
res = (float)res * coef;
//Note that the lower bound is -255 here!
res = min(max((int)res, -255), 255);
//mix(enhance)
res = (int)img.at<uchar>(y, x) * mixRatio + res;
//temporarily store it into "target" for now
gMiddle.at<float>(y, x) = res;
//record the range of graident and do normalization later
maxEnhanced = max(maxEnhanced, res);
minEnhanced = min(minEnhanced, res);
}
}
}
// cout << "minG: " << minGradient << ", maxG: " << maxGradient << endl;
if(isGradient){
for(int y = kernelMiddleY; (y + kernelHeight - kernelMiddleY) < height; y++){
for(int x = kernelMiddleX; (x + kernelWidth - kernelMiddleX) < width; x++){
float res = gMiddle.at<float>(y, x);
// cout << "res: " << res << " -> ";
if(mixRatio == 0){
//normalize from [minGradient, maxGradient] to [0,255]
res = (res-minGradient)/(maxGradient-minGradient) * 255;
}else if(maxEnhanced-minEnhanced > 0){
//enhance
//normalize from [minEnhanced, maxEnhanced] to [0,255]
res = (res-minEnhanced)/(maxEnhanced-minEnhanced) * 255;
}
// cout << res << " -> ";
res = round(res);
// cout << res << " -> ";
res = min(max((int)res, 0), 255);
// cout << res << endl;
target.at<uchar>(y, x) = res;
}
}
}
img = target;
};
void MedianFilterOp(cv::Mat& img, int kernelHeight, int kernelWidth,
int kernelMiddleY, int kernelMiddleX, bool padding, bool adaptive){
//p.146
//Template, a.k.a. kernel
//source image
int height = img.rows, width = img.cols;
//destination image
int newHeight = height-kernelHeight+kernelMiddleY;
int newWidth = width-kernelWidth+kernelMiddleX;
if(padding){
//after filter op, height will be decreased by (height-newHeight)
int padt = (height-newHeight)/2, padb = height-newHeight-padt;
int padl = (width-newWidth)/2, padr = width-newWidth-padl;
pad(img, padt, padb, padl, padr, cv::BORDER_REPLICATE);
//desitnation_image_height = (newHeight+height-newHeight) = height
newHeight = height;
newWidth = width;
//source image
height += (padt + padb);
width += (padl + padr);
}
cv::Mat target(cv::Size(newWidth, newHeight), CV_8UC1, cv::Scalar(0));
/*
kernelHeight - kernelMiddleY: the displacement from middle to bottom
(y + kernelHeight - kernelMiddleY): the y coordinate on image coordinate to the bottom of kernel
*/
for(int y = kernelMiddleY; (y + kernelHeight - kernelMiddleY) < height; y++){
/*
(y + kernelHeight - kernelMiddleY): the pixel from source image that will be
operated with the bottom of the kernel
*/
for(int x = kernelMiddleX; (x + kernelWidth - kernelMiddleX) < width; x++){
float res;
//y and i moves along same coordinate, x and j moves along another
if(adaptive){
//p.162
/*
only do operation on the pixels, which is max or min in its neighborhood
*/
//find the max and min in its neighborhood
int rangeMin = INT_MAX, rangeMax = INT_MIN;
for(int i = 0; i < kernelHeight; i++){
for(int j = 0; j < kernelWidth; j++){
rangeMin = min(rangeMin, (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j));
rangeMax = max(rangeMax, (int)img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j));
}
}
if((img.at<uchar>(y, x) != rangeMax) && (img.at<uchar>(y, x) != rangeMin))continue;
}
vector<int> nbhd(kernelHeight * kernelWidth, 0); //neighborhood
for(int i = 0; i < kernelHeight; i++){
for(int j = 0; j < kernelWidth; j++){
nbhd[i*kernelWidth+j] = img.at<uchar>(y-kernelMiddleY+i, x-kernelMiddleX+j);
}
}
//ndhd's size is always odd, so we just access nbhd[nbhd.size()/2]
nth_element(nbhd.begin(), nbhd.begin()+nbhd.size()/2, nbhd.end());
target.at<uchar>(y, x) = nbhd[nbhd.size()/2];
}
}
img = target;
};
// void LoGFilterOp(){
// //p.176, CH9
// };
void addNoise(cv::Mat& img, string mode, double mean, double stddev){
//p.157, Matlab imnoise
if(mode == "gaussian"){
std::default_random_engine generator;
std::normal_distribution<double> distribution(mean, stddev);
for(int r = 0; r < img.rows; r++){
for(int c = 0; c < img.cols; c++){
int val = distribution(generator);
val += img.at<uchar>(r, c);
val = min(max(val, 0), 255);
img.at<uchar>(r, c) = val;
}
}
}else if(mode == "salt_pepper"){
//todo
}
};
#ifdef CH5
int main(){
cv::Mat img_color = cv::imread("images/Lenna.png");
cv::Mat img_gray = cv::imread("images/Lenna.png", 0);
cv::Mat work_color = img_color.clone();
cv::Mat work_gray = img_gray.clone();
bool isSave = false;
map<string, Kernel*> kernels = {
//p.147
{"SmoothAvg", new Kernel("SmoothAvg", {1,1,1,1,1,1,1,1,1}, 1.0/9)}
, {"SmoothAvg5", new Kernel("SmoothAvg5", {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 1.0/25)}
, {"SmoothAvg7", new Kernel("SmoothAvg7", {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 1.0/49)}
, {"SmoothGauss", new Kernel("SmoothGauss", {1,2,1,2,4,2,1,2,1}, 1.0/16)}
// , {"SobelVertical", new Kernel("SobelVertical", {-1,0,1, -2,0,2, -1,0,1}, 1.0)} //coef?
// , {"SobelHorizontal", new Kernel("SobelHorizontal", {-1,-2,-1, 0,0,0, 1,2,1}, 1.0)}
, {"LogEdgeDetection", new Kernel("LogEdgeDetection", {0,0,-1,0,0, 0,-1,-2,-1,0, -1,-2,16,-2,-1, 0,-1,-2,-1,0, 0,0,-1,0,0}, 1.0)}
// , {"LaplacianEdgeDetection45", new Kernel("LaplacianEdgeDetection45", {0,-1,0, -1,4,-1, 0,-1,0}, 1.0)}
// , {"LaplacianEdgeDetection90", new Kernel("LaplacianEdgeDetection90", {-1,-1,-1, -1,8,-1, -1,-1,-1}, 1.0)}
};
for(auto it = kernels.begin(); it != kernels.end(); it++){
Kernel* kernel = it->second;
work_gray = img_gray.clone();
FilterOp(work_gray, {kernel}, true);
vector<cv::Mat> imgs = {img_gray, work_gray};
ShowHorizontal(imgs, kernel->name, isSave);
}
cv::Mat noise = img_gray.clone();
addNoise(noise, "gaussian", 0.0, 15.0);
//average filter
cv::Mat average = img_gray.clone();
Kernel* avgKernel = kernels["SmoothAvg"];
FilterOp(average, {avgKernel}, false);
//gaussian filter
cv::Mat gaussian = img_gray.clone();
Kernel* gKernel = kernels["SmoothGauss"];
FilterOp(gaussian, {gKernel}, false);
//median filter
cv::Mat median = img_gray.clone();
MedianFilterOp(median, 3, 3, 1, 1, false, false);
vector<cv::Mat> imgs = {noise, average, gaussian, median};
ShowHorizontal(imgs, "Average vs Gaussian vs Median", isSave);
//Sharpen, gradient kernels
map<string, Kernel*> gradientKernels = {
//p.163
{"RobertP45G", new Kernel("RobertP45G", {-1,0, 0,1}, 1.0, true)} //robert positive 45
, {"RobertN45G", new Kernel("RobertN45G", {0,-1, 1,0}, 1.0, true)} //robert negative 45
//p.165
, {"SobelVerticalG", new Kernel("SobelVerticalG", {-1,0,1, -2,0,2, -1,0,1}, 1.0, true)}
, {"SobelHorizontalG", new Kernel("SobelHorizontalG", {-1,-2,-1, 0,0,0, 1,2,1}, 1.0, true)}
//p.167
, {"Laplacian90", new Kernel("Laplacian90", {0,-1,0, -1,4,-1, 0,-1,0}, 1.0, true)}
, {"Laplacian45", new Kernel("Laplacian45", {-1,-1,-1, -1,8,-1, -1,-1,-1}, 1.0, true)}
, {"LaplacianWeighted", new Kernel("LaplacianWeighted", {-1,-4,-1, -4,20,-4, -1,-4,-1}, 1.0, true)}
//p.176
//Laplacian of Gaussian
};
// for(auto it = gradientKernels.begin(); it != gradientKernels.end(); it++){
// Kernel* kernel = it->second;
// work_gray = img_gray.clone();
// FilterOp(work_gray, {kernel}, true, 0.0);
// vector<cv::Mat> imgs = {img_gray, work_gray};
// ShowHorizontal(imgs, kernel->name, isSave);
// // Show(work_gray, kernel->name);
// }
cv::Mat rp45g = img_gray.clone();
FilterOp(rp45g, {gradientKernels["RobertP45G"]}, true, 0.0);
cv::Mat rn45g = img_gray.clone();
FilterOp(rn45g, {gradientKernels["RobertN45G"]}, true, 0.0);
cv::Mat rpn45g = img_gray.clone();
FilterOp(rpn45g, {gradientKernels["RobertP45G"], gradientKernels["RobertN45G"]}, true, 0.0);
vector<cv::Mat> RobertImgs = {img_gray, rp45g, rn45g, rpn45g};
ShowHorizontal(RobertImgs, "Robert P vs N vs P+N 45G", isSave);
cv::Mat vsg = img_gray.clone();
FilterOp(vsg, {gradientKernels["SobelVerticalG"]}, true, 0.0);
cv::Mat hsg = img_gray.clone();
FilterOp(hsg, {gradientKernels["SobelHorizontalG"]}, true, 0.0);
cv::Mat vhsg = img_gray.clone();
FilterOp(vhsg, {gradientKernels["SobelVerticalG"], gradientKernels["SobelHorizontalG"]}, true, 0.0);
vector<cv::Mat> SobelImages = {img_gray, vsg, hsg, vhsg};
ShowHorizontal(SobelImages, "Sobel V vs H vs V+H G", isSave);
cv::Mat l90 = img_gray.clone();
FilterOp(l90, {gradientKernels["Laplacian90"]}, true, 0.0);
cv::Mat l45 = img_gray.clone();
FilterOp(l45, {gradientKernels["Laplacian45"]}, true, 0.0);
cv::Mat lw = img_gray.clone();
FilterOp(lw, {gradientKernels["LaplacianWeighted"]}, true, 0.0);
vector<cv::Mat> LaplacianImages = {img_gray, l90, l45, lw};
ShowHorizontal(LaplacianImages, "Laplacian 90 vs 45 vs Weighted G", isSave);
cv::Mat rp45gEnhanced = img_gray.clone();
FilterOp(rp45gEnhanced, {gradientKernels["RobertP45G"]}, true, 0.0, 1.8);
vector<cv::Mat> RobertEnhancedImages = {img_gray, rp45g, rp45gEnhanced};
ShowHorizontal(RobertEnhancedImages, "RobertP45G vs Enhanced G", isSave);
cv::Mat vsgEnhanced = img_gray.clone();
FilterOp(vsgEnhanced, {gradientKernels["SobelVerticalG"]}, true, 0.0, 1.8);
vector<cv::Mat> SobelVerticalEnhancedImages = {img_gray, vsg, vsgEnhanced};
ShowHorizontal(SobelVerticalEnhancedImages, "SobelVerticalG vs Enhanced G", isSave);
cv::Mat lwEnhanced = img_gray.clone();
FilterOp(lwEnhanced, {gradientKernels["LaplacianWeighted"]}, true, 0.0, 1.8);
vector<cv::Mat> LaplacianEnhancedImages = {img_gray, lw, lwEnhanced};
ShowHorizontal(LaplacianEnhancedImages, "Laplacian Weighted vs Enhanced G", isSave);
}
#endif