代码如下:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
cout << "hello world!" << endl;
Mat src = imread("D:/OpenCVprj/image/test.png");
namedWindow("src", CV_WINDOW_AUTOSIZE);
imshow("src", src);
float alpha = 1.5;
float beta = 10;
int height = src.rows;
int width = src.cols;
int n = src.channels();
Mat dst;
dst = Mat::zeros(src.size(), src.type());
Mat ml;
src.convertTo(ml, CV_32F);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (n == 3) {
float b = ml.at<Vec3f>(row, col)[0];
float g = ml.at<Vec3f>(row, col)[1];
float r = ml.at<Vec3f>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);//satyrate_cast<uchar>()确保值大小在0~255之间
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 (n == 1) {
float v = ml.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
namedWindow("dst", CV_WINDOW_AUTOSIZE);
imshow("dst", dst);
waitKey(0);
return 0;
}
结果如下: