% Refocus in Spatial Domain
% Low-Pass Filter

function LPF_INT(imagedata,str)

[m,n,CH]=size(imagedata);

% Filtering Matrix f

% Case : Function "filter2(f,img,'same')"
% f = [1,1,1;1,1,1;1,1,1] ./ 9; strf = sprintf('F_1_%s',str);
% f = [1,1,1,1,1;1,2,2,2,1;1,2,3,2,1;1,2,2,2,1;1,1,1,1,1] ./ 35; strf = sprintf('F_2_%s',str);
% f = fspecial('average',[3 3]);      strf = sprintf('F_average_%s',str);
% f = fspecial('gaussian',[3 3],0.5); strf = sprintf('F_gauss_%s',str);

% Case : Function "conv2(img,f,'same')"
f = zeros(m,n);
% f1 = fspecial('average',[3 3]);   strf = sprintf('F_Average_%s',str);
% f1 = fspecial('gaussian',[3 3],0.5);   strf = sprintf('F_Gaussian_%s',str);
f1 = [1,1,1,1,1;1,2,2,2,1;1,2,3,2,1;1,2,2,2,1;1,1,1,1,1] ./ 35;   strf = sprintf('F_Kiya_%s',str);
[a,b] = size(f1);   f((m-a)/2+1:(m+a)/2,(n-b)/2+1:(n+b)/2) = f1;
% fvtool(f);
% wvtool(f);
% imhist(f);

% Filtering
for ch=1:CH
    img(:,:) = imagedata(:,:,ch);
    % img = filter2(f,img,'same');
    img = conv2(img,f,'same'); % 2D Convolution in Space Domain
    imagedata(:,:,ch) = img(:,:);
end

% Scale RefocusedImage in [0,255]
imagedata = imagedata - min(imagedata(:));  % Minimum Number is 0
imagedata = imagedata/max(imagedata(:));    % Maximum Number is 1
imagedata = uint8(255*imagedata);           % values are 0 ~ 255
imwrite(imagedata, strf);

% Histgram(strf);

disp(sprintf('Filtered image written as %s',strf));

% figure(1); image(imagedata);   % Display Image
% figure(1); imagesc(imagedata); % Display Image
% imtool(strf);                  % imtool = imshow
