% image import


A=fliplr(double(imread('Milan.tif')));
A_dB=10*log10(A);
figure; imagesc(A_dB,[prctile(A_dB(:),5),prctile(A_dB(:),95)]); colormap gray; colorbar


% filter design

n=9;
h=(1/n^2)*ones(n,n); 
h_=zeros(3*n,3*n);
h_(n+1:2*n,n+1:2*n)=h;
figure; surf(h_); zlim([0,1.1*(1/n^2)])

% filtering with a "manual" convolution

s1=size(A,1);
s2=size(A,2);
m=(n-1)/2;

Ae=zeros(s1+2*m,s2+2*m);    %zeropadding
Ae(m+1:end-m,m+1:end-m)=A; 


for i=m+1:s1+m
    for j=m+1:s2+m
Ae(i,j)=sum(sum(Ae(i-m:i+m,j-m:j+m).*h));
    end
end

A_dBe=10*log10(Ae);
figure; imagesc(A_dBe,[prctile(A_dBe(:),5),prctile(A_dBe(:),95)]); colormap gray; colorbar

% filtering using the bult-in convolution function

Ae=conv2(A,h); % same results can be achieved using the imfilter function (different options)
A_dBe=10*log10(Ae);
figure; imagesc(A_dBe,[prctile(A_dBe(:),5),prctile(A_dBe(:),95)]); colormap gray; colorbar



