9.gamma 变换提高对比度
一、gamma 校正原理
二、算法实现
1. 归一化 :将像素值转换为 0 ~ 1 之间的实数。
2.. 反归一化 :将经过预补偿的实数值反变换为 0 ~ 255 之间的整数值。具体算法为 : f*256
1 Mat gamma_correction(Mat& img, double gamma_c, double gamma_g) 2 { 3 int width = img.cols; 4 int height = img.rows; 5 int channel = img.channels(); 6 7 Mat out = Mat::zeros(height, width, CV_8UC3); 8 double val; 9 for (int y = 0; y < height; y++) 10 { 11 for (int x = 0; x < width; x++) 12 { 13 for (int c = 0; c < channel; c++) 14 { 15 //归一化 16 val = (double)img.at<Vec3b>(y, x)[c] / 255; 17 out.at<Vec3b>(y, x)[c] = (uchar)(1 / gamma_c * pow(val, 1 / gamma_g) * 255); 18 } 19 } 20 } 21 return out; 22 }