📚
DLIP
  • Introduction
  • Prerequisite
  • Image Processing Basics
    • Notes
      • Thresholding
      • Spatial Filtering
      • Masking with Bitwise Operation
      • Model n Calibration
    • Tutorial
      • Tutorial: Install OpenCV C++
      • Tutorial: Create OpenCV Project
      • Tutorial: C++ basics
      • Tutorial: OpenCV Basics
      • Tutorial: Image Watch for Debugging
      • Tutorial: Spatial Filter
      • Tutorial: Thresholding and Morphology
      • Tutorial: Camera Calibration
      • Tutorial: Color Image Processing
      • Tutorial: Edge Line Circle Detection
      • Tutorial: Corner Detection and Optical Flow
      • Tutorial: OpenCV C++ Cheatsheet
      • Tutorial: Installation for Py OpenCV
      • Tutorial: OpenCv (Python) Basics
    • LAB
      • Lab Report Template
      • Lab Report Grading Criteria
      • LAB Report Instruction
      • LAB: Grayscale Image Segmentation
        • LAB: Grayscale Image Segmentation -Gear
        • LAB: Grayscale Image Segmentation - Bolt and Nut
      • LAB: Color Image Segmentation
        • LAB: Facial Temperature Measurement with IR images
        • LAB: Magic Cloak
      • LAB: Straight Lane Detection and Departure Warning
      • LAB: Dimension Measurement with 2D camera
      • LAB: Tension Detection of Rolling Metal Sheet
  • Deep Learning for Perception
    • Notes
      • Lane Detection with Deep Learning
      • Overview of Deep Learning
        • Object Detection
        • Deep Learning Basics: Introduction
        • Deep Learning State of the Art
        • CNN, Object Detection
      • Perceptron
      • Activation Function
      • Optimization
      • Convolution
      • CNN Overview
      • Evaluation Metric
      • LossFunction Regularization
      • Bias vs Variance
      • BottleNeck Unit
      • Object Detection
      • DL Techniques
        • Technical Strategy by A.Ng
    • Tutorial - PyTorch
      • Tutorial: Install PyTorch
      • Tutorial: Python Numpy
      • Tutorial: PyTorch Tutorial List
      • Tutorial: PyTorch Example Code
      • Tutorial: Tensorboard in Pytorch
      • Tutorial: YOLO in PyTorch
        • Tutorial: Yolov8 in PyTorch
        • Tutorial: Train Yolo v8 with custom dataset
          • Tutorial: Train Yolo v5 with custom dataset
        • Tutorial: Yolov5 in Pytorch (VS code)
        • Tutorial: Yolov3 in Keras
    • LAB
      • Assignment: CNN Classification
      • Assignment: Object Detection
      • LAB: CNN Object Detection 1
      • LAB: CNN Object Detection 2
      • LAB Grading Criteria
    • Tutorial- Keras
      • Train Dataset
      • Train custom dataset
      • Test model
      • LeNet-5 Tutorial
      • AlexNet Tutorial
      • VGG Tutorial
      • ResNet Tutorial
    • Resource
      • Online Lecture
      • Programming tutorial
      • Books
      • Hardware
      • Dataset
      • Useful sites
  • Must Read Papers
    • AlexNet
    • VGG
    • ResNet
    • R-CNN, Fast-RCNN, Faster-RCNN
    • YOLOv1-3
    • Inception
    • MobileNet
    • SSD
    • ShuffleNet
    • Recent Methods
  • DLIP Project
    • Report Template
    • DLIP 2021 Projects
      • Digital Door Lock Control with Face Recognition
      • People Counting with YOLOv4 and DeepSORT
      • Eye Blinking Detection Alarm
      • Helmet-Detection Using YOLO-V5
      • Mask Detection using YOLOv5
      • Parking Space Management
      • Vehicle, Pedestrian Detection with IR Image
      • Drum Playing Detection
      • Turtle neck measurement program using OpenPose
    • DLIP 2022 Projects
      • BakeryCashier
      • Virtual Mouse
      • Sudoku Program with Hand gesture
      • Exercise Posture Assistance System
      • People Counting Embedded System
      • Turtle neck measurement program using OpenPose
    • DLIP Past Projects
  • Installation Guide
    • Installation Guide for Pytorch
      • Installation Guide 2021
    • Anaconda
    • CUDA cuDNN
      • CUDA 10.2
    • OpenCV
      • OpenCV Install and Setup
        • OpenCV 3.4.13 with VS2019
        • OpenCV3.4.7 VS2017
        • MacOS OpenCV C++ in XCode
      • Python OpenCV
      • MATLAB-OpenCV
    • Framework
      • Keras
      • TensorFlow
        • Cheat Sheet
        • Tutorial
      • PyTorch
    • IDE
      • Visual Studio Community
      • Google Codelab
      • Visual Studio Code
        • Python with VS Code
        • Notebook with VS Code
        • C++ with VS Code
      • Jupyter Notebook
        • Install
        • How to use
    • Ubuntu
      • Ubuntu 18.04 Installation
      • Ubuntu Installation using Docker in Win10
      • Ubuntu Troubleshooting
    • ROS
  • Programming
    • Python_Numpy
      • Python Tutorial - Tips
      • Python Tutorial - For Loop
      • Python Tutorial - List Tuple, Dic, Set
    • Markdown
      • Example: API documentation
    • Github
      • Create account
      • Tutorial: Github basic
      • Tutorial: Github Desktop
    • Keras
      • Tutorial Keras
      • Cheat Sheet
    • PyTorch
      • Cheat Sheet
      • Autograd in PyTorch
      • Simple ConvNet
      • MNIST using LeNet
      • Train ConvNet using CIFAR10
  • Resources
    • Useful Resources
    • Github
Powered by GitBook
On this page
  • Tutorial: Thresholding and Morphology
  • I. Introduction
  • II. Tutorial
  • Part 1. Binary Thresholding
  • Part 2. Morphology
  • Exercise
  • Exercise 1
  • Exercise 2

Was this helpful?

  1. Image Processing Basics
  2. Tutorial

Tutorial: Thresholding and Morphology

PreviousTutorial: Spatial FilterNextTutorial: Camera Calibration

Last updated 1 month ago

Was this helpful?

Tutorial: Thresholding and Morphology

Deep Learning Image Processing. Updated. 2024.3

I. Introduction

In this tutorial, we will learn how to apply thresholding and morphology algorithms to segment objects from the background. Thresholding is a powerful tool to segment object images into regions or from the background based on the image intensity values. After applying thresholding methods, morphology methods are usually applied for post-processing such as pruning unwanted spikes, filling holes, and connecting broken pieces. Also, you will learn how to draw and analyze the histogram of a digital image to determine the contrast of the image intensity and use this information to balance the contrast and determine an optimal value for the thresholding.

II. Tutorial

Part 1. Binary Thresholding

This tutorial shows how to create a simple code to apply the OpenCV function for local thresholding.

Thresholding: OpenCV

First, read the OpenCV documentation

Sample code

int threshold_value = 0;
int threshold_type = 3;
int morphology_type = 0;

int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;

Mat src, src_gray, dst, dst_morph;
src = imread("coin.jpg", 1);

/* threshold_type
0: Binary
1: Binary Inverted
2: Threshold Truncated
3: Threshold to Zero
4: Threshold to Zero Inverted*/

threshold(src, dst, threshold_value, max_BINARY_value, threshold_type);
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('coin.jpg',0)

ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

Example 1-1. Select the local threshold value manually.

Download the example code and test images.

Run the code on all test images.

Apply different values of thresholding for each images and find the best threshold values.

Example 1-2. Otsu Threshold

Modify the program to include ‘Otsu method’.

Apply on each test image and compare the results with global thresholding output.

Example 1-3. Plot Histogram

Calculate and Plot the histogram for each image in gray-scale and analyze if a clear threshold value exists in a histogram.

void cv::calcHist	(	const Mat * 	images,
                        int 	nimages,
                        const int * 	channels,
                        InputArray 	mask,
                        OutputArray 	hist,
                        int 	dims,
                        const int * 	histSize,
                        const float ** 	ranges,
                        bool 	uniform = true,
                        bool 	accumulate = false 
)		
# Python:
hist=cv.calcHist(	images, channels, mask, histSize, ranges[, hist[, accumulate]]	)

For plotting histogram, you may use the following function.

void plotHist(Mat src, string plotname, int width, int height) {
	/// Compute the histograms 
	Mat hist;
	/// Establish the number of bins (for uchar Mat type)
	int histSize = 256;
	/// Set the ranges (for uchar Mat type)
	float range[] = { 0, 256 };

	const float* histRange = { range };
	calcHist(&src, 1, 0, Mat(), hist, 1, &histSize, &histRange);

	double min_val, max_val;
	cv::minMaxLoc(hist, &min_val, &max_val);
	Mat hist_normed = hist * height / max_val; 
	float bin_w = (float)width / histSize;	
	Mat histImage(height, width, CV_8UC1, Scalar(0));	
	for (int i = 0; i < histSize - 1; i++) {	
		line(histImage,	
			  Point((int)(bin_w * i), height - cvRound(hist_normed.at<float>(i, 0))),			
			  Point((int)(bin_w * (i + 1)), height - cvRound(hist_normed.at<float>(i + 1, 0))),	
			  Scalar(255), 2, 8, 0);											
	}

	imshow(plotname, histImage);
}

See here for full example codes

Example 1-4. Local Threshold

Apply ‘Local Adaptive Thresholding’ on the following images. Compare the results of the global thresholding.

//adaptiveThreshold()
void cv::adaptiveThreshold	(	InputArray 	src,
                                 OutputArray 	dst,
                                 double 	maxValue,
                                 int 	adaptiveMethod,
                                 int 	thresholdType,
                                 int 	blockSize,
                                 double 	C 
)		
#Python:
dst=cv.adaptiveThreshold(	src, maxValue, adaptiveMethod, thresholdType, blockSize, C)

Sample code

void cv::adaptiveThreshold	(	
    InputArray 	src,
	OutputArray 	dst,
    double 	maxValue,
    int 	adaptiveMethod,
    int 	thresholdType,
    int 	blockSize,
    double 	C 
)	
    
adaptiveThreshold(src, dst, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY,3, 11);
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('sudoku.png',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
            cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()


Part 2. Morphology

Morphology: OpenCV

First, read the OpenCV documentation on morphology.

Sample code

// Structuring Element
int shape = MORPH_RECT; // MORPH_CROSS, MORPH_ELLIPSE  
Mat element = getStructuringElement( shape, Size(M, N) )                                     
 
// Apply a morphology operation
dilate( src, dst_morph, element);
erode ( src, dst_morph, element);
import cv2 as cv
import numpy as np
img = cv.imread('j.png',0)
cv.getStructuringElement(cv.MORPH_RECT,(5,5))
kernel = np.ones((5,5),np.uint8)
erosion = cv.erode(img,kernel,iterations = 1)

Example 2-1. Morphology selection with trackbar

Download the example code

Apply several morphology to obtain clear segmentation of the object in given images, after Thresholding.


Exercise

Exercise 1

Create a new C++ project in Visual Studio Community

  • Project Name: DLIP_Tutorial_Thresholding

  • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  • Source File: DLIP_Tutorial_Thresholding.cpp

  1. Analyze Histogram and apply Thresholding methods on given images.

  2. Find the optimal threshold method and value for the object segmentation.

  3. Show the results to TA before proceeding to Exercise 2.

Exercise 2

  1. Apply Morphology methods after threshold on all test images.

  2. Analyze which morphology methods works best for each images for object segmentation.

  3. Show the results to TA before finishing this tutorial.


for THRESH_OTSU

for calculating histogram calcHist()

Example 1:

Example 2:

Example 3:

Refer to adaptiveThreshold()

source code_1: Manual Threshold
source code_2: Threshold with Trackbar
Test images
Read this documentation
Read this documentation
Histogram of GrayScale Image File
Histogram of Color Image File
Histogram of Webcam Image
documentation
morphologyEx()
erode()
source code_: Morphology with Trackbar
Test images
OpenCV: OpenCV modules
Logo