# How to Convert Images from Spatial domain to Frequency domain?

February 4, 2022

By  Sneha

`lang: en_US`

You may be familiar with image filtering applications in your phone which enhance the image.

Image Enhancement

Do you want to research celestial object images and study energy distribution?

Energy Distribution

These two can be successfully achieved by analyzing the image in the frequency domain.

## Introduction:

Image in spatial domain means analyzing image as it is, i.e., the value of the pixel of the image change concerning the scene. But the image in the frequency domain means analyzing the image concerning its frequency, i.e., the rate at which pixel values are changing in the spatial domain. So, neither image enhancement nor research on distributed energy is possible in the spatial domain. So converting an image from the spatial domain to the frequency domain is necessary.

Spatial domain

Frequency domain

## Steps to convert image from spatial domain to frequency domain:

• Read the image which needs to be converted, analyzed or enhanced.

Image for Processing

• Convert image from RGB to Gray image. Since digital/binary (Black & White) analysis is easy and quick. The image will be in the form of a matrix in MATLAB.

rgb2gray(img);

GrayScale Image

• Normalize the image to analyze the image in a better manner.

double(mat2gray(img1));

• Now convert image from spatial domain to frequency domain. It can be achieved using Fourier transform, i.e., take Fourier transform of the normalized image. Usually, the “centre” or the ‘0’ frequency location of the spectrum is at the first and last element, and the “ends” of the spectrum (highest frequency) are at the center of the array. That’s just how the FFT works. But that’s confusing for display because the low frequencies appear in two disconnected locations. So fftshift() moves the zero frequency location to the middle of the array so that when you display it with imshow() or plot(), it looks like you would expect

fftshift(fft2(img2));

Fourier transformed image

• Then take the logarithm of the Fourier transformed image. It increases the magnitude of the image and keeps only the real part of the image. Since image processing of complex numbers doesn’t work well.

log(1+abs(imgF));

Real Part of fourier transformed image

• Display the image. Take a bar graph of the image matrix to analyze the energy distribution of the image.

bar(I);

Frequency of Image

## MATLAB CODE:

%Conversion of image from Spatial domain to frequency domain
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Read the image that you want in frequency domain
clc;
clear;
close all;
% Convert RGB image to gray image
img1=rgb2gray(img);
% normalize the matrix to double
img2=double(mat2gray(img1));
% Take Fourier transform of the image then center shift the Fourier
% transformed imageimg
F=fftshift(fft2(img2));
% Take log of the image matrix
I=log(1+abs(imgF));
% Plot the images
subplot(2,2,1);
imshow(img);
title('Original Image');
subplot(2,2,2);
imshow(I);
title('Fourier transform of image');
subplot(2,2,3);
bar(I);
title('Frequency bar graph of image');
imginv=abs(ifft2(imgF));
subplot(2,2,4);
imshow(imginv);
title('Inverse fourier transform of image')

## Conclusion:

Not only Image Enhancement and study on energy distribution but several applications of Fourier transform in Image processing such as converted image frequencies are used to produce sound/audio, identify the type of Electromagnetic Wave they (Image) contain, etc. Let just recall the steps:

2. Convert RGB to Grayscale image
3. Normalize the image.
4. Take Fourier transform.

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

Education is our future. MATLAB is our feature. Happy MATLABing!

#### Sneha

I am Sneha G.K . I am an Electronic and Communication Engineering Student. I am a passionate Programmer and a researcher too. I enjoy exploring new things everyday and experimenting it.

#### MATLAB Helper ®

Use Website Chat or WhatsApp at +91-8104622179