Digital Image Processing (N-PECET502P)
S. B. JAIN INSTITUTE OF TECHNOLOGY, MANAGEMENT &
RESEARCH, NAGPUR.
Practical No. 02
Aim: Perform Image Thresholding and Binary Image Generation using MATLAB/Python
Name of Student: _____________________________
Roll No.: _____________________________
Semester/Year: ______________
Academic Session: _____________
Date of Performance: __________
Date of Submission: __________
Department of Electronics and Telecommunication Engineering, S.B.J.I.T.M.R, Nagpur.
Digital Image Processing (N-PECET502P)
AIM: Perform Image Thresholding and Binary Image Generation using MATLAB/Python
OBJECTIVE/EXPECTED LEARNING:
• Understand image thresholding.
• Convert grayscale images to binary images.
• Learn global thresholding using MATLAB/Python.
• Visualize thresholding results.
• Understand applications of binary images.
THEORY:
Image thresholding is one of the simplest and most widely used image segmentation techniques in
Digital Image Processing. It is used to separate the foreground (objects of interest) from the
background by converting a grayscale image into a binary image. In this process, a threshold value
(T) is selected, and each pixel in the grayscale image is compared with this value.
If the pixel intensity is greater than or equal to the threshold (T), the pixel is assigned the value
255 (white), representing the foreground. If the pixel intensity is less than the threshold, it is
assigned the value 0 (black), representing the background. The thresholding operation can be
mathematically represented as:
where:
• f(x,y) is the intensity of the input grayscale image at pixel (x,y)
• g(x,y)g(x,y) is the corresponding binary image,
• T is the selected threshold value.
The quality of the binary image depends on the choice of the threshold value. A suitable threshold
clearly separates the object from the background, while an inappropriate threshold may result in loss
of important details or inclusion of unwanted regions. Thresholds can be selected manually or
determined automatically using methods such as Otsu's thresholding, which computes an optimal
threshold based on the image histogram.
Binary images produced by thresholding simplify image analysis because each pixel has only two
possible values: black (0) or white (255). This reduces computational complexity and makes
subsequent image processing tasks more efficient. Image thresholding is widely used in applications
such as object detection, document scanning, optical character recognition (OCR), medical image
analysis, industrial inspection, quality control, and computer vision systems. It serves as fundamental
preprocessing step for many advanced image processing and pattern recognition techniques.
Department of Electronics and Telecommunication Engineering, S.B.J.I.T.M.R, Nagpur.
Digital Image Processing (N-PECET502P)
Algorithm
1. Start.
2. Read the input image.
3. Convert to grayscale if needed.
4. Select a threshold value.
5. Apply threshold to generate a binary image.
6. Display original and binary images.
7. Save the binary image.
8. Stop.
MATLAB Program
x=imread("[Link]")
T=127
b=x>T
subplot(1,2,1)
imshow(x)
title('Original image')
subplot(1,2,2)
imshow(b)
title('Binary image')
Python Program (OpenCV)
import cv2
import [Link] as plt
img=[Link]('[Link]',0)
binary=[Link](img,127,255,cv2.THRESH_BINARY)
[Link](binary,cmap='gray')
[Link]('off')
Department of Electronics and Telecommunication Engineering, S.B.J.I.T.M.R, Nagpur.
Digital Image Processing (N-PECET502P)
OUTPUT:
Department of Electronics and Telecommunication Engineering, S.B.J.I.T.M.R, Nagpur.
Digital Image Processing (N-PECET502P)
CONCLUSION
The input image was successfully thresholded and converted into a binary image using MATLAB/Python.
Discussion Questions:
1. What is image thresholding?
2. What is a binary image?
3. What is the difference between global and adaptive thresholding?
4. What is the purpose of [Link]()?
5. What is Otsu's thresholding?
6. Why is grayscale conversion performed before thresholding?
7. What values are used in a binary image?
8. Mention one application of binary images.
9. What is image segmentation?
References:
1. Rafael C. Gonzalez & Richard E. Woods, Digital Image Processing.
2. MATLAB Image Processing Toolbox Documentation.
3. OpenCV Documentation.
4. A.K. Jain, Fundamentals of Digital Image Processing. Prentice Hall, 1989.
Department of Electronics and Telecommunication Engineering, S.B.J.I.T.M.R, Nagpur.