OpenCv--调整图像亮度和对比度
图像变换
- 像素变换-点操作
- 邻域操作-区域
调整图像亮度和对比度属于像素变换-点操作
原理:g(x,y)=f(x,y)+
示例代码:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
//原理: a*f(x)+b a对应对比度,b对应亮度
Mat src1, src2;
src1 = imread("C:\\Users\\马迎伟\\Desktop\\heibao.jpg");
//src2 = imread("C:\\Users\\马迎伟\\Desktop\\a1a.jpg");
if (src1.empty())
{
cout << "could not find src1" << endl;
return -1;
}
//将 8U3转换成float类型
src1.convertTo(src2, CV_32F);
imshow("input", src1);
int height = src1.rows;
int width = src1.cols;
int nc = src1.channels();
float alpha = 1.2;
float beta = 10;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (nc==1)
{
float v = src1.at<uchar>(row, col);
src1.at<uchar>(row, col) = saturate_cast<uchar>(alpha * v + beta);
}
else if (nc==3)
{
float b = src1.at<Vec3b>(row, col)[0];
float g = src1.at<Vec3b>(row, col)[1];
float r = src1.at<Vec3b>(row, col)[2];
src1.at<Vec3b>(row, col)[0] = alpha * b + beta;
src1.at<Vec3b>(row, col)[1] = alpha * g + beta;
src1.at<Vec3b>(row, col)[2] = alpha * r + beta;
}
}
}
char name[] = "daigula k";
namedWindow(name, CV_WINDOW_AUTOSIZE);
imshow(name, src1);
waitKey(0);
}