?
公式:
两个参数 \alpha > 0 和 \beta 一般称作 增益 和 偏置 参数。我们往往用这两个参数来分别控制 对比度 和 亮度 。
?
- #include "stdafx.h"
- #include<iostream>
- #include<thread>
- #include<vector>
- #include <opencv2/core/core.hpp>
- #include <opencv2/contrib/contrib.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/objdetect/objdetect.hpp>
- ?
- using
namespace cv; - using
namespace std; - ?
- int g_slider_position = 0, g_slider_position2 = 0;
- Mat image;
- Mat new_image;
- double alpha, beta;
- ?
- void onTrackingbarSlide(int pos)
- {
- ???new_image = Mat::zeros(image.size(), image.type());
- ???beta = pos;
- ???for (int y = 0; y < image.rows; y++)
- ???{
- ??????for (int x = 0; x < image.cols; x++)
- ??????{
- ?????????for (int c = 0; c < 3; c++)
- ?????????{
- ????????????//saturate_cast 防止数据溢出
- ????????????new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ???imshow("New Image", new_image);
- }
- ?
- void onTrackingbarSlide2(int pos)
- {
- ???new_image = Mat::zeros(image.size(), image.type());
- ?
- ???alpha = (double)pos / 10.0;
- ?
- ???for (int y = 0; y < image.rows; y++)
- ???{
- ??????for (int x = 0; x < image.cols; x++)
- ??????{
- ?????????for (int c = 0; c < 3; c++)
- ?????????{
- ????????????//saturate_cast 防止数据溢出
- ????????????new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ???imshow("New Image", new_image);
- }
- ?
- int _tmain(int argc, _TCHAR* argv[])
- {
- ???/// 读入用户提供的图像
- ???image = imread("E:\\myImage\\sql.png");
- ?
- ???//初始化为0的数组
- ???Mat new_image = Mat::zeros(image.size(), image.type());
- ?
- ???/// 初始化
- ???cout << "* Enter the alpha value [1.0-3.0]: ";
- ???cin >> alpha;
- ???cout << "* Enter the beta value [0-100]: ";
- ???cin >> beta;
- ?
- ???/// 创建窗口
- ???namedWindow("Original Image", 1); // 1:WINDOW_AUTOSIZE
- ???namedWindow("New Image", 1);
- ?
- ???cvCreateTrackbar("亮度(增益)", "New Image", &g_slider_position, 100, onTrackingbarSlide);
- ???cvCreateTrackbar("对比度(偏置)", "New Image", &g_slider_position2, 30, onTrackingbarSlide2);
- ?
- ???/// 执行运算 new_image(i,j) = alpha*image(i,j) + beta
- ???for (int y = 0; y < image.rows; y++)
- ???{
- ??????for (int x = 0; x < image.cols; x++)
- ??????{
- ?????????for (int c = 0; c < 3; c++)
- ?????????{
- ????????????//saturate_cast 防止数据溢出
- ????????????new_image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
- ?????????}
- ??????}
- ???}
- ?
- ???/// 显示图像
- ???imshow("Original Image", image);
- ???imshow("New Image", new_image);
- ?
- ???/// 等待用户按键
- ???waitKey();
- ???return 0;
- }
?
?
参考:
http://blog.csdn.net/mjlsuccess/article/details/12401839
时间: 2024-10-29 19:11:09