图像变换可以看做如下:
像素变换–点操作
领域操作–区域
调整图像亮度和对比度属于像素变换–点操作
对比度: 就是两个像素点之间的差值,差值越大对比度越高,反之越低
g(i,j)=af(i,j)+b 其中a>0,b是增益变量,a是对比度,b是亮度
saturate_cast(value)确保值大小范围为0-255;
Mat.at(i,j)[index]=value给每个像素点每个通道赋值
代码:
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include<opencv2/face.hpp>
#include<iostream>
#include<math.h>
#include <string>
#include<fstream>
using namespace cv::face;
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main() {
Mat src, dst;
src = imread("C:\\Users\\Administrator\\Desktop\\3.jpg");
//namedWindow("原始图片");
imshow("原始图片", src);
dst = Mat::zeros(src.size(), src.type());//生成和原始图片大小一样类型一样的空白图片
int rows = src.rows;
int cols = src.cols;
double alpha = 0.5;
double beta = 50;
for (int r1= 0; r1 < rows; r1++) {
for (int c = 0; c < cols; c++) {
if (src.channels() == 3) {
double b = src.at<Vec3b>(r1, c)[0];
double g = src.at<Vec3b>(r1, c)[1];
double r = src.at<Vec3b>(r1, c)[2];
dst.at<Vec3b>(r1, c)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(r1, c)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(r1, c)[2] = saturate_cast<uchar>(r*alpha + beta);
}
else {
if (src.channels() == 1) {
double v = src.at<Vec3b>(rows, cols)[0];
dst.at<uchar>(r1, c) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
}
imshow("改变后的图片", dst);
waitKey(0);
}
结果: