0% found this document useful (0 votes)
7 views19 pages

DSP Matlab Lecture # 02

This document provides a MATLAB lecture focused on image processing techniques, including reading and displaying single and multiple images, adding two images, and converting RGB images to grayscale. It includes code examples and explanations for each technique, emphasizing prerequisites and customization options. The content is aimed at helping users understand and apply basic image processing operations in MATLAB.

Uploaded by

hmussawar477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

DSP Matlab Lecture # 02

This document provides a MATLAB lecture focused on image processing techniques, including reading and displaying single and multiple images, adding two images, and converting RGB images to grayscale. It includes code examples and explanations for each technique, emphasizing prerequisites and customization options. The content is aimed at helping users understand and apply basic image processing operations in MATLAB.

Uploaded by

hmussawar477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

MATLAB

(LECTURE # 02)
2

AGENDA
 Read and Display Single Images
 Read Two Images and Display
 How to add two images
 RGB to Gray Scale
3

READ AND DISPLAY SINGLE IMAGE


a=imread('Babar.JPG'); % Reads the image file 'Babar.JPG' into
variable 'a'

imshow(a);
4

READ AND DISPLAY SINGLE IMAGE


The imread() function reads the image file from the
specified location and stores its pixel data in the variable
a.
The variable a now contains the image data as a matrix,
where:
• Each pixel's color information is represented in grayscale (if
the image is black and white) or in RGB (if the image is in
color).
5

READ AND DISPLAY SINGLE IMAGE


 This MATLAB script reads an image file and displays it in a figure
window.
 Below is the detailed description of the code functionality:

Objective:
To load and display an image named Babar.JPG in a MATLAB figure
window.

Usage:
This script is commonly used for:
• Viewing images in MATLAB for analysis or inspection.
• Visualizing the output of image processing techniques.
6

READ AND DISPLAY SINGLE IMAGE


Prerequisites:
The file Babar.JPG must exist in the current MATLAB working
directory, or you need to specify the full path to the image file.
MATLAB's Image Processing Toolbox is required for the imshow()
function.
Customization:
Replace Babar.JPG with the name of another image file to display
a different image.
Additional processing, can be applied before displaying the image.
7

READ TWO IMAGES AND DISPLAY


a=imread('Smith.JPG');
b=imread('Cummins.JPG');
figure
subplot(121)
imshow(b)
subplot(122)
imshow(a)
8

READ TWO IMAGES AND DISPLAY


a=imread('Smith.JPG'); % Reads the image file 'Smith.JPG' into variable 'a'.
b=imread('Cummins.JPG'); % Reads the image file 'Cummins.JPG' into variable 'b’.
figure % Opens a new figure window for displaying the images.
subplot(121) % Creates a subplot layout with 1 row, 2 columns, and
selects the 1st position.
imshow(b) % Displays the image stored in 'b' (Cummins.JPG) in the first
subplot
subplot(122) % Creates the second subplot (1 row, 2 columns, 2nd
position)
imshow(a) % Displays the image stored in 'a' (Smith.JPG) in the second
subplot
9

READ TWO IMAGES AND DISPLAY


•This MATLAB script reads and displays two images side-by-
side in a single figure window using the subplot function.

Objective:

•To load two images from the disk and display them in a single
figure window arranged horizontally.

Usage:

• This script is useful for comparing two images side-by-side,


making it ideal for visual analysis in fields such as image
processing, computer vision, and digital forensics.
10

READ TWO IMAGES AND DISPLAY


In MATLAB, the subplot function is used to create multiple plots in a
single figure window.
The syntax subplot(121) is a shorthand for subplot(m, n, p) where:
 m: The number of rows in the subplot grid.
 n: The number of columns in the subplot grid.
 p: The position of the current subplot.
For subplot(121):
 m = 1: There is 1 row of subplots.
 n = 2: There are 2 columns of subplots.
 p = 1: The plot will occupy the first position in this 1x2 grid.
11

READ TWO IMAGES AND DISPLAY


Prerequisites:
• Ensure that Smith.JPG and Cummins.JPG exist in current MATLAB
working directory or specify the full path to the images.
• MATLAB’s Image Processing Toolbox must be installed for the
imshow() function.
Customization:
You can replace the file names Smith.JPG and Cummins.JPG with
other image files.
 Modify the subplot layout to display images in different
arrangements (e.g., 2 rows, 1 column).
12

HOW TO ADD TWO IMAGES


a=imread('Smith.JPG');
b=imread('Cummins.JPG');
c=imadd(a,b);
subplot(221);imshow(a);title('1st image')
subplot(222);imshow(b);title('2nd image')
subplot(223);imshow(c);title('addition image')
13

HOW TO ADD TWO IMAGES


a = imread('Smith.JPG'); % Reads the first image ('Smith.JPG') into variable 'a'.
b = imread('Cummins.JPG'); % Reads the second image ('Cummins.JPG') into
variable 'b'.
c = imadd(a, b); % Performs pixel-wise addition of the two images and stores
the result in 'c’.
subplot(221); imshow(a); title('1st image') % Displays the first image in the top-left
position.

subplot(222); imshow(b); title('2nd image') % Displays the second image in the


top-right position.
subplot(223); imshow(c); title('addition image') % Displays the resulting image
at the bottom-left position.
14

HOW TO ADD TWO IMAGES


 This MATLAB script reads two images, performs pixel-wise addition, and
displays the original images alongside their addition result using subplots
in a single figure window.
 Below is the detailed description:
Objective:
 To load two images, add them together using MATLAB's iamadd() function,
and display the original images along with the result of their addition.
Usage:
 This script demonstrates how to perform and visualize pixel-wise
operations on images.
 It is useful in:
• Image processing and analysis.
• Understanding image arithmetic operations.
15

HOW TO ADD TWO IMAGES


Prerequisites:
The files Smith.JPG and Cummins.JPG must exist in the current
MATLAB working directory or their full paths should be specified.
The two images must have the same dimensions for pixel-wise
addition.

Customization:
Replace Smith.JPG and Cummins.JPG with other image files to
test with different inputs.
Modify the operation (e.g., imsubtract, immultiply) to explore
other arithmetic operations on images.
16

RGB TO GRAY SCALE


b=imread('Smith.JPG');
figure
subplot(121)
imshow(b)
subplot(122)
I=rgb2gray(b);
imshow(I)
17

RGB TO GRAY SCALE


b = imread('Smith.JPG’); % Reads the color image file 'Smith.JPG' into
variable 'b'.
figure % Opens a new figure window for displaying
the images.
subplot(121) % Creates the first subplot (1 row, 2 columns,
1st position).
imshow(b) % Displays the original color image stored in
'b'.
I = rgb2gray(b); % Converts the color image 'b' to grayscale
and stores it in 'I'.
subplot(122) % Creates the second subplot (1 row, 2
columns, 2nd position).
imshow(I) % Displays the grayscale image stored in 'I'.
18

RGB TO GRAY SCALE


 This MATLAB script reads a color image, converts it to grayscale,
and displays both the original and grayscale images side-by-side in
a single figure window using subplots.
 Below is the detailed description of the file:
Objective:
 To load a color image (Smith.JPG), convert it to a grayscale image,
and display both the original and grayscale images for comparison.
Usage:

This script demonstrates how to:


 Convert a color image to grayscale.
 Compare the original image with its grayscale representation.

It is commonly used in image processing tasks where grayscale


images are preferred for computational efficiency.
19

RGB TO GRAY SCALE


Prerequisites:
The file Smith.JPG must exist in the current MATLAB working directory
or its full path should be provided.
MATLAB's Image Processing Toolbox is required for the rgb2gray
function and imshow.

Customization:
Replace Smith.JPG with another image file to test with different inputs.
Add titles to the subplots for better clarity

• title('Original Image') % For the first subplot.


• title('Grayscale Image') % For the second subplot.

You might also like