https://www.cnblogs.com/wangyblzu/p/5710715.html
HSV和RGB一样是一种图像的颜色模型,h表示色调,s表示饱和度
1.RandomHue
void RandomHue(const cv::Mat& in_img, cv::Mat* out_img, const float hue_prob, const float hue_delta) { float prob; caffe_rng_uniform(1, 0.f, 1.f, &prob); if (prob < hue_prob) {
以一定概率进行色调调整 CHECK_GE(hue_delta, 0) << "hue_delta must be non-negative."; float delta; caffe_rng_uniform(1, -hue_delta, hue_delta, &delta); AdjustHue(in_img, delta, out_img); } else { *out_img = in_img; } }
需要先把rgb图转成hsv图,然后对第一个通道也就是h的通道的每个值加上delta,最后再换转成rgb图
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; } }
2.RandomSaturation
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(1, 0.f, 1.f, &prob); 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(1, lower, upper, &delta); AdjustContrast(in_img, delta, out_img); } else { *out_img = in_img; } }
RandomSaturation做的是让通道里每个像素值乘以一个数,RandomHue是加一个数,这和 RandomBrightness和RandomContrast的区别是一样的
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; } }