Dip 6
Dip 6
Bhopal
1
INDEX
Seria Assignment Date of Date of Remarks
l 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:- Introduction To Matlab commands and functions 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');
operations on
binary images
thinned
7. watershed Performs [labels,num] =
watershed watershed(img);
segmentation
8. imfill Fills holes in a filled_img = imfill(img,
binary image filled 'holes');
9. regionprops Measures Props = regionprops
properties 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 detection
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
operation
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 dilation 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
Objective:- Get knowledge read and display commands and functions
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 function 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 function to display the image in a
figure window.
4. Get image information: (Optional) Use the imfinfo function to get
information about the image, such as its format, size, and color depth.
5. Display image information: (Optional) Use the disp function to display
the image information in the command window.
Code:-
7
Mathematical Expression:-
Original image:-
8
Output:-
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
function in matlab
Objective
The objective 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 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.
Color Space: A mathematical 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 converting an RGB image to a grayscale
image by calculating 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 multiple subplots,
allowing for the display of multiple 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 plotting.
MATLAB Functions:
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.
title: Sets the title for a plot.
Algorithm:
1. Read the image: Use the imread function to read the image specified by
the image_path.
2. Convert to grayscale: Use the rgb2gray function to convert the RGB
image to grayscale.
3. Create a figure: Use the figure function to create a new figure window.
4. Create subplots: Use the subplot function to divide the figure window
into a 1x2 grid.
5. Display the original image: Use the imshow function to display the
original image in the first subplot.
6. Display the grayscale image: Use the imshow function to display the
grayscale image in the second subplot.
7. Set titles: Use the title function to set the titles for each subplot.
Code:-
function 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);
title('Original Image');
subplot(1, 2, 2);
imshow(gray_img);
title('Grayscale Image');
end
grayscale_and_display(“hindi.jpg”)
Mathematical Expression:-
Original image:-
Output:-
13
Conclusion:
The provided MATLAB program successfully demonstrates the process of
converting 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 applications.
Tutorial:
Question: How can we adjust the brightness of the grayscale image before
displaying it?
Solution:-
function 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 (multiply 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);
title('Original Image');
subplot(1, 2, 2);
imshow(adjusted_gray_img, []); % Display with automatic scaling
title('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?
Solution:-
function 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);
title('Original Image');
subplot(1, 2, 2);
imshow(filtered_gray_img, []); % Display with automatic scaling
title('Grayscale Image (Filtered)');
end
grayscale_and_display(“hindi.jpg”,1.5)
16
EXPERIMENT-4
AIM:- Write a program for histogram Calculation and equalization.
(a) standard matlab function
(B) enhancing contrast using histogram equalizer.
Objective:
The objective 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 representation of the distribution of pixel
intensities in an image.
It shows the frequency of occurrence of each intensity level.
A flat histogram indicates a wide range of pixel intensities, while a
peaked histogram suggests a concentrated distribution of intensities.
Histogram Equalization:
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 intensities to create a more uniform distribution.
This can improve the visibility of details in the image.
Steps involved in histogram equalization:
1. Calculate the histogram: Determine the frequency of each pixel intensity
level.
2. Compute the cumulative distribution function (CDF): Calculate the
cumulative sum of the histogram.
17
3. Map pixel intensities: Map each pixel intensity to a new intensity based
on the CDF.
4. Rescale: Normalize the mapped intensities to the desired range (usually
0-255 for 8-bit images).
MATLAB Functions:
imhist: Calculates the histogram of an image.
histeq: Equalizes the histogram of an image using the standard method.
cumsum: Calculates the cumulative sum of an array.
uint8: Converts a double array to an 8-bit unsigned integer array.
A. Using Standard MATLAB Function
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 function 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 cumulative distribution function (CDF): Calculate the
cumulative sum of the histogram.
Map pixel intensities: Map each pixel intensity to a new intensity based
on the CDF.
18
Rescale: Normalize the mapped intensities 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) function histogram_and_equalization(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);
title('Original Image');
subplot(1, 3, 2);
bar(histogram);
title('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
title('Equalized Image');
end
histogram_and_equalization('hindi.jpg');
(b) function histogram_and_equalization(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 cumulative distribution function (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);
title('Original Grayscale Image');
subplot(1, 3, 2);
bar(histogram);
title('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
title('Equalized Image');
end
% Example usage:
histogram_and_equalization('hindi.jpg');
Mathematical Expression:-
(a)
20
(b)
Original image:-
21
Output:-
(b)
22
Conclusion:
The provided MATLAB program effectively demonstrates the process of
calculating the histogram of an image and applying histogram equalization to
enhance its contrast. By understanding the principles of histograms and
equalization, we can improve the visual quality of images, making them more
informative and easier to analyze.
Tutorial:
Q1: How can we apply adaptive histogram equalization (AHE) to the image
instead of global histogram equalization?
Solution:-
function histogram_and_equalization(image_path)
23
% Read the image
img = imread(image_path);
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Apply adaptive histogram equalization
equalized_img = adapthisteq(gray_img);
% Display the original, histogram, and equalized images
figure;
subplot(1, 3, 1);
imshow(gray_img);
title('Original Grayscale Image');
subplot(1, 3, 2);
imhist(gray_img);
title('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
title('Equalized Image (AHE)');
end
% Example usage:
histogram_and_equalization('hindi.jpg');
Q2: How can we apply a gamma correction to the image before histogram
equalization?
Solution:-
function histogram_and_equalization(image_path, gamma)
% Read the image
img = imread(image_path);
24
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Apply gamma correction
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);
title('Original Grayscale Image');
subplot(1, 3, 2);
imhist(gray_img);
title('Histogram');
subplot(1, 3, 3);
imshow(equalized_img);
title('Equalized Image (Gamma Corrected)');
end
% Example usage:
histogram_and_equalization('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 negative of one of the image. (Subtract 255 from the image) and
display
Objective:
The objective of this MATLAB program is to:
1. Read two input images.
2. Display the original images.
3. Perform basic image operations:
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 negative of one of the images.
4. Display the results of each operation.
These operations are fundamental in image processing and can be used as
building blocks for more complex tasks.
Theory
Image Processing:
26
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.
Image Operations:
Addition: Adding corresponding pixel values of two images to create a
new image. The result is a brighter image if the values are positive and a
darker image if the values are negative.
Subtraction: Subtracting corresponding pixel values of two images. This
can be used to find differences between images or to create a negative
image.
Constant Addition/Subtraction: Adding or subtracting a constant value
to each pixel of an image. This can be used to adjust the overall
brightness or contrast.
Negative: Inverting the pixel values of an image. This creates a
complementary image where dark areas become light and vice versa.
MATLAB Functions:
imread: Reads an image file into a MATLAB matrix.
imshow: Displays an image in a figure window.
subplot: Creates subplots within a figure window.
title: Sets the title for a plot.
+: Addition operator.
-: Subtraction operator.
+: Addition operator for scalar values.
-: Subtraction 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 titles for each subplot using title.
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 negative:
o Subtract each pixel value from 255 to get the negative of the first
image.
8. Display the results:
o Display the added, subtracted, constant-added, constant-
subtracted, and negative images in the remaining subplots using
imshow and appropriate titles.
Code:-
function image_operations(image1_path, image2_path)
% Read the images
28
image1 = imread(image1_path);
image2 = imread(image2_path);
29
% Subtract a constant value of 100 from image2
subtracted_constant_image2 = image2 - 100;
subplot(2, 3, 6);
imshow(subtracted_constant_image2);
title('Image 2 - 100');
% Obtain the negative of image1
negative_image1 = 255 - image1;
figure;
imshow(negative_image1);
title('Negative of Image 1');
end
% Example usage:
image_operations('hindi.jpg', 'english.jpg');
Mathematical Expression:-
Original Image:-
30
Output:-
Negative of image 1
31
Conclusion:
The provided MATLAB program effectively demonstrates various image
operations, including addition, subtraction, constant manipulation, and
obtaining the negative of an image. These operations are fundamental building
blocks for many image processing tasks and can be combined and extended to
achieve more complex image transformations.
Tutorial:-
Q1: How can we apply a Gaussian blur to one of the images before performing
the operations?
Solution:-
32
function image_operations(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_operations('image1.jpg', 'image2.jpg', 2);
Q2: How can we perform element-wise multiplication and division of the two
images?
Solution:-
function image_operations(image1_path, image2_path)
% Read the images
image1 = imread(image1_path);
image2 = imread(image2_path);
% ... (other operations)
% Multiply the images element-wise
multiplied_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 multiplied and divided images
figure;
imshow(multiplied_image);
title('Multiplied Images');
33
figure;
imshow(divided_image);
title('Divided Images');
end
34
35