Experiment No : 1
Aim:
To get familiarized with the MATLAB Image processing toolbox.
1. To explore the basic features and functions available in the MATLAB Image Processing Toolbox.
2. To perform common image operations such as resizing, cropping, and grayscale conversion.
3. To analyze and display images using different MATLAB functions.
Software Used:
MATLAB – Used for implementing and executing the image processing operations.
MATLAB Image Processing Toolbox – Used for reading, displaying, manipulating, and
analyzing digital images.
Theory:
MATLAB provides the Image Processing Toolbox, which contains various functions for reading,
displaying, modifying and analyzing digital images. In this experiment, basic image processing operations
are performed on sample images such as coins.png, peacock.jpg, and peppers.png.
A digital image is represented in MATLAB as a matrix of pixel values. A grayscale image contains a
single intensity value for each pixel, while a color image generally contains three channels: Red, Green
and Blue (RGB). The type of image determines which operations can be directly applied to it.
The main functions used in this experiment are given below.
1. Reading an Image – imread()
The imread() function is used to read an image file and store it in a MATLAB variable.
img = imread("peppers.png");
The same function can be used to read coins.png and peacock.jpg.
2. Displaying an Image – imshow()
The imshow() function is used to display an image in the MATLAB figure window.
imshow(img);
It can be used to display both grayscale and color images.
3. Resizing an Image – imresize()
The imresize() function is used to change the size of an image. A scale factor can be used to increase or
decrease the image size.
resized_img = imresize(img, 0.5);
Here, the image is reduced to 50% of its original size.
4. Cropping an Image – imcrop()
The imcrop() function is used to extract a particular region from an image.
cropped_img = imcrop(img, [10, 20, 150, 160]);
The values specify the position and size of the region to be cropped.
5. RGB to Grayscale Conversion – rgb2gray()
Color images such as peppers.png and peacock.jpg contain three RGB channels. The rgb2gray() function
converts a color image into a grayscale image with a single intensity channel.
gray_img = rgb2gray(img);
The resulting image contains different shades of gray according to the intensity of the pixels.
For an image that is already grayscale, such as coins.png, this conversion is not required.
6. Conversion to Black-and-White Image – im2bw()
The im2bw() function is used to convert an image into a binary or black-and-white image. The conversion
is based on a threshold value.
bw_img = im2bw(gray_img);
The pixels are divided into two groups, resulting in black and white regions.
7. Image Information – imfinfo()
The imfinfo() function is used to obtain information about an image file.
info = imfinfo("peppers.png");
disp(info);
It provides information such as image format, width, height, bit depth, file size and color type.
8. Image Tool – imtool()
The imtool() function opens an image in an interactive Image Tool window.
imtool(img);
It allows the image to be viewed closely and helps in examining different regions and pixel information.
9. Image Histogram – imhist()
The imhist() function is used to display the histogram of an image. A histogram shows the distribution of
pixel intensity values.
imhist(img);
For a grayscale image, the horizontal axis represents the intensity values and the vertical axis represents
the number of pixels at each intensity level. For a color image, the histogram represents the intensity
distribution of its color channels.
Procedure / Algorithm Steps:
1. First, MATLAB is opened and a new script is created for performing the experiment.
2. The given sample images such as coins.png, peacock.jpg, and peppers.png are loaded into
MATLAB using the imread() function.
3. The original image is displayed using the imshow() function so that its appearance can be observed
before applying any processing.
4. The size of the image is reduced to half of its original size using the imresize() function. This helps
to observe how resizing changes the image dimensions.
5. A particular portion of the image is selected and cropped using the imcrop() function.
6. The color images are converted into grayscale images using the rgb2gray() function. The grayscale
image contains different shades of gray instead of the original colors.
7. The image is then converted into a black-and-white image using the im2bw() function. This gives
an image containing mainly black and white regions.
8. The basic details of the image, such as its size, format, and bit depth, are obtained using the
imfinfo() function.
9. The imtool() function is used to open the image separately and examine it more closely.
10. The original image and the processed images are arranged together using subplot() so that their
differences can be easily compared.
11. Finally, the histograms of the color and grayscale images are plotted using the imhist() function to
observe the distribution of pixel intensity values.
12. The same operations are performed on the given sample images, and the outputs are observed and
recorded.
Results: 1.Peppers.png
2. Peacock.jpg
3.Coins.png
Discussion:
The basic functions of the MATLAB Image Processing Toolbox were studied by applying
different operations on the given images. This helped in understanding how MATLAB can be used
for basic image processing.
The imread() function was used to load the images into MATLAB, and imshow() was used to view
the original images. This made it easier to compare the image before and after applying different
operations.
The resizing operation using imresize() changed the dimensions of the image. In the experiment,
a scale factor of 0.5 was used, so the image was reduced to half of its original size without changing
its overall appearance.
The imcrop() function was used to extract a particular part of the image. By giving the required
coordinates, only the selected region was obtained from the original image.
The images used in the experiment were not all of the same type. peacock.jpg and peppers.png are
color images, whereas coins.png is already a grayscale image. This difference was important while
applying the image processing functions.
When rgb2gray() was applied to a color image, it converted the three RGB channels into a single
grayscale image. However, applying rgb2gray() to coins.png was not required because it was
already grayscale, and MATLAB may give an error when a grayscale image is given to this
function.
The im2bw() function was used to convert the image into a black-and-white form. It separates the
pixels based on a threshold, so the resulting image contains black and white regions instead of
different intensity levels.
The imfinfo() function was useful for checking the properties of each image. It provided
information such as the image format, width, height, bit depth and other file details.
The imtool() function provided an interactive way to view the image. It was useful for zooming
into the image and observing the image details more clearly than the normal display.
The histograms obtained using imhist() showed the distribution of pixel intensity values. The shape
of the histogram was different for different images because each image has a different arrangement
of bright, dark and intermediate intensity regions.
Conclusion:
The experiment helped us to understand the basic features of the MATLAB Image Processing Toolbox
and their use in processing digital images. Different operations such as reading, displaying, resizing,
cropping, grayscale and black-and-white conversion were successfully performed.
Reference:
MATLAB Documentation : https://www.mathworks.com/help/images/
R. C. Gonzalez and R. E. Woods, Digital Image Processing, 4th Edition, Pearson, 2018.
Code:
1.Code for Peacock.jpg
clc;
clear;
close all;
% Read the image
img = imread("peacock.jpg");
% Resize the image
resized_img = imresize(img, 0.5);
% Crop the image
cropped_img = imcrop(img, [10,20,150,160]);
% Convert RGB image to grayscale
gray_img = rgb2gray(img);
% Convert image to black and white
bw_img = im2bw(img);
% Display image using Image Tool
imtool(img);
% Display images and histograms
figure;
subplot(3,2,1);
imshow(img);
title('Original Image');
subplot(3,2,2);
imshow(resized_img);
title('Resized Image');
subplot(3,2,3);
imshow(cropped_img);
title('Cropped Image');
subplot(3,2,4);
imshow(gray_img);
title('Grayscale Image');
subplot(3,2,5);
imshow(bw_img);
title('B&W Image');
figure
subplot(1,2,1);
imhist(img);
title('Color Image Histogram');
subplot(1,2,2);
imhist(gray_img);
title('Grayscale Histogram');
2. Code for peppers.png
clc;
clear;
close all;
% Read the image
img = imread("peppers.png");
% Resize the image
resized_img = imresize(img, 0.5);
% Crop the image
cropped_img = imcrop(img, [10,20,150,160]);
% Convert RGB image to grayscale
gray_img = rgb2gray(img);
% Convert image to black and white
bw_img = im2bw(img);
% Display image using Image Tool
imtool(img);
% Display images and histograms
figure;
subplot(3,2,1);
imshow(img);
title('Original Image');
subplot(3,2,2);
imshow(resized_img);
title('Resized Image');
subplot(3,2,3);
imshow(cropped_img);
title('Cropped Image');
subplot(3,2,4);
imshow(gray_img);
title('Grayscale Image');
subplot(3,2,5);
imshow(bw_img);
title('B&W Image');
figure
subplot(1,2,1);
imhist(img);
title('Color Image Histogram');
subplot(1,2,2);
imhist(gray_img);
title('Grayscale Histogram');
3.code for coins.png
clc;
clear;
close all;
% Read the image
img = imread("coins.png");
% Resize the image
resized_img = imresize(img, 0.5);
% Crop the image
cropped_img = imcrop(img, [10, 20, 150, 160]);
% Convert image to grayscale
gray_img = img;
% Convert image to black and white
bw_img = im2bw(gray_img);
% Display image using Image Tool
imtool(img);
% Display images
figure;
subplot(3,2,1);
imshow(img);
title('Original Image');
subplot(3,2,2);
imshow(resized_img);
title('Resized Image');
subplot(3,2,3);
imshow(cropped_img);
title('Cropped Image');
subplot(3,2,4);
imshow(gray_img);
title('Grayscale Image');
subplot(3,2,5);
imshow(bw_img);
title('B&W Image');
% Display histograms
figure;
subplot(1,2,1);
imhist(gray_img);
title('Grayscale Histogram');
subplot(1,2,2);
imhist(img);
title('Original Image Histogram');