Line segment detection

Line segment detectionIntroduction to the experimental results of the routineCode explanationComplete codeCode explanationDetect line segmentsfind_line_segmentsHough Transform

Introduction to the experimental results of the routine

The routine code in this section is located at: [Source code summary / 04.Detecting / 01.find_lines]

We use CanMV IDE to open the routine code and connect K230 to the computer via USB

Click the run button in the lower left corner of CanMV IDE,

Aim the camera of K230 at the line segment

You can see that the line segment in the picture will be marked on the screen (if there is no screen, look at the frame buffer)

Original image:

image-20250211100907523

K230 recognition result:

image-20250211101012471

Code explanation

The peripherals we will use in this section are mainly camera modules

Line segment detection is done by find_line_segments() in K230 Method implementation, this method belongs to the image module

The code used in the routine is as follows:

Complete code

*Subject to the content of the file [Source code summary / 04.Detecting / 01.find_lines.py]

Code explanation

The basic structure of this section of the code is as follows

Import and initialization part:

scale_coordinates function:

Image processing Pipeline settings:

Main loop processing:

In this section of code, we did not use a more conventional way to do line segment detection, but took advantage of the K230's feature of supporting multiple layers

Detect graphics at a lower resolution and then scale the result to a high-resolution background image

After doing this, the frame rate of the program will increase significantly

All subsequent codes in this chapter use the original detection method, the code will be simpler, but the frame rate will be much lower

Detect line segments

find_line_segments

Uses the Hough transform to find line segments in an image. Returns a list of image.line objects.

roi is the region of interest rectangle (x, y, w, h) to copy. If not specified, roi is the image rectangle. Only pixels within the roi are operated on.

merge_distance specifies the maximum number of pixels two line segments can be apart from each other without being merged.

max_theta_difference is the maximum angle difference between two line segments to be merged by merge_distance above.

This method uses the LSD library (also used by OpenCV) to find line segments in an image. It is a bit slow, but very accurate and line segments do not jump.

Not supported on compressed images or bayer images.

 

Hough Transform

Hough Transform is a mathematical method for detecting geometric shapes such as lines and circles from images.

For example, imagine that you have a drawing with many dots scattered in front of you.

If you want to find the straight line that these dots can form:

  1. The traditional method is to try to connect any two points to see if a straight line can be formed, which is very time-consuming
  2. The idea of Hough transform is the opposite - for each point, assume that it may be on countless straight lines, and find the straight line that multiple points fall on

Take an example in life: you stand on the side of the road and look at the telephone poles. Although the telephone poles are distributed in different positions, you can see at a glance that they are arranged in a straight line. This is similar to the principle of Hough transform.

Practical application

Algorithm steps