caffe RandomBrightness和RandomContrast

1.

void RandomBrightness(const cv::Mat& in_img, cv::Mat* out_img,
    const float brightness_prob, const float brightness_delta) {
  float prob;
  caffe_rng_uniform(1, 0.f, 1.f, &prob);
  if (prob < brightness_prob) {
    CHECK_GE(brightness_delta, 0) << "brightness_delta must be non-negative.";
    float delta;
    caffe_rng_uniform(1, -brightness_delta, brightness_delta, &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;
  }
}

opencv的convertTo的讲解:https://blog.csdn.net/qq_22764813/article/details/52135686

             https://blog.csdn.net/qq_15505637/article/details/53887274

type是是否改变深度的,即uint8、uint16、float等,-1就是保持不变

scale是比例因子,即每个像素值需要乘的值

shift是在每个像素上加上的值

opencv调整亮度:https://blog.csdn.net/hb707934728/article/details/74914104

g(i,j)=a*f(i,j)+b  a是调整对比度,b是调整亮度,实际上就是scale调整对比度,shift调整亮度,这里就是对比度不变,调整亮度

 

 

 

 

caffe_rng_uniform:生成n个在a、b之间均匀分布的值

uniform distribution:均匀分布

http://www.voidcn.com/article/p-hqhoassv-qh.html

void caffe_rng_uniform(const int n, const Dtype a, const Dtype b, Dtype* r) {
  CHECK_GE(n, 0);
  CHECK(r);
  CHECK_LE(a, b);
  boost::uniform_real random_distribution(a, caffe_nextafter(b));
  boost::variate_generator >
      variate_generator(caffe_rng(), random_distribution);
  for (int i = 0; i < n; ++i) {
    r[i] = variate_generator();
  }
}

 

 

 

 

2.

之前的RandomBrightness是增加亮度,RandomContrast则是增加对比度,其实两个函数的实现差不多,

之前的RandomBrightness博客说过,g(i,j)=a*f(i,j)+b是对比度和亮度的数学表达,调整a是调整的对比度,调整b是调整的亮度,RandomBrightness、RandomContrast是分别实现的调整a和b

RandomBrightness和RandomContrast的prob都是表示执行这个操作的可能性,即是否调整对比度和亮度

 

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;
  }
}

 

调整的值是delta,实际上是通过在lower和upper选取均匀分布的一个值作为调整对比度的值

 

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;
  }
}

 

你可能感兴趣的:(caffe RandomBrightness和RandomContrast)