OpenCV(5) 对比度和亮度
2015-11-13 13:36
344 查看
公式:
![](http://images2015.cnblogs.com/blog/249742/201511/249742-20151113133539275-2061120725.png)
两个参数 \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://images2015.cnblogs.com/blog/249742/201511/249742-20151113133539931-733542387.png)
参考:
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
相关文章推荐
- 韩顺平Linux笔记(十三)——Mysql数据库在linux下的使用
- Operating System: Three Easy Pieces --- One More Problem (Note)
- Mr. Process的一生-Linux内核的社会视角 (1)调度
- 修改linux系统默认字符编码
- Stybaniewicz夺得今年规模最大WSOPC赛事冠军
- 刚安装完Linux不能上网问题!
- linux IP 设置
- 钻一钻Tomcat 之 Tomcat项目部署(Ⅵ)
- 虚拟机下Linux虚拟机克隆后导致克隆体不能上网问题!
- Linux环境进程间通信(五): 共享内存(上)
- CentOS7下Firewall防火墙配置用法详解
- linux学习基础---linux文件系统结构
- linux进程间通信-共享内存
- CentOS下安装Tomcat8
- Linux切换目录的几个命令总结
- 驱动还是应用? 这是一个多人提起的问题 韦东山
- Linux系统如何查看及修改文件读写权限
- Linux系统下实现iscsi共享存储
- VC OPEN之后没有反应解决办法
- powershell excel 导入 sqlserver