OpenCV(5) 对比度和亮度

 

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

 

 

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

上一篇:apache开源项目--kylin


下一篇:python---生成随机密码