1. 背景:
伽馬校正可以用來調整圖像的亮度,公式為 I = I^gamma。
當gamma>1,高光部分動态範圍被壓縮,低光部分動态範圍被擴充(使低光部分的細節可以看清),圖像整體變暗;
當gamma<1,高光部分被擴充,低光部分被壓縮,圖像整體變亮。
如圖:
2. matlab代碼:
clc;
clear;
gamma = 0.3;
img = imread('D:/Code/Image/half.jpg');
img = rgb2gray(img);
figure,imshow(img);
img = double(img);
[row,col] = size(img);
new_img = zeros(row,col);
for i = 1:row
for j = 1:col
new_img(i,j) = img(i,j).^gamma;
end
end
new_img = mat2gray(new_img);
figure,imshow(new_img);
效果如下,gamma設定為0.3,低光部分動态範圍擴大:
3. C++代碼:
#include using namespace cv;
int main()
{
Mat img = imread("D:/Code/Image/half.jpg",0);
imshow("原始圖", img);
Mat newImg = Mat::zeros(img.size(), img.type());
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
newImg.at(i, j) = pow(img.at(i, j)/255.0, 0.3)*255.0; //[0,1]才會有gamma特性,0.2^0.3 == 0.6
}
}
imshow("效果圖", newImg);
waitKey(0);
return 0;
}