www.pudn.com > snippets(1).rar > imgshift.m, change:2009-10-12,size:705b


function Ixy = imgshift(I,y,x)
% function Ixy = imgshift(I,y,x)
%
% shift an image by a specified amount in in x- and y-directions
% empty rows/columns are filled by replicating the border

% preparations
Iy = zeros(size(I),'single');
Ixy = zeros(size(I),'single');

% decide whether to shift left or right, and apply shift
if y>0
  Iy(:,y+1:end) = I(:,1:end-y);
  Iy(:,1:y) = repmat(I(:,1),1,y);
else
  Iy(:,1:end+y) = I(:,1-y:end);
  Iy(:,end+y+1:end) = repmat(I(:,end),1,-y);
end

% decide whether to shift up or down, and apply shift
if x>0
  Ixy(x+1:end,:) = Iy(1:end-x,:);
  Ixy(1:x,:) = repmat(Iy(1,:),x,1);
else
  Ixy(1:end+x,:) = Iy(1-x:end,:);
  Ixy(end+x+1:end,:) = repmat(Iy(end,:),-x,1);
end