-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_noise.cpp
71 lines (53 loc) · 2.07 KB
/
add_noise.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
#include <add_noise.h>
int main(int argc, char ** argv) {
// Default parameters
string case_name = "doc0";
int noise_level = 5;
int dense_level = 20;
// Parse command line parameters
const string opt_str = "t:l:d:";
int opt = getopt(argc, argv, opt_str.c_str());
while (opt != -1) {
switch (opt) {
case 't':
case_name = string(optarg);
break;
case 'l':
noise_level = atoi(optarg);
break;
case 'd':
dense_level = atoi(optarg);
break;
}
opt = getopt(argc, argv, opt_str.c_str());
}
string out_name = case_name + "_noise_" + to_string(noise_level);
cout << "Test case name: \t" << case_name << endl;
cout << "Saved name: \t" << out_name << endl;
cout << "Noise level [1-100]: \t" << noise_level << endl;
cout << "Dense level [1-1000]:\t" << dense_level << endl;
const string gt_folder = "data/gt/";
const string in_img_path = gt_folder + case_name + ".png";
const string out_img_path = gt_folder + out_name + ".png";
cv::Mat in_img = cv::imread(in_img_path);
random_device rand_device;
default_random_engine rand_engine(rand_device());
uniform_real_distribution<double> uni_dist(1 - (double)noise_level / 100, 1);
uniform_int_distribution<int> uni_dense(0, 1000);
for (int y = 0; y < in_img.rows; y++) {
for (int x = 0; x < in_img.cols; x++) {
int dense_test = rand_engine() % 1000;
// dense_test = int(abs(sin(x * y)) * dense_test);
if (dense_test > dense_level) continue;
cv::Vec3b color = in_img.at<cv::Vec3b>(y, x);
double noise_alpha = uni_dist(rand_engine);
for (int k = 0; k < 3; k++) {
color[k] = (uchar)(color[k] * noise_alpha);
}
in_img.at<cv::Vec3b>(y, x) = color;
}
}
cv::imwrite(out_img_path, in_img);
cout << "[INFO] Saved noisy image." << endl;
return 0;
}