#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// read image
Mat src = imread("C:/Users/qxq/Pictures/image/1.jpg");
if (src.empty())
{
printf("Could not load image...");
return -1;
}
转化为灰度图
//cvtColor(src, src, CV_BGR2GRAY);
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
/*
//如果想用32f格式
Mat M1;
src.convertTo(M1, CV_32F);
//下面循环中的这句就可以用Vec3f.
float b = M1.at<Vec3f>(row, col)[0];
*/
//contrast and brightness changes
//对比度和亮度的改变
int height = src.rows;
int width = src.cols;
Mat dst;
dst = Mat::zeros(src.size(), src.type());
float alpha = 0.8;//表示对比度的系数
float beta = 10;//表示亮度的系数
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (src.channels() == 3)
{
float b = src.at<Vec3b>(row, col)[0];
float g = src.at<Vec3b>(row, col)[1];
float r = src.at<Vec3b>(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);
}
else if (src.channels() == 1)
{
float v = src.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
char output_title[] = "contrast and brightness change done";
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
imshow(output_title, dst);
waitKey(0);
return 0;
}