getPatch

by

 

Extracts a patch from an image, given the center location and size of the patch.

getPatch(I,x,y,wx,wy)
function p = getPatch(I,x,y,wx,wy)
%% Returns the patch around the location (x,y) in the image 'I' and the patch 'p' is of size 'wy X wx'
% x  -  the x-coordinate of center
% y  -  the y-coordinate of center
% wx -  the width of the patch in x - direction
% wy -  the width of the patch in y- direction
% p  -  the patch

% NOTE:

% X is horizontal direction
% Y is vertical direction
% if no 'wy' is inputted, wy=wx
% if the center of the patch is near boundary, p may be smaller than what you asked for; 
% in that case, the patch present within the image region is returned

% Author : Mopuri K Reddy, SERC, IISc, INDIA.
% Date   : 10/10/2013.

if(nargin<4)
    error('ERROR : Missing Input argument(s)');    
end
if(nargin==4)
    wy=wx;
end

yt=(max(1,round(y-wy/2)));
yb=(min(size(I,1),round(y+wy/2)));
xl=max(1,round(x-wx/2));
xr=min(size(I,2),round(x+wx/2));

p=I(yt+1:yb,xl+1:xr,:);

Contact us