%% Comparison of the Image the blocks where the classifires give better 
%  n blocks where bicubic gives better result. 
%  --------------------------------------------

Him = imread('lenna.png');  % Input image 

rs_fact = 4;                    % resolution Factor
rs_fact =  par.upscale^(round(log(rs_fact)/log(par.upscale))); 

im_ycbcr = rgb2ycbcr(Him);
lImH = double(im_ycbcr(:, :, 1));
lImL = imresize(lImH, 1/rs_fact); 


lImO = lImL;

% Patch Wise comparison of resultant Output Image and the input Image 
% This Step is used to generate membership function for selective patch processing. 

%% hImm is the output obtained by sparse coding technique. 

lIm2 = imresize(lImL, rs_fact);
lIm2 = lIm2(1:512, 1:512);
PSNR1 = (lImH - lIm2).^2;
PSNR1 = sum(PSNR1(:));
PSNR2 = (lImH - hImm).^2;
PSNR2 = sum(PSNR2(:));

pch_sz = 15; 
[row, col] = size(lImH);

lp_rep = floor([row, col]/pch_sz);

cnt1 = 1; 
cnt2 = 1;
figure, 
for r = 1:lp_rep(1)
    for c = 1:lp_rep(2)
        patch = lImH((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        patch1 = lIm2((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        patch2 = hImm((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        
        dst1 = sum((patch(:)-patch1(:)).^2);
        dst2 = sum((patch(:)-patch2(:)).^2);
               
        if (dst1-dst2) > 100
                subplot(1, 3, 1), imshow(uint8(patch)); 
                subplot(1, 3, 2), imshow(uint8(patch1)); 
                subplot(1, 3, 3), imshow(uint8(patch2));
                title('Classifier'); 
                pause(0.01); 
                imwrite(uint8(patch), ['./testing_patches/classifier' num2str(mean(double(patch(:)))) ...
                    '_std_' num2str(std(double(patch(:)))) '.png'])
                cnt1 = cnt1+1;
        end
        
        if (dst2-dst1) > 100
                subplot(1, 3, 1), imshow(uint8(patch)); 
                subplot(1, 3, 2), imshow(uint8(patch1)); 
                subplot(1, 3, 3), imshow(uint8(patch2));
                title('bicubic'); 
                pause(0.01); 
                imwrite(uint8(patch), ['./testing_patches/bicubic' num2str(mean(patch(:))) ...
                    '_std_' num2str(std(patch(:))) '.png'])
                cnt2 = cnt2+1;
        end
    end
end


%% hImm is the output obtained by sparse coding technique. 

lIm2 = imresize(lImL, rs_fact);
lIm2 = lIm2(1:512, 1:512);
PSNR1 = (lImH - lIm2).^2;
PSNR1 = sum(PSNR1(:));
PSNR2 = (lImH - hImm).^2;
PSNR2 = sum(PSNR2(:));

pch_sz = 15; 
[row, col] = size(lImH);

lp_rep = floor([row, col]/pch_sz);

cnt1 = 1; 
cnt2 = 1;

mn = zeros(1, prod(lp_rep));
figure, 
for r = 1:lp_rep(1)
    for c = 1:lp_rep(2)
        patch = lImH((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        patch1 = lIm2((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        patch2 = hImm((r-1)*pch_sz+1:r*pch_sz, (c-1)*pch_sz+1:c*pch_sz); 
        
        dst1 = sum((patch(:)-patch1(:)).^2);
        dst2 = sum((patch(:)-patch2(:)).^2);
        
        m = round(mean(patch(:)));
        mn(m) = mn(m) + 1; 
        
    end
end
x = 0:255; 
hist(mn, x); 


