OpenCV(5) 对比度和亮度

时间:2024-01-19 17:00:27

 

公式:OpenCV(5) 对比度和亮度

两个参数 \alpha > 0 和 \beta 一般称作 增益 和 偏置 参数。我们往往用这两个参数来分别控制 对比度 和 亮度 。

 

  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<thread>
  4. #include<vector>
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/contrib/contrib.hpp>
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <opencv2/imgproc/imgproc.hpp>
  9. #include <opencv2/objdetect/objdetect.hpp>
  10. using
    namespace cv;
  11. using
    namespace std;
  12. int g_slider_position = 0, g_slider_position2 = 0;
  13. Mat image;
  14. Mat new_image;
  15. double alpha, beta;
  16. void onTrackingbarSlide(int pos)
  17. {
  18.    new_image = Mat::zeros(image.size(), image.type());
  19.    beta = pos;
  20.    for (int y = 0; y < image.rows; y++)
  21.    {
  22.       for (int x = 0; x < image.cols; x++)
  23.       {
  24.          for (int c = 0; c < 3; c++)
  25.          {
  26.             //saturate_cast 防止数据溢出
  27.             new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
  28.          }
  29.       }
  30.    }
  31.    imshow("New Image", new_image);
  32. }
  33. void onTrackingbarSlide2(int pos)
  34. {
  35.    new_image = Mat::zeros(image.size(), image.type());
  36.    alpha = (double)pos / 10.0;
  37.    for (int y = 0; y < image.rows; y++)
  38.    {
  39.       for (int x = 0; x < image.cols; x++)
  40.       {
  41.          for (int c = 0; c < 3; c++)
  42.          {
  43.             //saturate_cast 防止数据溢出
  44.             new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
  45.          }
  46.       }
  47.    }
  48.    imshow("New Image", new_image);
  49. }
  50. int _tmain(int argc, _TCHAR* argv[])
  51. {
  52.    /// 读入用户提供的图像
  53.    image = imread("E:\\myImage\\sql.png");
  54.    //初始化为0的数组
  55.    Mat new_image = Mat::zeros(image.size(), image.type());
  56.    /// 初始化
  57.    cout << "* Enter the alpha value [1.0-3.0]: ";
  58.    cin >> alpha;
  59.    cout << "* Enter the beta value [0-100]: ";
  60.    cin >> beta;
  61.    /// 创建窗口
  62.    namedWindow("Original Image", 1); // 1:WINDOW_AUTOSIZE
  63.    namedWindow("New Image", 1);
  64.    cvCreateTrackbar("亮度(增益)", "New Image", &g_slider_position, 100, onTrackingbarSlide);
  65.    cvCreateTrackbar("对比度(偏置)", "New Image", &g_slider_position2, 30, onTrackingbarSlide2);
  66.    /// 执行运算 new_image(i,j) = alpha*image(i,j) + beta
  67.    for (int y = 0; y < image.rows; y++)
  68.    {
  69.       for (int x = 0; x < image.cols; x++)
  70.       {
  71.          for (int c = 0; c < 3; c++)
  72.          {
  73.             //saturate_cast 防止数据溢出
  74.             new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
  75.          }
  76.       }
  77.    }
  78.    /// 显示图像
  79.    imshow("Original Image", image);
  80.    imshow("New Image", new_image);
  81.    /// 等待用户按键
  82.    waitKey();
  83.    return 0;
  84. }

 

 

OpenCV(5) 对比度和亮度

参考:

http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html#basic-linear-transform

http://blog.csdn.net/mjlsuccess/article/details/12401839