原理公式:
?(?, ?) = ??(?, ?) + ? (? > 0,?是增益變量)
調整圖像的亮度和對比度相當于對圖像每個像素點的操作
#include<iostream>
#include<opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
Mat img, dst, ts;
img = imread("D:\\OpenCV\\images\\9.jpg");
if (img.empty())
{
cout << "image loading failed..." << endl;
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", img);
//單通道圖像測試,去掉注釋就可以
//cvtColor(img, img, COLOR_BGR2GRAY);
//imshow("img_gray", img);
int height = img.rows;
int weight = img.cols;
int nc = img.channels();
float alpha = 1.8;
float beta = 10;
float b, g, r, v;
img.convertTo(ts, CV_32F);
dst = Mat::zeros(img.size(), img.type());
for (int row = 0; row < height; row++)
{
for (int col = 0; col < weight; col++)
{
if (nc == 1)
{
v = img.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
else
if (nc == 3) {
b = ts.at<Vec3f>(row, col)[0];
g = ts.at<Vec3f>(row, col)[1];
r = ts.at<Vec3f>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
}
}
}
imshow("brightness_img", dst);
imwrite("D:\\OpenCV\\images\\btns_img.jpg", dst);
waitKey(0);
return 0;
}
最後,放幾張效果圖
單通道
三通道