# How to turn code into a class?

4 views (last 30 days)

Show older comments

I coded the program below for frequency domain filtering and functions well.

I have to use it repeatedly to apply different filters so i made it into a function or class. I tried to learn what a class is, but i get the error illegal use

How can I do this?

%% 4. Perform frequency domain filtering by Convolution Theorem

%a) Step1: Given an input image im of size R x C, obtain the pading dimensions

imm=imread('skull.jpg');

im=imresize(imm,[226 187]);

im_double = im2double(im);% Converting the image class into "double" (currently uint8)

[R,C] = size(im_double);% reading the image size

imz = zeros(2*R,2*C);% creating a zero matrix

[r,c] = size(imz);% reading the size of the zero matrix

% Step 2:created a padded image

for i = 1:r

for j = 1:c

if i <= R && j<= C

imz(i,j) = im_double(i,j);

else

imz(i,j) = 0;

end

end

end

figure;

imshow(mat2gray(log(1+abs(imz))));

title('padded image fp');

% Step 3: creating another null array of same size

s = zeros(r,c);

% Multiplying the padded image with (-1)^(x+y)

for i = 1:r

for j = 1:c

s(i,j) = imz(i,j).*(-1).^(i + j);

end

end

figure;

imshow(s);

title('pre processed image(-1)^x+y*fp');

% Step 4: Computing the 2D DFT

IMZ = fft2(s);

figure;

imshow(mat2gray(log(1+abs(IMZ))));

title('2D DFT of the pre processed image Fp');

% Step 5: Generating the Real, Symmetric Filter Function using fregspace

[x,y] = freqspace(r,'meshgrid');

z = zeros(r,c);

for i = 1:r

for j = 1:c

z(i,j) = sqrt(x(i,j).^2 + y(i,j).^2);

end

end

% Choosing the Cut off Frequency and hence defining the low pass filter mask

H = zeros(r,c);

for i = 1:r

for j = 1:c

if z(i,j) <= 0.39 % cut-off frequency

H(i,j) = 1;

else

H(i,j) = 0;

end

end

end

figure;

imshow(mat2gray(log(1+abs(H))));

title('Low Pass Filter Mask, H');

% Step 6:Form the product using array multiplication

Hfp = IMZ.*H;

figure;

imshow(mat2gray(log(1+abs(Hfp))));

title('Low passed output, HFp');

% Step 7: gp(x,y) = {real{inverse DFT[G(u,v)]}(-1)^(x+y)

% calculation of inverse 2D DFT of the "out"

Hfp_inverse = ifft2(Hfp,'symmetric');

figure;

imshow(abs(Hfp_inverse));

title('inverse 2D DFT');

% post process operation

gp = zeros(r,c);

for i = 1:r

for j = 1:c

gp(i,j) = Hfp_inverse(i,j).*((-1).^(i+j));

end

end

figure;

imshow(gp);

title('inverse 2D DFT*(-1)^x+y, gp');

% Step 8: Obtain the final processed result g(x,y) by extracting the M X N region from the top, left quadrant of gp(x,y)

% let the smoothed image (or low pass filtered image) be "out"

g = zeros(R,C);

for i = 1:R

for j = 1:C

g(i,j) = gp(i,j);

end

end

figure;

subplot(2,1,1);

imshow(im_double);

title('input image f');

subplot(2,1,2);

imshow(g);

title('output image g');

### Answers (1)

Steven Lord
on 17 Oct 2021

##### 0 Comments

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!