Getting Started with Image Processing using MATLAB
Getting Started with Image Processing using MATLAB
Imagine pointing your camera to some object and the camera tells you the name of that object, yes, Google Lens in Android
smart phones is doing the same thing using Image Processing. This gives computer a vision to detect and recognize the things
and take actions accordingly. Image processing has lot of applications like Face detection & recognition, thumb impression,
augmented reality, OCR, Barcode scan and many more. There are lot of softwares available for image processing, among them
MATLAB is the most suitable to start with.
MATLAB can perform many advance image processing operations, but for Getting started with Image processing in MATLAB,
here we will explain some basic operations like RGB to Gray, rotate the image, binary conversion etc. You can further make
automated programs for noise removal, image clarity, filtering by using the functions explained in this tutorial.
Before proceeding further, if you are new to MATLAB you can check our previous MATLAB tutorials for better understating:
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 1/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In MATLAB, as always, there are two ways to perform any image processing algorithm, one is by directly entering the
command in the editor/command window and other is by creating a GUI for the same. Here, we will show you both the
methods to perform basic operations of image processing in MATLAB.
b = rgb2gray(a);
subplot(2,3,2);
imshow(b);
c = im2bw(a);
subplot(2,3,3);
imshow(c);
d = imadjust(b);
subplot(2,3,4);
imshow(d);
e = a;
e=rgb2gray(e);
subplot(2,3,5);
imhist(e);
%colormap('spring')
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 2/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In variable ‘a’, we are importing image using command imread(‘filename’) and then making a plot of ‘2’ row and ‘3’ column
using subplot(row, column, position) and displaying the imported image on position ‘1’. To show the image we use command
imshow(‘filename’).
Below are few commands to perform some basic processing on uploaded image:
In variable ‘b’, we are converting the RGB image into grayscale intensity image by using the command rgb2gray(‘filename’)
and displaying it in plot on position ‘2’.
In variable ‘c’, we are converting the image into binary image or you can say in format of ‘0’ (black) and ‘1’ (white) by using
the command im2bw(‘filename’) and displaying it in plot on position ‘3’.
In variable ‘d’, we are adjusting or mapping the grayscale image intensity values by using the command imadjust(‘filename’)
and displaying it in plot on position ‘4’.
In variable ‘e’, we are plotting the histogram of the grayscale image by using the command imhist(‘filename’) and displaying
it in plot on position ‘5’. For plotting the histogram you always have to convert the image into grayscale and then you will be
able to see the histogram of that graphic file.
Imfinfo(‘filename with location’) command is used to display information about the graphical file.
[height, width, colour_planes] = size(‘filename’) command is used to display the size and color planes of a particular graphic
file.
colormap('spring') is used to change the type of colormap of graphic file. Here, in my code I set this command as comment
but you can use it by removing the percentage sign. There are many types of color in MATLAB like Jet, HSV, Hot, Cool,
Summer, Autumn, Winter, Gray, Bone, Copper, Pink, Lines and spring.
Like these, there are number of commands in MATLAB which can be used to perform different tasks, you can check the image
processing functions in MATLAB (https://in.mathworks.com/help/images/functionlist.html) by following the link.
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 3/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
guide
A popup window will open, then select new blank GUI as shown in below image,
Now we have to choose number of pushbuttons (every pushbutton will perform different task) and one axis to display image.
To resize or to change the shape of the Pushbutton or Axes, just click on it and you will be able to drag the corners of the
button. By double-clicking on any of these you will be able to change the color, string, tag and other options of that particular
button. After customization it will look like this
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 4/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 5/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
You can customize the buttons as per your choice. Now when you save this, a code is generated in the Editor window of
MATLAB. Edit the generated code to set the task for different pushbuttons. Below we have edited the MATLAB code.
In the ‘uploadimage’ function, copy and paste the below code to insert the file from the PC. Here, command uigetfile(‘image
extension type’) is used for importing image in the MATLAB GUI. Read that file using command imread() and then display it
with command imshow() on axes1 using axes(handles.axes1). Now, with the command setappdata(), store the variable in the
GUI so the variable will be accessible to one part of the GUI to the other part of the GUI.
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 6/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
a=uigetfile('.jpg')
a=imread(a);
axes(handles.axes1);
imshow(a);
setappdata(0,'a',a)
Now, in every function you will see command getappdata() which is used to retrieve data which is stored using
the setappdata() in the GUI.
S. Button
Command Task to be Performed
No. Name
Upload
1. uigetfile() Click to import image from Disk
Image
Convert to
Click to convert the image into
3. im2bw() Binary
binary
Image
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 7/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In the ‘rgb2gray’ function, copy and paste the below code to convert the RGB image into grayscale by using command
rgb2gray().
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 8/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
a=getappdata(0,'a');
agray=rgb2gray(a);
axes(handles.axes1);
imshow(agray);
In the ‘im2bw’ function, copy and paste the below code to convert the image into binary image or you can say in format of ‘0’
(black) and ‘1’ (white) by using command im2bw().
a=getappdata(0,'a');
abw=im2bw(a);
axes(handles.axes1);
imshow(abw);
In the ‘reset’ function, copy and paste the below code to reset the edited image into the original image.
a=getappdata(0,'a');
axes(handles.axes1);
imshow(a);
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 9/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In the ‘histogram’ function, copy and paste the below code to plot the histogram of the grayscale image by using the command
imhist(‘filename’) and display it in on axes1. For plotting the histogram you always have to convert the image into grayscale
and then you will be able to see the histogram of that graphic file.
a=getappdata(0,'a');
ahist=a;
ahist=rgb2gray(ahist);
axes(handles.axes1);
imhist(ahist);
In the ‘complementimage’ function, copy and paste the below code to see the complement of the inserted graphic file by using
command imcomplement().
a=getappdata(0,'a');
acomp=a;
acomp=imcomplement(acomp);
axes(handles.axes1);
imshow(acomp);
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 10/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In the ‘edge’ function, copy and paste the below code to detect and find edges in grayscale image by using command
edge(‘filename’,’method’). In the place of method you can choose among these three, Canny, Prewitt and montage. We are
using Canny method for edge detection. Also you cannot detect the edge directly from the original image, first you have to
convert it into grayscale and then you can able to detect the edges.
a=getappdata(0,'a');
aedge=a;
aedge=rgb2gray(aedge);
aedge=edge(aedge,'Canny')'
axes(handles.axes1);
imshow(aedge);
In the ‘clockwise’ function, copy and paste the below code to rotate the image in the clockwise direction by using command
imrotate(filename,‘angle’)
a=getappdata(0,'a');
aclock=a;
aclock=imrotate(aclock,270);
axes(handles.axes1);
imshow(aclock);
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 11/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
In the ‘anticlockwise’ function, copy and paste the below code to rotate the image in the anti-clockwise direction by using
command imrotate(filename,‘angle’)
a=getappdata(0,'a');
aclock=a;
aclock=imrotate(aclock,90);
axes(handles.axes1);
imshow(aclock);
MATLAB may take few seconds to respond, do not click on any GUI buttons until MATLAB is showing busy message in the
lower left corner as shown below,
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 12/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
When everything is ready, import the image from the PC by clicking on the ‘Upload Image’ button. Now, you will be able to
convert or rotate the image by clicking any button accordingly. Below table will show you the task we are performing on the
click of any particular button:
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 13/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
You can even do advanced level of image processing with the Image Processing Toolbox which you can purchase from
MATHWORKS official site, some of advance level operation are listed below:
Geometric operations
Block operations
Linear filtering and filter design
Transforms
Image analysis and enhancement
Binary image operations
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 14/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
Permalink (/comment/35121#comment-35121)
Permalink (/comment/35799#comment-35799)
nice (/comment/35799#comment-35799)
nice
Permalink (/comment/36000#comment-36000)
(https://bit.ly/44HF3iH )
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 15/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
(https://bit.ly/3OfZ3lZ )
(https://bit.ly/4914ZIq )
(https://bit.ly/3qP4OyI )
(https://bit.ly/3SAFeIM )
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 16/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
(https://bit.ly/3v8jM4G )
(https://bit.ly/3tlIZrY )
(https://bit.ly/3rzvmV5 )
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 17/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
Semicon Media is a unique collection of online media, focused purely on the Electronics Community across the
globe. With a perfectly blended team of Engineers and Journalists, we demystify electronics and its related
technologies by providing high value content to our readers.
(https://www.facebook.com/circuitdigest/) (https://twitter.com/CircuitDigest)
(https://www.youtube.com/channel/UCy3CUAIYgZdAOG9k3IPdLmw) (https://www.linkedin.com/company/circuit-
digest/)
COMPANY
Privacy Policy (/privacy-policy) Cookie Policy (/cookie-policy) Terms of Use (/terms-of-use) Contact
PROJECT
555 Timer Circuits (/555-timer-circuits) Op-amp Circuits (/op-amp-circuits) Audio Circuits (/audio-
OUR NETWORK
(https://circuitdigest.com)
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 18/19
2/8/24, 10:35 PM Getting Started with Image Processing using MATLAB
(https://components101.com)
(https://iotdesignpro.com)
https://circuitdigest.com/tutorial/getting-started-with-image-processing-using-matlab 19/19