【问题标题】:How to apply gamma correction for cv::mat?如何对 cv::mat 应用伽马校正?
【发布时间】:2021-08-14 11:09:00
【问题描述】:

我是opencv的新手。有这样一个问题:我有一个图像,我是这样获取并保存的

    Decoder decoder;
    decoder.HandleRequest_GetFrame(nullptr, filename, 1, image);
    cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
    bool isOk = cv::imwrite(save_location, image);

我需要在 cv::mat 保存之前对其应用伽马校正。我试图谷歌,但看起来openCV没有像cv::gamma(image, 2.2)这样的直接方法。

怎么做?

【问题讨论】:

    标签: c++ image opencv image-processing


    【解决方案1】:

    Gamma 校正在 opencv 中不直接实现。
    您可以做的是实现一个查找表,将 0 到 255 的每个值映射到 gamma 校正后的相应值:((value/255) ** (1/gamma)) * 255。创建 LUT 后,您可以使用 cv2.LUT 将图像的值映射到校正后的值 - cv2.LUT(image, table)

    python实现链接:
    https://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/#:~:text=There%20are%20two%20(easy)%20ways,range%20%5B0%2C%20255%5D.

    【讨论】:

      【解决方案2】:

      最终,我找到了方法(根据本教程https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html):

          //>>>Get Frame
          Decoder decoder;
          decoder.HandleRequest_GetFrame(nullptr, filename, 1, image);
          //<<<
      
          //>>> Collor conversion from BGR to RGB
          cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
          //<<<
      
      #if 1
          //>>> Gamma correction
          cv::Mat new_image = cv::Mat::zeros(image.size(), image.type());
          double alpha = 2.2; /*< Simple contrast control */
          int beta = 0;       /*< Simple brightness control */
      
          for (int y = 0; y < image.rows; y++) {
              for (int x = 0; x < image.cols; x++) {
                  for (int c = 0; c < image.channels(); c++) {
                      new_image.at<cv::Vec3b>(y, x)[c] =
                          cv::saturate_cast<uchar>(alpha*image.at<cv::Vec3b>(y, x)[c] + beta);
                  }
              }
          }
      
          image = new_image;
          //<<<
      #endif // 0
      
          bool isOk = cv::imwrite(save_location, image);
      

      【讨论】:

      • 您描述的是 alpha-beta 校正而不是 gamma 校正,不是吗?
      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 2011-09-28
      相关资源
      最近更新 更多