You are on page 1of 2

%==================================================================

% Same code is available as part of Complete Matlab Code and Datasets


% Motion Deblurring Source Code for a Simple Case Shown in Figure 3
% Solves for 1D motion blur assuming object is moving from top to bottom
% Using the 52 element binary sequence for fluttering

%==============================================================
m = 52; % Coded Sequence length
CodeSeq=double('1010000111000001010000110011110111010111001001100111')-'0';
% Read input image
InputImage = double(readpfm_color('InputTaxi.pfm'));
[H,W,CH] = size(InputImage);
k = [235]; % Assume Blur Size in pixels = 235

% Resize image height by m/k so that the effective blur is m


InputImage1 = imresize(InputImage,[ceil(H*m/k) W],'bicubic');

% Get object size, n, knowing blurredSize = n + m - 1


n = size(InputImage1,1) - m + 1;

% Foreground: Get A matrix for foreground which encodes the blurring


Af = ComposeMotionBlurMatrix(CodeSeq, n);

% Background: bk is contribution vector of bkgrnd in blurred img for all pix


bk = abs(1 - Af*ones(n,1));

% Assume constant background in first m and last m pixels (effective blur is m)


bkLeft = zeros(size(bk)); bkLeft(1:m)=bk(1:m);
bkRite = zeros(size(bk)); bkRite(end-m+1:end)=bk(end-m+1:end);

% Ready to Solve AX=B for each color channel


A = [Af bkLeft bkRite];

for colorchannel = 1:CH


B = InputImage1(:,:,colorchannel); % coded img for channel

X = A\B; %Least square solution

OutColorImage(:,:,colorchannel) = X(1:end-2,:);
end

% Expand/shrink the deblurred image to match blur size of k


OutColorImage = imresize(OutColorImage,[H-k+1 W],'bicubic');

%==================================================================

function [A] = ComposeMotionBlurMatrix(CodeSeq,n)


CodeSeq = CodeSeq(:)/sum(CodeSeq);
k = size(CodeSeq,1);
ZeroPaddedCodeSeq = sparse([CodeSeq; zeros(n-1,1)]);
A = gallery('circul',ZeroPaddedCodeSeq)';% Create Circulant Matrix
A = A(:,1:n); % Keep first n columns out of (n+k-1) columns
%=================================================================