OpenCV利用像素点操作调整图像亮度
一、概述
示例代码:利用简单的数学元素提升图像的亮度。
二、示例图像
三、代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //图像像素点操作 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main( int argc, char const *argv[]) { Mat input,output; input = imread( "girl.jpg" ); if (!input.data){ cout << "can't not found image" <<endl; return -1; } imshow( "input" ,input); //获取图片的宽高 int height = input.rows; int width = input.cols; double alpha = 1.2; double beat =50; //按照原图的大小创建一个输出图像 output = Mat::zeros(input.size(),input.type()); //以下代码的意思是将input中的像素值取出,然后再经由数学运算赋值到output中 for ( int y=0;y<height;y++){ for ( int x = 0 ;x<width;x++){ //blue,saturate_cast<uchar>(value)确保值大小范围为0~255之间 output.at<Vec3b>(y,x)[0] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[0]+beat); //green output.at<Vec3b>(y,x)[1] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[1]+beat); //red output.at<Vec3b>(y,x)[2] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[2]+beat); } } imshow( "output" ,output); waitKey(0); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步