In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use OpenCV for line detection in Python, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this article on how to use OpenCV for line detection in Python, let's take a look.
1. Introduction
In image processing, line detection is a common algorithm, which usually obtains the set of n edge points and finds the straight lines passing through these edge points. Among them, for line detection, the most popular detector is the straight line detection technology based on Hough transform.
two。 Hough transform
Hough transform is a feature extraction method in image processing, which can identify the geometric shape in the image. It will vote in the parameter space to determine the shape of the object, through the detection of the cumulative results to find a solution corresponding to a maximum, using this solution can be used to get a parameter in accordance with a specific shape.
Before using Hough transform to detect straight lines, the edge detection algorithm must be used to reduce the amount of data of the image, remove irrelevant information, and retain the important structural features of the image.
3. Take a chestnut 3.1 to read in the image for graying.
First, we read in the sample test image, and then use the cvtColor () function to gray the image. The sample code is as follows:
Im = cv2.imread (". / ladder.png") gray_img = cv2.cvtColor (im, cv2.COLOR_BGR2GRAY)
The running results are as follows:
In the above image, the left side is the original color image, and the right side is the grayscale image after graying.
3.2 perform edge detection
Then we use edge detection algorithms (Canny, Sobel, Laplacian, etc.) to detect the edges of objects. The sample code is as follows:
Canny = cv2.Canny (gray_img, 30,150)
The running results are as follows:
3.3 perform Hough transform
Finally, we use Hough transform to get the straight line detection result. The sample code is as follows:
Lines = cv2.HoughLines (canny, 1, np.pi / 180,180) lines1 = lines [:, 0,:] for rho Theta in lines1 [:]: a = np.cos (theta) b = np.sin (theta) x0 = a * rho y0 = b * rho x1 = int (x0 + 3000 * (- b)) y1 = int (y0 + 3000 * (a)) x2 = int (x0-3000 * (- b)) y2 = int (y0-3000 * (a) cv2.line (im, (x1, y1), (x2, y2), (0,0,255)) 2)
The running results are as follows:
It can be seen that through a few simple steps, we can easily detect all the straight lines in the image.
Supplement
Of course, Python can use OpenCV to detect not only the straight line, but also the straight line tilt angle. The following is the core code of the implementation
Import cv2import numpy as npdef line_detect (image): # convert the picture to HSV hsv = cv2.cvtColor (image, cv2.COLOR_BGR2HSV) # set the threshold lowera = np.array ([0,0221]) uppera = np.array ([180,30,255]) mask1 = cv2.inRange (hsv, lowera, uppera) kernel = np.ones ((3,3) Np.uint8) # perform morphological operations on the resulting image (closed operation and open operation) mask = cv2.morphologyEx (mask1, cv2.MORPH_CLOSE, kernel) # closed operation: means to dilate first Then perform corrosion operation mask = cv2.morphologyEx (mask, cv2.MORPH_OPEN, kernel) # Open operation: it means corrosion first, then expansion operation # draw outline edges = cv2.Canny (mask, 50,150, apertureSize=3) # display picture cv2.imshow ("edges", edges) # detect white line here is the condition to set the detection line, you can read the HoughLinesP () function Then set the detection condition lines= cv2.HoughLinesP (edges, 1, np.pi / 180,40 minLineLengthreading 10) print "lines=", lines print "= =" iTunes 1 # traverses the data obtained by Hough transform for line in lines: # newlines1 = lines [:, 0,:] print "line [" + str (iMuy1) + "] =", line x1 talent y1x2 Y2 = line [0] # determine a straight line at two points Here is the data of two points (x1Powery1) (x2maginy2) cv2.line (image, (x1memy1), (x2memy2), (0meme0255), 2) # convert the line # to floating point number on the original image. Calculate the slope x1 = float (x1) x2 = float (x2) y1 = float (y1) y2 = float (y2) print "x1century% sdhorex% mecheline y1century% sparry y2skills% s"% (x1, x2, y1) Y2) if x2-x1 = = 0: print "straight line is vertical" result=90 elif y2-y1 = = 0: print "straight line is horizontal" result=0 else: # calculate the slope k =-(y2-y1) / (x2-x1) # find the inverse tangent Then convert the obtained radians into degrees result = np.arctan (k) * 57.29577 print "straight line tilt angle is:" + str (result) + "degrees" I = ionization 1 # to display the final result graph cv2.imshow ("line_detect") Image) return resultif _ _ name__ ='_ _ main__': # read the picture src = cv2.imread ("lines/line6.jpg") # set the window size cv2.namedWindow ("input image", cv2.WINDOW_AUTOSIZE) # display the original picture cv2.imshow ("input image") Src) # call function line_detect (src) cv2.waitKey (0) this is the end of the article on "how to use OpenCV for Line Detection in Python" Thank you for reading! I believe you all have a certain understanding of "how to use OpenCV for straight line detection in Python". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.