-
Notifications
You must be signed in to change notification settings - Fork 1
/
饱和度调整.txt
129 lines (116 loc) · 3.69 KB
/
饱和度调整.txt
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
//来自src/caffe/util/im_transforms.cpp
#include <random>
//判断是否相等
void CHECK_GE(float upper,float lower,string str)
{
if(upper < lower)
{
cout<<"error!"<<endl;
cout<<str<<endl;
exit();
}
}
//产生随机数
//参考https://blog.csdn.net/qq_22080999/article/details/82533368
float caffe_rng_uniform(float a,float b)
{
default_random_engine e(time(0));
uniform_real_distribution<float> u(a,b);
return u(e);
}
void RandomBrightness(const cv::Mat& in_img, cv::Mat* out_img,
const float brightness_prob, const float brightness_delta) {
float prob = caffe_rng_uniform(0.f, 1.f);
if (prob < brightness_prob) {
CHECK_GE(brightness_delta, 0, "brightness_delta must be non-negative.");
float delta = caffe_rng_uniform(-brightness_delta, brightness_delta);
AdjustBrightness(in_img, delta, out_img);
} else {
*out_img = in_img;
}
}
void AdjustBrightness(const cv::Mat& in_img, const float delta,
cv::Mat* out_img) {
if (fabs(delta) > 0) {
in_img.convertTo(*out_img, -1, 1, delta);
} else {
*out_img = in_img;
}
}
void RandomContrast(const cv::Mat& in_img, cv::Mat* out_img,
const float contrast_prob, const float lower, const float upper) {
float prob = caffe_rng_uniform(0.f, 1.f);
if (prob < contrast_prob) {
CHECK_GE(upper, lower, "contrast upper must be >= lower.");
CHECK_GE(lower, 0, "contrast lower must be non-negative.");
float delta = caffe_rng_uniform(lower, upper);
AdjustContrast(in_img, delta, out_img);
} else {
*out_img = in_img;
}
}
void AdjustContrast(const cv::Mat& in_img, const float delta,
cv::Mat* out_img) {
if (fabs(delta - 1.f) > 1e-3) {
in_img.convertTo(*out_img, -1, delta, 0);
} else {
*out_img = in_img;
}
}
void RandomSaturation(const cv::Mat& in_img, cv::Mat* out_img,
const float saturation_prob, const float lower, const float upper) {
float prob = caffe_rng_uniform(0.f, 1.f);
if (prob < saturation_prob) {
CHECK_GE(upper, lower, "saturation upper must be >= lower.");
CHECK_GE(lower, 0, "saturation lower must be non-negative.");
float delta;
caffe_rng_uniform(lower, upper);
AdjustSaturation(in_img, delta, out_img);
} else {
*out_img = in_img;
}
}
void AdjustSaturation(const cv::Mat& in_img, const float delta,
cv::Mat* out_img) {
if (fabs(delta - 1.f) != 1e-3) {
// Convert to HSV colorspae.
cv::cvtColor(in_img, *out_img, CV_BGR2HSV);
// Split the image to 3 channels.
vector<cv::Mat> channels;
cv::split(*out_img, channels);
// Adjust the saturation.
channels[1].convertTo(channels[1], -1, delta, 0);
cv::merge(channels, *out_img);
// Back to BGR colorspace.
cvtColor(*out_img, *out_img, CV_HSV2BGR);
} else {
*out_img = in_img;
}
}
void RandomHue(const cv::Mat& in_img, cv::Mat* out_img,
const float hue_prob, const float hue_delta) {
float prob = caffe_rng_uniform(0.f, 1.f);
if (prob < hue_prob) {
CHECK_GE(hue_delta, 0, "hue_delta must be non-negative.");
float delta = caffe_rng_uniform(-hue_delta, hue_delta);
AdjustHue(in_img, delta, out_img);
} else {
*out_img = in_img;
}
}
void AdjustHue(const cv::Mat& in_img, const float delta, cv::Mat* out_img) {
if (fabs(delta) > 0) {
// Convert to HSV colorspae.
cv::cvtColor(in_img, *out_img, CV_BGR2HSV);
// Split the image to 3 channels.
vector<cv::Mat> channels;
cv::split(*out_img, channels);
// Adjust the hue.
channels[0].convertTo(channels[0], -1, 1, delta);
cv::merge(channels, *out_img);
// Back to BGR colorspace.
cvtColor(*out_img, *out_img, CV_HSV2BGR);
} else {
*out_img = in_img;
}
}