

function [Wrds, Xvar, Mns, Xorg] = test_sample_patches(lIm, patch_size, ovrlap)

[nrow, ncol] = size(lIm);

% generate low resolution counter parts
% lIm=im2double(lIm); % so that it scales down to an integer size

x = 1:patch_size-ovrlap:nrow-patch_size+1;
y = 1:patch_size-ovrlap:ncol-patch_size+1;

[X,Y] = meshgrid(x,y);

xrow = X(:);
ycol = Y(:);

lIm = double(lIm);

Wrds = zeros(5*patch_size^2,   length(xrow));
Mns = zeros(1,   length(xrow));
Xvar = zeros(1,   length(xrow));
Xorg = zeros(patch_size^2,   length(xrow));

% compute the first and second order gradients
hf1 = [-1,0,1];
vf1 = [-1,0,1]';
 
lImG11 = conv2(lIm, hf1,'same');
lImG12 = conv2(lIm, vf1,'same');
 
hf2 = [1,0,-2,0,1];
vf2 = [1,0,-2,0,1]';
 
lImG21 = conv2(lIm,hf2,'same');
lImG22 = conv2(lIm,vf2,'same');


for ii = 1:length(xrow), 
    
    row = xrow(ii);
    col = ycol(ii);
    
    Lpatch = lIm(row:row+patch_size-1,col:col+patch_size-1);
    
    Lpatch1 = lImG11(row:row+patch_size-1,col:col+patch_size-1);
    Lpatch2 = lImG12(row:row+patch_size-1,col:col+patch_size-1);
    Lpatch3 = lImG21(row:row+patch_size-1,col:col+patch_size-1);
    Lpatch4 = lImG22(row:row+patch_size-1,col:col+patch_size-1);
     
    LpatchM = [Lpatch1(:),Lpatch2(:),Lpatch3(:),Lpatch4(:)];
    tn = mean(Lpatch(:)); 
    
    Wrds(:, ii) = [LpatchM(:);Lpatch(:)-tn];
    
    Mns(ii) = tn;
    Xvar(ii) = sqrt(sum((Lpatch(:)-tn).^2));
    Xorg(:, ii) = Lpatch(:);
            
   
end

disp('Number of Doc : ');
disp(length(xrow));
