Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use OpenCV to realize Line Detection in C++

Shulou Source: shulou.com Published: 2022-06-02 08:07:10 09月30日 Update

C++ how to use OpenCV to achieve line detection, for this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Hough transform

Hough transform is one of the basic methods to recognize geometric shapes from images in image processing. It is widely used, and there are many improved algorithms. It is mainly used to separate geometric shapes (such as lines, circles, etc.) with the same characteristics from the image. The most basic Hough transform is to detect lines (line segments) from black-and-white images.

Take line detection as an example, each pixel coordinate point is transformed into a unified measure of the contribution of straight line characteristics. A simple example is as follows: a straight line is a collection of a series of discrete points in an image. Through the discrete polar coordinate formula of a straight line, the geometric equation of the discrete points of the straight line can be expressed as follows:

X * cos (theta) + y * sin (theta) = r, where the angle theta refers to the angle between r and the X axis, r is to the geometric vertical of the line

Straight distance. Any point on a straight line, x, y can be expressed, where r, theta are constants. The formula is graphically represented as follows:

However, in the field of image processing, the pixel coordinate P (x, y) of the image is known, and r, theta are the variables we are looking for. If we can draw each (r, theta) value according to the pixel coordinate P (x, y), then the transformation from the image Cartesian coordinate system to the polar coordinate Hough space system is called the Hough transformation of a straight line. By quantizing the Hough parameter space, the transformation is divided into a finite interval or cumulative lattice. When the Hough transform algorithm starts, each pixel coordinate point P (x, y) is converted to the curve point of (r, theta) and accumulated to the corresponding grid data point. When a wave crest appears, it shows that there is a straight line. The same principle can be used to detect circles, but the parameter equation for circles is changed to such as

The following equation:

(X-a) ^ 2 + (ymurb) ^ 2 = r ^ 2 where (a, b) is the coordinate of the center point of the circle and the radius of the r circle. Thus Hoff's parameter space becomes a three-dimensional parameter space. The given circle radius is transformed into two-dimensional Hough parameter space, and the transformation is relatively simple and commonly used.

Related API

Code demonstration

Create a new project opencv-0017, configure properties (VS2017 configure OpenCV common properties), and then write # include and main methods in the source file

Start coding for Hoff's direct detection.

This is the answer to the question about how to use OpenCV to achieve line detection in C++. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Lines Hough transformations images detection parameters spaces pixels geometry coordinates methods problems discretization cantilever formulas radii image processing attributes shapes curves Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Huawei vpn MariaDB