OpenCV之增强图像对比度与亮度

通过一个一元一次方程的参数控制增强亮度与对比度。



#include
#include
using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
Mat src,dest;
src = imread("D:/test/b.jpg");

dest = Mat::zeros(src.size(), src.type());


int rows = src.rows;
int cols = src.cols;


double contrast = 1.5;//对比度参数
double luminance = 30;//亮度参数


for ( int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if ( src.channels() == 3 )
{
int b = src.at(i, j)[0];
int g = src.at(i, j)[1];
int r = src.at(i, j)[2];

                                //一元一次方程
dest.at(i, j)[0] = saturate_cast(b*contrast + luminance);
dest.at(i, j)[1] = saturate_cast(g*contrast + luminance);
dest.at(i, j)[2] = saturate_cast(r*contrast + luminance);
}
else if(src.channels() == 1 )
{
int v = src.at(i, j);
dest.at(i, j) = saturate_cast(v*contrast + luminance);
}
}
}


string title1 = "增强亮度与对比度之前";
string title2 = "增强亮度与对比度之后";
namedWindow(title1, WINDOW_AUTOSIZE);
imshow(title1, src);
namedWindow(title2, WINDOW_AUTOSIZE);

imshow(title2, dest);


waitKey(0);

system("pause");
return 0;
}


你可能感兴趣的:(OpenCV之增强图像对比度与亮度)