Opencv实现图像灰度线性变换
人气:0通过图像灰度线性变换提高图像对比度和亮度,原图像为src,目标图像为dst,则dst(x,y) = * src(x,y) + 。
不仅对单通道图像可以做灰度线性变换,对三通道图像同样可以。
#include<opencv2/opencv.hpp>; #include<iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src,dst; src = imread("1.jpg"); if (!src.data) { cout << "could not load image" << endl; return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); int rows = src.rows; int cols = src.cols; float alpha = 1.2, beta = 10; dst = Mat::zeros(src.size(), src.type()); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (src.channels() == 3) { int b = src.at<Vec3b>(row, col)[0]; int g = src.at<Vec3b>(row, col)[1]; int r = src.at<Vec3b>(row, col)[2]; dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>((alpha*b + beta)); dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>((alpha*g + beta)); dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>((alpha*r + beta)); } else if(src.channels()==1){ int v = src.at<uchar>(row, col); dst.at<uchar>(row, col) = saturate_cast<uchar>(alpha*v + beta); } } } namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output", dst); waitKey(0); return 0; }
运行结果如下:
您可能感兴趣的文章:
加载全部内容