function Par = Im2ParIm(Im,options);

if(nargin < 2), options = [];, end
d.patchsize = 7;
d.offsetrange = 25;
d.OffsetToleranceOne = 3; 
d.XYResizeFactor = 8; % the reduction factor in the bilinear step
d.OffsetToleranceTwo = 4; 
options = ResolveMissingOptions(options,d);

M = AllRelativePatchNormXCorr(Im,options.patchsize,options.offsetrange);
M = RemoveCenter(M,options);
N = MatrixDecimate_Max(M,3,options.OffsetToleranceOne,'max');
N = MatrixDecimate_Max(N,4,options.OffsetToleranceOne,'max');
sz1 = size(N);
N = reshape(N,[sz1(1),sz1(2),sz1(3)*sz1(4)]);
N = sumfilter(N,options.XYResizeFactor);
N = imresize(N,[sz1(1),sz1(2)]/(options.XYResizeFactor),'bilinear');
sz2 = size(N);
N = reshape(N,[sz2(1),sz2(2),sz1(3),sz1(4)]);
N = MatrixDecimate_Max(N,3,options.OffsetToleranceTwo,'max');
Par = MatrixDecimate_Max(N,4,options.OffsetToleranceTwo,'max');


function M = RemoveCenter(M,options);
CoreRangeLower = floor(options.offsetrange - options.patchsize/2);
CoreRangeHigh = ceil(options.offsetrange + options.patchsize/2);
CoreRangeLower = clamp(CoreRangeLower,1,min(size(M,3),size(M,4)));
CoreRangeHigh = clamp(CoreRangeHigh,1,min(size(M,3),size(M,4)));
CoreRange = CoreRangeLower:CoreRangeHigh;
M(:,:,CoreRange,CoreRange) = 0;
