Tutorial: Edge Line Circle Detection

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: Image data click here

Part 1. Edge Detection

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

Download Tutorial Code: Canny Edge Demo

  • Declare and define variables:

  • Loads the source image:

  • Create a matrix of the same type and size of src (to be dst), to grayscale

  • Create a window to display the results

  • Create a Trackbar for the user to enter the lower threshold for our Canny detector

  • First, we blur the image with a filter of kernel size 3:

  • Second, we apply the OpenCV function Canny:

  • We fill a dst image with zeros (meaning the image is completely black).

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

Part 2. Line Detection: Hough Transform

Download Tutorial Code: Hough Line Transform Demo

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

1) The Standard Hough Transform ( HoughLines( ) )

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

2) The Probabilistic Hough Line Transform ( HoughLinesP() )

  • 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)\

  • Load an image

  • Detect the edges using Canny detector

  • Copy edges to the images that will display the results in BGR

(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 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.

  • Show results

Part 3. Circle Detection: Hough Circles

Download Tutorial Code: Circle Detection Demo

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.

  • Example code

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.

Also, refer to Youtube

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

Image

Output Examples

Image

Report

Write a concise report using the LAB report template.

Last updated

Was this helpful?