HFE grayscale HOWTO: m-file s-by-s decomposition

m-file: imsharpen.m

Basics see clhowto1_1.html

clear all

% input HFE parameters
% n - order of high pass
% Do - "diameter" ~ cutoff frequency
% a - gain for all frequencies
% b - gain of high freq.

n = 2; Do = 5;
a = 1; b = 1.5;

%load image, convert into float: double precision, already converted into grayscale, you can use rgb2gray()

imrgb = imread('Pevnost_na_skaleg.jpg');
im = im2double(imrgb);

%size of image, picture must be same in height and width (square fot fft); not resolved at this time

[im_height,im_width] = size(im);

%euclidean distance matrix, butterworth high-pass, see clhowto1_1.html

[U,V] = rc2uv(im_height,im_width);
D = sqrt(U.^2+V.^2);
H = 1./(1+(D./Do).^(2*n));

%high pass = 1-high pass, wonderful! :-)

H = 1-H;

% one can need im_height,im_width matrix A of ones!

A = ones(im_height,im_width);

% an here "ones" A is used for HFE: gain_a*A + gain_highpass*H

HPE = a.*A + b.*H;

%spectrum of image, details see clhowto1_1.html

IM = fftshift(fft2(im));

%filter image with HFE (matrix dot product)

IMF=IM.*HPE;

%image reconstruction from spectra IMF (inverse fft)

imf = ifft2(fftshift(IMF));

%comment % if 0 if you want to see details.

if 0
%print output:
figure(1); clf;
subplot(1,2,1);
%print input figure
imshow(im);
title('original figure');
%print output figure
subplot(1,2,2);
imshow(imf);
title(sprintf('filtered figure with HPE butterw. order=%d,D=%d,a=%d,b=%d',n,Do,a,b));
end

%print butter filter and original... hope you know that how to do. mesh() is pretty plot for g(u,v) functions

figure(2); colormap jet; colorbar;
mesh(HPE);
title(sprintf('HPE filter, param order=%d,diam(D)=%d,a=%d,b=%d',n,Do,a,b));
%whole fullscale
figure(3);
imshow(imf);
title(sprintf('filtered figure with HFE butterw. \n order=%d,D=%d,a=%d,b=%d',n,Do,a,b));
figure(4);
imshow(im);
title(sprintf('original'));

%writefile (png, 16bit grayscale):

imwrite(imf,'out.png','png','BitDepth',16);