0% found this document useful (0 votes)
26 views34 pages

Lab Pro

lab file

Uploaded by

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

Lab Pro

lab file

Uploaded by

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

Indian Ins tute of Informa on Technology

Bhopal

Digital Image Processing


DEPARTMENT- CSE (312)

5th SEMESTER

Submitted By: Harshit Mahour Submitted To:


Scholar No: 22U02046 Dr. Bhupendra Singh
kirar

1
INDEX
Serial Assignment Date of Date of Remarks
No. Performance Submission

1. Experiment-1 7-8-
2024
2. Experiment-2 7-8-
2024
3. Experiment-3 7-8-
2024
4. Experiment-4 21-8-
2024
5. Experiment-5 28-8-
2024
6. Experiment-6

7. Experiment-7

8. Experiment-8

2
EXPERIMENT-1
Aim:- Introduc on To Matlab commands and func ons used in digital image
processing

3
TABLE FOR COMMANDS

4
S.NO. Command Name Feature Example
1. Imread Reads an image img =
file into the imread('image.jpg');
workspace
2. Imwrite Writes an image to imwrite(img,
a file 'new_image.png');
3. rgb2gray Converts an RGB img = rgb2gray(img);
image to
grayscalegray
4. imresize Resizes an image img = imresize(img,
resized [256 256]);
5. edge Detects edges in edges = edge(img,
an image 'canny');
6. bwmorph Performs Img =
morphological bwmorph(img,'thin');
opera ons on
binary images
thinned
7. watershed Performs [labels,num] =
watershed watershed(img);
segmenta on
8. imfill Fills holes in a filled_img = imfill(img,
binary image filled 'holes');
9. regionprops Measures Props = regionprops
proper es of (labeled_img,'area',
connected 'centroid');
components
10. Imshow Displays an image imshow(img);
in a figure window
11. imcrop Crops an img = imcrop(img, [100
imagecropped 100 200 200]);
12. hough Performs Hough [H,theta,rho] =
transform for line hough(img);
or circle detec on
13. bwlabel Labels connected [labeled_img, num]
components in a =bwlabel(img);
binary image
14. strel Creates a Sse = strel('disk',5);
structuring
element for
morphological
opera on
15. imerode Performs erosion img = imerode(img, se);
on a binary image
eroded
16. imopen Performs opening Img = imopen(img, se);
on a binary image
opened

5
Finds the
bwperim perimeter of perim = bwperim (img);
objects in a binary
image
17. bwboundary Finds the boundary =
boundary of bwboundary(img);
objects in a binary
image
18. imdilate Performs dila on img = imdilate(img, se);
on a binary image
dilated
19. imclose Performs closing img = imclose(img, se);
on a binary
imageclosed

6
EXPERIMENT-2
Aim:- Write program for read and display an images in MATLAB
Objec ve:- Get knowledge read and display commands and func ons
Theory:
Image Processing:
 Image: A digital representation of a visual scene, typically stored as a 2D
array of pixels.
 Pixel: The smallest unit of an image, representing a single point in the
image.
 Pixel Value: A numerical value associated with a pixel, representing its
intensity or color.
MATLAB Functions:
 imread: Reads an image file into a MATLAB matrix.
 imshow: Displays an image in a figure window.

Algorithm
1. Choose an image file: Select the image file you want to read and display.
2. Read the image: Use the imread func on to read the image file into a
MATLAB matrix. The matrix will contain the pixel values of the image.
3. Display the image: Use the imshow func on to display the image in a
figure window.
4. Get image informa on: (Op onal) Use the imfinfo func on to get
informa on about the image, such as its format, size, and color depth.
5. Display image informa on: (Op onal) Use the disp func on to display
the image informa on in the command window.
Code:-

7
Mathema cal Expression:-

Original image:-

8
Output:-

Conclusion:- This MATLAB program effec vely reads and displays an image. It
provides a basic framework for image processing tasks
Tutorial:-
Q1: How can we convert the displayed image to grayscale and display the
grayscale image?
Sol:-
% Read the image file
image = imread('your_image.jpg');

9
% Convert the image to grayscale
gray_image = rgb2gray(image);
% Display the grayscale image
imshow(gray_image);

Q2: How can we resize the displayed image to a specific size and display the
resized image?
Sol:- % Read the image file
image = imread('your_image.jpg');
% Resize the image to 256x256 pixels
resized_image = imresize(image, [256 256]);
% Display the resized image
imshow(resized_image);

10
EXPERIMENT-3
AIM:- write a program to gray scale images and display images using Sub plot
func on in matlab
Objec ve
The objec ve of this program is to:
 Read an image file.
 Convert the image to grayscale.
 Display both the original and grayscale images side-by-side using subplots
in MATLAB.
Theory
Image Processing:
 Image: A digital representa on of a visual scene, typically stored as a 2D
array of pixels.
 Pixel: The smallest unit of an image, represen ng a single point in the
image.
 Color Space: A mathema cal model that describes how colors can be
represented. Common color spaces include RGB (Red, Green, Blue) and
grayscale.
Grayscale Conversion:
 RGB to Grayscale: The process of conver ng an RGB image to a grayscale
image by calcula ng the average intensity of the red, green, and blue
channels for each pixel.
 Intensity: A measure of the brightness or luminance of a pixel.
Subplots:
 Subplots: A way to divide a figure window into mul ple subplots, allowing
for the display of mul ple images or plots within a single figure.
 subplot(m,n,p): Creates a subplot grid with m rows and n columns, and
selects the p-th subplot for plo ng.
MATLAB Func ons:

11
 imread: Reads an image file into a MATLAB matrix.
 rgb2gray: Converts an RGB image to grayscale.
 imshow: Displays an image in a figure window.
 figure: Creates a new figure window.
 subplot: Creates subplots within a figure window.
 tle: Sets the tle for a plot.
Algorithm:
1. Read the image: Use the imread func on to read the image specified by
the image_path.
2. Convert to grayscale: Use the rgb2gray func on to convert the RGB image
to grayscale.
3. Create a figure: Use the figure func on to create a new figure window.
4. Create subplots: Use the subplot func on to divide the figure window into
a 1x2 grid.
5. Display the original image: Use the imshow func on to display the
original image in the first subplot.
6. Display the grayscale image: Use the imshow func on to display the
grayscale image in the second subplot.
7. Set tles: Use the tle func on to set the tles for each subplot.

Code:-
func on grayscale_and_display(myfamily.jpg)
% Read the image
img = imread(english.jpg);
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Display the original and grayscale images using subplot
figure;

12
subplot(1, 2, 1);
imshow(img);
tle('Original Image');
subplot(1, 2, 2);
imshow(gray_img);
tle('Grayscale Image');
end
grayscale_and_display(“hindi.jpg”)
Mathema cal Expression:-

Original image:-

Output:-

13
Conclusion:
The provided MATLAB program successfully demonstrates the process of
conver ng an RGB image to grayscale and displaying both the original and
grayscale images side-by-side using subplots. This is a fundamental image
processing technique that can be applied to various tasks, such as image analysis,
image enhancement, and computer vision applica ons.
Tutorial:
Ques on: How can we adjust the brightness of the grayscale image before
displaying it?
Solu on:-
func on grayscale_and_display(image_path, brightness_factor)
% Read the image
img = imread(image_path);

14
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Adjust brightness (mul ply by a factor)
adjusted_gray_img = gray_img * brightness_factor;
% Display the original and grayscale images using subplot
figure;
subplot(1, 2, 1);
imshow(img);
tle('Original Image');
subplot(1, 2, 2);
imshow(adjusted_gray_img, []); % Display with automa c scaling
tle('Grayscale Image (Adjusted Brightness)');
end
grayscale_and_display(“hindi.jpg”,1.5)
Q2: How can we apply a Gaussian filter to the grayscale image before displaying
it?
Solu on:-
func on grayscale_and_display(image_path, sigma)
% Read the image
img = imread(image_path);
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Apply Gaussian filtering
filtered_gray_img = imgaussfilt(gray_img, sigma);
% Display the original and grayscale images using subplot
figure;

15
subplot(1, 2, 1);
imshow(img);
tle('Original Image');
subplot(1, 2, 2);
imshow(filtered_gray_img, []); % Display with automa c scaling
tle('Grayscale Image (Filtered)');
end
grayscale_and_display(“hindi.jpg”,1.5)

16
EXPERIMENT-4
AIM:- Write a program for histogram Calcula on and equaliza on.
(a) standard matlab func on
(B) enhancing contrast using histogram equalizer.
Objec ve:
The objec ve of this program is to:
1. Calculate the histogram of an image.
2. Equalize the histogram to enhance contrast.
3. Display the original image, its histogram, and the equalized image.
Theory
Histogram:
 A histogram is a graphical representa on of the distribu on of pixel
intensi es in an image.
 It shows the frequency of occurrence of each intensity level.
 A flat histogram indicates a wide range of pixel intensi es, while a
peaked histogram suggests a concentrated distribu on of intensi es.
Histogram Equaliza on:
 A technique used to enhance the contrast of an image by stretching its
histogram to cover the full range of possible pixel values.
 It redistributes the pixel intensi es to create a more uniform distribu on.
 This can improve the visibility of details in the image.
Steps involved in histogram equaliza on:
1. Calculate the histogram: Determine the frequency of each pixel intensity
level.
2. Compute the cumula ve distribu on func on (CDF): Calculate the
cumula ve sum of the histogram.

17
3. Map pixel intensi es: Map each pixel intensity to a new intensity based
on the CDF.
4. Rescale: Normalize the mapped intensi es to the desired range (usually
0-255 for 8-bit images).
MATLAB Func ons:
 imhist: Calculates the histogram of an image.
 histeq: Equalizes the histogram of an image using the standard method.
 cumsum: Calculates the cumula ve sum of an array.
 uint8: Converts a double array to an 8-bit unsigned integer array.
A. Using Standard MATLAB Func on
Algorithm:
 Read the image: Load the image file using imread.
 Calculate the histogram: Use imhist to compute the histogram of the
image.
 Equalize the histogram: Use the built-in histeq func on to equalize the
histogram.
 Display the images: Show the original image, its histogram, and the
equalized image using subplots.
B. Enhancing Contrast Using Histogram Equalizer
Algorithm:
 Read the image: Load the image file using imread.
 Convert to grayscale: Convert the image to grayscale if necessary.
 Calculate the histogram: Determine the frequency of each pixel intensity
level.
 Compute the cumula ve distribu on func on (CDF): Calculate the
cumula ve sum of the histogram.
 Map pixel intensi es: Map each pixel intensity to a new intensity based
on the CDF.

18
 Rescale: Normalize the mapped intensi es to the desired range (usually
0-255 for 8-bit images).
 Display the images: Show the original image, its histogram, and the
equalized image using subplots.
Code:-
(a) func on histogram_and_equaliza on(image_path)
% Read the image
img = imread(image_path);
% Calculate the histogram
histogram = imhist(img);
% Equalize the histogram
equalized_img = histeq(img);
% Display the original, histogram, and equalized images
figure;
subplot(1, 3, 1);
imshow(img);
tle('Original Image');
subplot(1, 3, 2);
bar(histogram);
tle('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
tle('Equalized Image');
end
histogram_and_equaliza on('hindi.jpg');
(b) func on histogram_and_equaliza on(image_path)
% Read the image

19
img = imread(image_path);
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Calculate the histogram
histogram = imhist(gray_img);
% Calculate the cumula ve distribu on func on (CDF)
cdf = cumsum(histogram) / numel(gray_img);
% Map the gray levels to the CDF
equalized_img = uint8(255 * cdf(gray_img + 1));
% Display the original, histogram, and equalized images
figure;
subplot(1, 3, 1);
imshow(gray_img);
tle('Original Grayscale Image');
subplot(1, 3, 2);
bar(histogram);
tle('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
tle('Equalized Image');
end
% Example usage:
histogram_and_equaliza on('hindi.jpg');
Mathema cal Expression:-
(a)

20
(b)

Original image:-

21
Output:-

(b)

22
Conclusion:
The provided MATLAB program effec vely demonstrates the process of
calcula ng the histogram of an image and applying histogram equaliza on to
enhance its contrast. By understanding the principles of histograms and
equaliza on, we can improve the visual quality of images, making them more
informa ve and easier to analyze.
Tutorial:
Q1: How can we apply adap ve histogram equaliza on (AHE) to the image
instead of global histogram equaliza on?
Solu on:-
func on histogram_and_equaliza on(image_path)

23
% Read the image
img = imread(image_path);
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Apply adap ve histogram equaliza on
equalized_img = adapthisteq(gray_img);
% Display the original, histogram, and equalized images
figure;
subplot(1, 3, 1);
imshow(gray_img);
tle('Original Grayscale Image');
subplot(1, 3, 2);
imhist(gray_img);
tle('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
tle('Equalized Image (AHE)');
end
% Example usage:
histogram_and_equaliza on('hindi.jpg');
Q2: How can we apply a gamma correc on to the image before histogram
equaliza on?
Solu on:-
func on histogram_and_equaliza on(image_path, gamma)
% Read the image
img = imread(image_path);

24
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Apply gamma correc on
gamma_corrected_img = imadjust(gray_img, [], [], gamma);
% Equalize the histogram
equalized_img = histeq(gamma_corrected_img);
% Display the original, histogram, and equalized images
figure;
subplot(1, 3, 1);
imshow(gray_img);
tle('Original Grayscale Image');
subplot(1, 3, 2);
imhist(gray_img);
tle('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
tle('Equalized Image (Gamma Corrected)');
end
% Example usage:
histogram_and_equaliza on('hindi.jpg', 0.5);

25
EXPERIMENT-5
Aim:- Write a MATLAB program to perform the following.
1) Read 2 images image and display the image
2) Add the two images and display.
3) Subtract the two images and display
4) Add a constant value of 50 to one of the images and display
5) Subtract a constant value 100 from one of the images and display.
6) Obtain nega ve of one of the image. (Subtract 255 from the image) and
display
Objec ve:
The objec ve of this MATLAB program is to:
1. Read two input images.
2. Display the original images.
3. Perform basic image opera ons:
o Add the two images pixel-wise.
o Subtract the second image from the first.
o Add a constant value to one of the images.
o Subtract a constant value from one of the images.
o Obtain the nega ve of one of the images.
4. Display the results of each opera on.
These opera ons are fundamental in image processing and can be used as
building blocks for more complex tasks.
Theory
Image Processing:

26
 Image: A digital representa on of a visual scene, typically stored as a 2D
array of pixels.
 Pixel: The smallest unit of an image, represen ng a single point in the
image.
 Pixel Value: A numerical value associated with a pixel, represen ng its
intensity or color.
Image Opera ons:
 Addi on: Adding corresponding pixel values of two images to create a
new image. The result is a brighter image if the values are posi ve and a
darker image if the values are nega ve.
 Subtrac on: Subtrac ng corresponding pixel values of two images. This
can be used to find differences between images or to create a nega ve
image.
 Constant Addi on/Subtrac on: Adding or subtrac ng a constant value to
each pixel of an image. This can be used to adjust the overall brightness
or contrast.
 Nega ve: Inver ng the pixel values of an image. This creates a
complementary image where dark areas become light and vice versa.
MATLAB Func ons:
 imread: Reads an image file into a MATLAB matrix.
 imshow: Displays an image in a figure window.
 subplot: Creates subplots within a figure window.
 tle: Sets the tle for a plot.
 +: Addi on operator.
 -: Subtrac on operator.
 +: Addi on operator for scalar values.
 -: Subtrac on operator for scalar values.
Algorithm
1. Read the images:

27
o Use imread to read the two input image files.
2. Display the original images:
o Create a figure window using figure.
o Use subplot to divide the figure into subplots.
o Display the first image in the first subplot using imshow.
o Display the second image in the second subplot using imshow.
o Set appropriate tles for each subplot using tle.
3. Add the images:
o Add the corresponding pixel values of the two images element-
wise.
4. Subtract the images:
o Subtract the corresponding pixel values of the second image from
the first.
5. Add a constant value:
o Add a constant value (e.g., 50) to each pixel of the first image.
6. Subtract a constant value:
o Subtract a constant value (e.g., 100) from each pixel of the second
image.
7. Obtain the nega ve:
o Subtract each pixel value from 255 to get the nega ve of the first
image.
8. Display the results:
o Display the added, subtracted, constant-added, constant-
subtracted, and nega ve images in the remaining subplots using
imshow and appropriate tles.
Code:-
func on image_opera ons(image1_path, image2_path)
% Read the images

28
image1 = imread(image1_path);
image2 = imread(image2_path);

% Display the images


figure;
subplot(2, 3, 1);
imshow(image1);
tle('Image 1');
subplot(2, 3, 2);
imshow(image2);
tle('Image 2');
% Add the images
added_image = image1 + image2;
subplot(2, 3, 3);
imshow(added_image);
tle('Added Images');
% Subtract the images
subtracted_image = image1 - image2;
subplot(2, 3, 4);
imshow(subtracted_image);
tle('Subtracted Images');
% Add a constant value of 50 to image1
added_constant_image1 = image1 + 50;
subplot(2, 3, 5);
imshow(added_constant_image1);
tle('Image 1 + 50');

29
% Subtract a constant value of 100 from image2
subtracted_constant_image2 = image2 - 100;
subplot(2, 3, 6);
imshow(subtracted_constant_image2);
tle('Image 2 - 100');
% Obtain the nega ve of image1
nega ve_image1 = 255 - image1;
figure;
imshow(nega ve_image1);
tle('Nega ve of Image 1');
end
% Example usage:
image_opera ons('hindi.jpg', 'english.jpg');
Mathema cal Expression:-

Original Image:-

30
Output:-

Nega ve of image 1

31
Conclusion:
The provided MATLAB program effec vely demonstrates various image
opera ons, including addi on, subtrac on, constant manipula on, and
obtaining the nega ve of an image. These opera ons are fundamental building
blocks for many image processing tasks and can be combined and extended to
achieve more complex image transforma ons.
Tutorial:-
Q1: How can we apply a Gaussian blur to one of the images before performing
the opera ons?
Solu on:-

32
func on image_opera ons(image1_path, image2_path, sigma)
% Read the images
image1 = imread(image1_path);
image2 = imread(image2_path);
% Apply Gaussian blur to image1
blurred_image1 = imgaussfilt(image1, sigma);
% ... (rest of the code remains the same)
end
image_opera ons('image1.jpg', 'image2.jpg', 2);
Q2: How can we perform element-wise mul plica on and division of the two
images?
Solu on:-
func on image_opera ons(image1_path, image2_path)
% Read the images
image1 = imread(image1_path);
image2 = imread(image2_path);
% ... (other opera ons)
% Mul ply the images element-wise
mul plied_image = image1 .* image2;
% Divide the images element-wise (avoiding division by zero)
divided_image = image1 ./ image2;
divided_image(isnan(divided_image)) = 0; % Replace NaN values with 0
% Display the mul plied and divided images
figure;
imshow(mul plied_image);
tle('Mul plied Images');

33
figure;
imshow(divided_image);
tle('Divided Images');
end

34

You might also like