Introduction to OpenCV Line Detection
OpenCV line detection is an essential function available in the OpenCV library for python language coding. The Hough Transform is a commonly used method utilized majorly for processing the image to enable detection of any particular shapes and provide details of the mathematical representation of that particular shape detected. The function can even detect shapes that are distorted or broken to some extent. The OpenCV line detection method uses the Houghline method two to detect the edges of the specified image and then identify the particular shape.
Syntax of OpenCV Line Detection()
Given below is the syntax mentioned:
OpenCV does the implementation of the line detection through hough line transformation as follows:
Standard method for hough transformation:
- The result given through the standard hough transformation process is a resultant vector of couples which are denoted by rho and theta.
- The OpenCV hough lines() function is the useful implementation of the resultant vector.
An alternative probabilistic method for hough line transformation:
- It is considered to be a more efficient method for the implementation of the line detection procedure by utilization of hough line transformation. The most extreme hints of lines are also detected through the output, which is resulted from the implementation of the method.
- The OpenCV HoughLine P() function is used for implementing line detection through the probabilistic method.
Explanation of parameters for OpenCV detection():
- The theoretical understanding and working are summarized and encapsulated into the OpenCV line detection function, which is represented in a practical demonstration by cv2.HoughLine(). The function it results in returning an array of value. Theta is observed to be measured in radians, while rho is observed to measured on pixels. For the image that has to be processed, it firstly needs to be converted into the binary form, and the first parameter would be the image being input in the binary image form. further application of threshold or the usage of detection of canny edges is done before the application of Houghline transformation is done on the source image.
- The next two parameters represent the accuracies for the values that are taken. The 4th parameter is representative of the threshold, which is used to describe the minimum vote that the parameter should get in order for the parameter to consider the value as a line. It must be noted that the total number of votes that are fed into the perimeter are dependent upon the total number of points that are present on the line which is present in the image. Therefore, the number of votes can be set to be dependent on the minimum length that the line has in order for it to be detected.
How does OpenCV Line Detection Function Work?
- At first, the two-dimensional array is created, which is initially set at the value of 0, which would be used for holding values of the parameters.
- The rows for the parameter are denoted by the value ‘r’, and the columns are denoted by theta.
- The accuracy that the user needs is taken as the base point to set the size for the array. For instance, if the user needs the accuracy for the precise angle in shape at 1 degree, the user would require 180 columns.
- For the value to be taken for the, rose the maximum possible length is the length of the diagonal for the image; hence for the requirement of 1-pixel accuracy, the number of roles required can be equivalent to the length of the diagonal for the image.
Example of OpenCV Line Detection
Given below is the example of OpenCV Line Detection:
Command used to import the OpenCV library to utilize the OpenCV line detection function.
import numpy as np1
# command used for reading an image from the disk, cv2.imread function is used
img1 = cv2 *.imread('educba.png')
gray1 = cv2 *. * cvt * Color * (* img1 *, * cv2 *. * COLOR * _ * BGR2GRAY *) * *
edges1 = cv2.Canny * (gray1, * 50 *, * 150 *, * aperture * Size * * = * * 3 *) *
lines1 = cv2 *. * Hough * Lines(edges1, * 1, * np *. * pi * / * 180 *, * * 200)
for rho1 *, *theta1 in lines:
a1 * *= * *np *. *cos * (*theta *) *
b1 * *= * *np. *sin * * (*theta *) *
x1 * *= * *a1 ** *rho1
y0 = b*rho1
x_1 * *= * *int * (*x1 * *+ * *1000 ** * (-b))
y_1 * *= * *int * (*y1 *+ *1000 ** * (*a *))
x_2 * *= * *int * (*x1 * *- * *1000 ** * ( *-b *))
y_2 * *= * *int(*y1 * *- * *1000 ** * (*a *))
cv2.line(*img1, * (x_1, *y_1), * (x_2, *y_2), * (0, *0, *255) *, *2)
# Creation of a GUI window in order to display the image on the screen
cv2.imwrite * (*'line detection.png', *img1 *)
# cv2.waitKey method used for holding the window on screen
The OpenCV line detection function is a very useful function that is utilized in a plethora of applications especially related to image detection. This inbuilt function is used to isolate specific features with respect to uh particular shape, which is present within the image specified for processing. The function is also used for detection of the gaps that are present within the image boundaries, and it is essentially useful because it is tolerant towards the gaps that are present in the description for the feature boundaries, and it is mostly unaffected by the amount of tolerable noise which is present within an image. Hence, the Open CV line detection function seems to be used a lot for scanning and detecting barcodes and associated applications; it is also used to verify and recognise awful barcodes across various platforms and mobile development apps.
This is a guide to OpenCV Line Detection. Here we discuss the introduction; how does the OpenCV line detection function work? and example, respectively. You may also have a look at the following articles to learn more –