本文为转载,原博客地址为:http://blog.csdn.net/majinlei121/article/details/50420927
本文程序根据论文"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."编写,主要为论文中提出的一种LEP局部边缘保持滤波程序,高动态图像压缩的程序在下一篇博客里。
下面为LEP函数程序:
-
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
其中用到的boxfilter()函数来自引导滤波,为:
-
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
运行程序为:
-
clear all
-
I = double(imread('..\images\stripes.png')) / 255;
-
-
alpha=0.1;
-
beta=1;
-
r=2;
-
-
out = LocalWls_HDR(I, alpha, beta, r);
-
-
figure();imshow([I, out], [0, 1]);title('input,LEP');
-
row=155;
-
figure;plot(1:size(I,2), I(row,:,1),1:size(I,2), out(row,:,1), 'LineWidth', 2);
-
legend('input','LEP');
效果图为:
|
请发表评论