下面是效果展示图:
下面是代码展示:
1 #include<iostream> 2 #include<opencv.hpp> 3 4 using namespace std; 5 using namespace cv; 6 7 int main() 8 { 9 10 Mat sou; 11 sou = imread("C:\Users\32829\Desktop\aa.jpg"); 12 if (!sou.data) 13 { 14 cout << "图像载入失败" << endl; 15 } 16 namedWindow("old"); 17 imshow("old", sou); 18 19 Mat dst; 20 //定义一张空的图片 21 dst = Mat::zeros(sou.size(), sou.type()); 22 int height = sou.rows;//图片的高度 23 int weight = sou.cols;//图片的宽度 24 float alpha = 1.2;//自己设置的亮度的那个a 25 float beta = 30;//增量 26 for (int row = 0; row <( height); row++) 27 { 28 for ( int col = 0; col <( weight); col++) 29 { 30 //一通道直接就是修改就行了 31 if (sou.channels() == 1) 32 { 33 int v = sou.at<uchar>(row, col); 34 dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta); 35 36 } 37 if (sou.channels() == 3) 38 { 39 //三通道,先获得他的每个通道的数值,然后再修改,赋给新的图片 40 float b = (sou.at<Vec3b>(row, col)[0]); 41 float g = (sou.at<Vec3b>(row, col)[1]); 42 float r = (sou.at<Vec3b>(row, col)[2]); 43 dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta); 44 dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar> (g * alpha + beta); 45 dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar> (r * alpha + beta); 46 } 47 } 48 } 49 50 namedWindow("new1"); 51 imshow("new1", dst); 52 53 54 55 56 57 58 59 60 61 62 waitKey(0); 63 64 65 66 67 return 1; 68 }
但是之前用Vec3f。发生问题,但是添加一个函数就可以了,看来得转换一下,因为图片一个像素都是用一个字节来表示的;
===========================================================================
基础知识总结
===============================================================================
=======================================================================
===================================================================