function y = tv(X) [M,N] = size(X); Dh = diff(X,[],1); Dh = [Dh;zeros(1,N)]; Dv = diff(X,[],2); Dv = [Dv zeros(M,1)]; y = sum(sum(sqrt(Dh.^2+Dv.^2))); end
P385-2
function y = atv(X) [M,N] = size(X); Dh = -diff(X,[],1); Dh = [Dh;zeros(1,N)]; Dv = -diff(X,[],2); Dv = [Dv zeros(M,1)]; y = sum(sum(abs(Dh)+abs(Dv))); end
P387
I = double(rgb2gray(imread(‘Lena.bmp‘))); I0 = I; ep=1; dt=0.25; lam=0; ep2=ep^2; [ny,nx]=size(I); iter = 80;
for i=1:iter, % 中心差法计算梯度和微分 % WN N EN % W O E % WS S ES I_x = (I(:,[2:nx nx])-I(:,[1 1:nx-1]))/2; % Ix = (E-W)/2 I_y = (I([2:ny ny],:)-I([1 1:ny-1],:))/2; % Iy = (S-N)/2 I_xx = I(:,[2:nx nx])+I(:,[1 1:nx-1])-2*I; % Ixx = E+W-2*O I_yy = I([2:ny ny],:)+I([1 1:ny-1],:)-2*I; % Iyy = S+N-2*O Dp = I([2:ny ny],[2:nx nx])+I([1 1:ny-1],[1 1:nx-1]); Dm = I([1 1:ny-1],[2:nx nx])+I([2:ny ny],[1 1:nx-1]); I_xy = (Dp-Dm)/4; % Ixy = Iyx = ((ES+WN)-(EN+WS))/4
Num = I_xx.*(ep2+I_y.^2)-2*I_x.*I_y.*I_xy+I_yy.*(ep2+I_x.^2); Den = (ep2+I_x.^2+I_y.^2).^(3/2); I_t = Num./Den + lam.*(I0-I); I=I+dt*I_t; %% evolve image by dt end imshow(I,[]);
P400
RGB = imread(‘moon.jpg‘); I = rgb2gray(RGB); J = imnoise(I,‘gaussian‘,0,0.025); imshow(J) K = wiener2(J,[5 5]); figure, imshow(K)
P401
I = im2double(imread(‘cameraman.tif‘)); imshow(I), title(‘Original Image‘);
% 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.
%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 Y 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
补充函数2——guidedfilter_color
function q = guidedfilter_color(I, p, r, eps) % GUIDEDFILTER_COLOR O(1) time implementation of guided filter using a color image as the guidance. % % - guidance image: I (should be a color (RGB) image) % - filtering input image: p (should be a gray-scale/single channel image) % - local window radius: r % - regularization parameter: eps
[hei, wid] = size(p); N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.