%Read in a color image and sharpen it using (1) unsharp masking (2) Laplacian
%on each RGB component separately. (Works better than applying to Y component only.)
%sigma is standard deviation for Gaussian lowpass filter for unsharp masking.
clear;X=imread('toucan.jpg');X=double(X);sigma=5;
X=(X-min(min(min(X))))/(max(max(max(X)))-min(min(min(X))));
figure,imagesc(X),axis off,title('Original image')
%Next 3 lines: Generate Gaussian lowpass filter PSF:
L=round(5*sigma);if(2*floor(L/2)==L);L=L+1;end;L1=(L+1)/2;%make L an odd integer.
for n=1:L;for m=1:L;G(n,m)=exp(-((n-L1)^2+(m-L1)^2)/2/sigma^2)/sqrt(2*pi*sigma^2);
end;end;G=G/sum(sum(G));%Perform unsharp masking:
Y(:,:,1)=X(:,:,1)+X(:,:,1)-conv2(X(:,:,1),G,'same');
Y(:,:,2)=X(:,:,2)+X(:,:,2)-conv2(X(:,:,2),G,'same');
Y(:,:,3)=X(:,:,3)+X(:,:,3)-conv2(X(:,:,3),G,'same');
Y=(Y-min(min(min(Y))))/(max(max(max(Y)))-min(min(min(Y))));
figure,imagesc(Y),axis off,title('Unsharp masking')
%Use Laplacian. Amplifies noise.
H=-[0 1 0;1 -5 1;0 1 0];Z(:,:,1)=conv2(X(:,:,1),H,'same');
Z(:,:,2)=conv2(X(:,:,2),H,'same');Z(:,:,3)=conv2(X(:,:,3),H,'same');
Z=(Z-min(min(min(Z))))/(max(max(max(Z)))-min(min(min(Z))));
figure,imagesc(Z),axis off,title('Laplacian-sharpened')
