function BilateralTMO(img, target_contrast, gammaCorrection, scaleV)
% input: hdr img
% input: target_contrast = 5; %default
% scaleV: 1 for hdr, 255 for uint8, 65535 for uint16
% gammaCorrection: open or close gamma Correction
maxgray = scaleV;
% 1 compute luminance channel
I_img = double(img(:,:,1).*20 + img(:,:,2).*40 + img(:,:,3).*1)./61;
% yuv = rgb2ycbcr(outmat./maxgray);
% yuv = rgb2ycbcr(img);
% I_img = yuv(:,:,1);
eps = 1e-6;
[r,c,col] = size(img);
log_I_img= log10(I_img+ eps);
% sigma_s = max([r, c]) * 0.02;
sigma_s = max([r, c]) * 0.02;
sigma_r = 0.4; %default 0.4
imgFil = blateralFilter_Grid(log_I_img, maxgray, sigma_s, sigma_r);
log_base = imgFil;
log_detail = log_I_img - log_base;
figure; imshow(log_base); title('base');
figure; imshow(log_detail); title('detail');
max_log_base = max(log_base(:));
min_log_base = min(log_base(:));
delta = max_log_base - min_log_base;
c_factor = log10(target_contrast) / (delta);
log_absolute = c_factor * max_log_base;
log_Iout = log_base * c_factor + log_detail - log_absolute;
Iout = 10.^(log_Iout) - eps;
Iout(Iout < 0.0) = 0.0;
%change luminance
imgOut = zeros(r,c,col);
N = Iout./I_img;
for i=1:col
imgOut(:,:,i) = img(:,:,i) .* N;
end
figure;imshow(imgOut);
title('TMO img');
imgOut1 = imgOut(:,:,1);
% gamma-correction
if gammaCorrection == 1
scale_factor = 1.0 / (10^(c_factor * max(imgFil(:))));
linearRGB = imgOut.* scale_factor;
gammaRGB = linearRGB.^(1/2.2);
figure;imshow((gammaRGB));
title('TMO img with gamma correction');
end
end
原始图像
处理后图像