本文给出的是高动态范围图像压缩的程序,即HDR转换为LDR,其中程序中用到的hdr格式的高动态范围图像下载地址为http://download.csdn.net/detail/majinlei121/9380904
下面为高动态范围压缩程序:
clear all;
HDR = hdrread('..\HDR Images\AhwahneeGreatLounge_small.hdr');
% HDR = hdrread('..\HDR Images\AtriumMorning.hdr');
% HDR = hdrread('..\HDR Images\belgium.hdr');
% HDR = hdrread('..\HDR Images\cadik-desk02_mid.hdr');
% HDR = hdrread('..\HDR Images\designCenter.hdr');
% HDR = hdrread('..\HDR Images\desk.hdr');
% HDR = hdrread('..\HDR Images\doll.hdr');
% HDR = hdrread('..\HDR Images\groveD.hdr');
% HDR = hdrread('..\HDR Images\HancockKitchenInside_small.hdr');
% HDR = hdrread('..\HDR Images\memorial.hdr');
% HDR = hdrread('..\HDR Images\orion_correct_small.hdr');
% HDR = hdrread('..\HDR Images\paul_bunyan_small.hdr');
% HDR = hdrread('..\HDR Images\pillarsB_small.hdr');
% HDR = hdrread('..\HDR Images\snowman.hdr');
% HDR = hdrread('..\HDR Images\tinterna_small.hdr');
% HDR = hdrread('..\HDR Images\vinesunset.hdr');
% HDR = hdrread('..\HDR Images\yosemite_small.hdr');
L_in=(1/3)*(HDR(:,:,1)+HDR(:,:,2)+HDR(:,:,3));
L=log(L_in*1e6+1);
L=L/max(max(L));
% L=mat2gray(L);
alpha=0.1;
beta=1;
r=2;
nLevel = 3;
B = cell(1, nLevel);
D = cell(1, nLevel);
D_compression=cell(1, nLevel);
B{nLevel}=L;
for j = nLevel:-1:2
B{j-1}=LocalWls_HDR(B{j}, alpha, beta, r);
D{j}=B{j}-B{j-1};
r=20;
end
B0=mean(mean(B{1}))*ones(size(L));
D{1}=B{1}-B0;
for j = nLevel:-1:1
D_compression{j}=(2/pi)*atan(20*D{j});
D_compression{j}=mat2gray(D_compression{j});
end
L_out=D_compression{1}*0.5+D_compression{2}+D_compression{3};
% Rmax_clip = prctile(L_out(:),99);
% Rmin_clip = prctile(L_out(:),1);
% DR_clip = Rmax_clip/Rmin_clip;
% exponent = log(100)/log(DR_clip);
% L_out = max(0,L_out/Rmax_clip) .^ exponent;
Rmax_clip = prctile(L_out(:),99.5);
Rmin_clip = prctile(L_out(:),0.5);
L_out(L_out>Rmax_clip)=Rmax_clip;
L_out(L_out
function out = LocalWls_HDR(I, alpha, beta, r)
if ~exist('alpha','var')
alpha = 0.1;
end
if ~exist('beta','var')
beta = 1;
end
if ~exist('r','var')
r = 4;
end
[hei, wid] = size(I);
N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
mean_I = boxfilter(I, r) ./ N;
mean_II = boxfilter(I.*I, r) ./ N;
var_I = mean_II - mean_I .* mean_I;
I_x = diff(I,1,2);
I_x = padarray(I_x, [0 1 0], 'post');%进行列方向差分,求dx
I_y = diff(I,1,1);
I_y = padarray(I_y, [1 0 0], 'post');%进行行方向差分,求dy
I_gradient=abs(I_x+I_y);
I_gradient=I_gradient.^(2-beta);
I_gradient=alpha*boxfilter(I_gradient,r)./N;
a = var_I ./ (var_I + I_gradient+0.00000001);
b = mean_I - a .* mean_I;
mean_a = boxfilter(a, r) ./ N;
mean_b = boxfilter(b, r) ./ N;
out = mean_a .* I + mean_b;
end
function imDst = boxfilter(imSrc, r)
% BOXFILTER O(1) time box filtering using cumulative sum
%
% - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r)));
% - Running time independent of r;
% - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum);
% - But much faster.
[hei, wid] = size(imSrc);
imDst = zeros(size(imSrc));
%cumulative sum over Y axis
imCum = cumsum(imSrc, 1);
%difference over Y axis
imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);
%cumulative sum over X axis
imCum = cumsum(imDst, 2);
%difference over X axis
imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
end
最后实现的LDR图像有一些还是有光晕的,不知是程序的问题,还是论文本身的问题。
Gu B, Li W, Zhu M, et al. Local edge-preserving multiscale decomposition for high dynamic range image tone mapping[J]. Image Processing, IEEE Transactions on, 2013, 22(1): 70-79.