📚
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: Edge Line Circle Detection
  • I. Introduction
  • II. Tutorial
  • Exercise
  • Assignment - Submit (1 week)
  • Introduction
  • Report

Was this helpful?

  1. Image Processing Basics
  2. Tutorial

Tutorial: Edge Line Circle Detection

PreviousTutorial: Color Image ProcessingNextTutorial: Corner Detection and Optical Flow

Last updated 1 month ago

Was this helpful?

Tutorial: Edge Line Circle Detection

Tutorial: Edge, StraightLine, Circle Detection

I. Introduction

In this tutorial, you will learn how to use OpenCV to detect edges, lines, and circles. For an application, you will learn how to find straight lanes using Canny edge detection and Hough transformation algorithms.

II. Tutorial

Download Test Image Files:

Part 1. Edge Detection

We will learn how to use Canny Edge Algorithm to detect and display edges.

  • OpenCV Canny():

Download Tutorial Code:

C++: void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false )
 

·         image – single-channel 8-bit input image.
·         edges – output edge map; it has the same size and type as image .
·         threshold1 – first threshold for the hysteresis procedure.
·         threshold2 – second threshold for the hysteresis procedure.
·         apertureSize – aperture size for the Sobel() operator.
·         L2gradient – a flag, indicating whether a more accurate  L2 norm   should be used to calculate the image gradient magnitude ( L2gradient=true ), or whether the default L1 norm  is enough ( L2gradient=false ).
  • Declare and define variables:

  Mat src, src_gray;
  Mat dst, detected_edges;
 
  int edgeThresh = 1;
  int lowThreshold;
  int const max_lowThreshold = 100;
  int ratio = 3;  // a ratio of lower:upper
  int kernel_size = 3; //Sobel Operation
String window_name = "Edge Map";
  • Loads the source image:

/// Load an image
src = imread( argv[1] );
if( !src.data )
  { return -1; }
  • Create a matrix of the same type and size of src (to be dst), to grayscale

dst.create( src.size(), src.type() );
cvtColor( src, src_gray, CV_BGR2GRAY );
  • Create a window to display the results

namedWindow( window_name, CV_WINDOW_AUTOSIZE );
  • Create a Trackbar for the user to enter the lower threshold for our Canny detector

createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
  • First, we blur the image with a filter of kernel size 3:

blur( src_gray, detected_edges, Size(3,3) );
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
  • We fill a dst image with zeros (meaning the image is completely black).

dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow( window_name, dst );

Part 2. Line Detection: Hough Transform

In OpenCV, there are two Options for Hough Lıne Transform

  • It gives you the results of(θ, rθ)

  • A more efficient implementation of the Hough Line Transform. It gives as output of extremes(end) points of the detected lines (x0, y0, x1, y1)\

void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )

/*

·         image – 8-bit, single-channel binary source image. The image may be modified by the function.
          lines-Output vector of lines. Each line is represented by a 2 or 3 element vector (ρ,θ) or (ρ,θ,votes) 
                 ρ is the distance from the coordinate origin (0,0) (top-left corner of the image)
                 θ is the line rotation angle in radians ( 0∼vertical line,π/2∼horizontal line )
                 votes is the value of accumulator.
          lines (HoughLinesP) Output vector of lines. Each line is represented by a 4-element vector (x1,y1,x2,y2), where (x1,y1) and (x2,y2) are the ending points of each detected line segment.
·         lines – Output vector of lines. Each line is represented by a 4-element vector   , where   and   are the ending points of each detected line segment.
·         rho – Distance resolution of the accumulator in pixels.
·         theta – Angle resolution of the accumulator in radians.
·         threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (   ).
·         minLineLength – Minimum line length. Line segments shorter than that are rejected.
·         maxLineGap – Maximum allowed gap between points on the same line to link them.
*/
  • Load an image

// Loads an image
	const char* filename = "../images/Lane_test.jpg";
	Mat src = imread(filename, IMREAD_GRAYSCALE);
	
	// Check if image is loaded fine
	if (src.empty()) {
		printf(" Error opening image\n");
		return -1;
	}
	
	imshow("Source", src);
  • Detect the edges using Canny detector

	// Edge detection
	Canny(src, dst, 50, 200, 3);
  • Copy edges to the images that will display the results in BGR

    	// Copy edge results to the images that will display the results in BGR
    	cvtColor(dst, cdst, COLOR_GRAY2BGR);
    	cdstP = cdst.clone();

(Option 1) Standard Hough Line Transform

  • First, apply the Hough Transform. Then display the results by drawing the lines.

  • Output vector of lines. Each line is represented by a 2 or 3 element vector (ρ,θ) or (ρ,θ,votes) . ρ is the distance from the coordinate origin (0,0) (top-left corner of the image). θ is the line rotation angle in radians ( 0∼vertical line,π/2∼horizontal line ). votes is the value of accumulator.\

	// (Option 1) Standard Hough Line Transform
	vector<Vec2f> lines;		
	HoughLines(dst, lines, 1, CV_PI / 180, 150, 0, 0); 
	
		// Draw the detected lines
	for (size_t i = 0; i < lines.size(); i++)
	{
		float rho = lines[i][0], theta = lines[i][1];
		Point pt1, pt2;
		double a = cos(theta), b = sin(theta);
		double x0 = a * rho, y0 = b * rho;
		pt1.x = cvRound(x0 + 1000 * (-b));
		pt1.y = cvRound(y0 + 1000 * (a));
		pt2.x = cvRound(x0 - 1000 * (-b));
		pt2.y = cvRound(y0 - 1000 * (a));
		line(cdst, pt1, pt2, Scalar(0, 0, 255), 3, LINE_AA);
	}
	
	imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);

(Option 2) Probabilistic Hough Line Transform

  • Lines (HoughLinesP) Output vector of lines. Each line is represented by a 4-element vector (x1,y1,x2,y2), where (x1,y1) and (x2,y2) are the ending points of each detected line segment.

vector<Vec4i> linesP; 
	HoughLinesP(dst, linesP, 1, CV_PI / 180, 50, 50, 10); 
	
	// Draw the lines
	for (size_t i = 0; i < linesP.size(); i++)
	{
		Vec4i l = linesP[i];
		line(cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
	}
	
	imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
  • Show results

	// Show results
	imshow("Source", src);
	imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
	imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);

Part 3. Circle Detection: Hough Circles

Usually, the function detects the centers of circles well but the radius may not be accurate. It helps if you can specify the radius ranges ( minRadius and maxRadius ), if available. Or, you may set maxRadius to a negative number to return centers only without radius search, and find the correct radius using an additional procedure.

void cv::HoughCircles(InputArray image, OutputArray circles, int method, double dp, double   minDist, double   param1 = 100, double      	param2 = 100, int minRadius = 0, int              	maxRadius = 0  )
 
/*
image:  	8-bit, single-channel, grayscale input image.
Circles: 	Output vector of found circles. Each vector is encoded as 3 or 4 element floating-point vector (x,y,radius) or (x,y,radius,votes) .
method	Detection method, see HoughModes. Currently, the only implemented method is HOUGH_GRADIENT
dp          	Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
minDist   Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
param1 	First method-specific parameter. In case of HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny edge detector (the lower one is twice smaller).
param2	Second method-specific parameter. In case of HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
minRadius          	Minimum circle radius.
maxRadius         	Maximum circle radius. If <= 0, uses the maximum image dimension. If < 0, returns centers without finding the radius. 
*/
  • Example code

vector<Vec3f> circles;
HoughCircles(gray, circles, 3, 2, gray.rows / 4, 200, 100);
for (size_t i = 0; i < circles.size(); i++)
{
	Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
 	int radius = cvRound(circles[i][2]);
 	// draw the circle center
 	circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
 	// draw the circle outline
  circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0); 
}
namedWindow("circles", 1);
imshow("circles", src);

Exercise

Exercise 1

Download the following tutorial codes for Edge and Line Detection. Fill in the blanks.

Try to fill in the codes without referring to the demo source files

Exercise 2

  1. Detect Pupil/Iris & Signpost from the following images

Assignment - Submit (1 week)

Introduction

In this assignment, detect and draw lanes (left and right each) on the given images. Also, find and draw the vanishing point, the point where the two lane lines intersect.

Dataset

Download two image files:

Use the necessary image process techniques, you have learnt in class, including

  • Filtering

  • set ROI

  • Canny Edge

  • Hough Line Detection

  • Algorithm for selecting one line per lane

  • Calculate the vanishing point

  • Display lanes in colors

Output Examples

Report

Write a concise report using the LAB report template.

Second, we apply the OpenCV function :

Finally, we will use the function to map only the areas of the image that are identified as edges (on a black background)

Download Tutorial Code:

1) The Standard Hough Transform ( )

2) The Probabilistic Hough Line Transform ( () )

Download Tutorial Code:

Exercise code 1:

Exercise code 2:

Also, refer to

Image
Image
Canny
copyTo
Hough Line Transform Demo
HoughLines( )
HoughLinesP
Circle Detection Demo
HoughCircles() OpenCV docs
Canny Edge Exercise
Hough Line Exercise
Youtube
Dataset 1
Dataset 2
Image data click here
read docs
Canny Edge Demo