In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python how to achieve Opencv cv2.Canny () edge detection". In daily operation, I believe many people have doubts about how to achieve Opencv cv2.Canny () edge detection in Python. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve Opencv cv2.Canny () edge detection by Python". Next, please follow the editor to study!
Canny edge detection is a popular edge detection algorithm. It was developed by John F and is a multi-stage algorithm.
Canny edge detection consists of four steps:
Noise reduction (using Gaussian filtering to remove high-frequency noise)
Calculate the edge gradient and direction (SobelX and SobleY cores filter the smoothed image horizontally and vertically to find the edge gradient and direction of each pixel)
Non-maximum suppression (after the gradient size and direction are obtained, the image is fully scanned to remove any unwanted pixels, which may not form an edge. Check whether the pixel is a local maximum in the neighborhood of its gradient direction. Otherwise, it will be suppressed (return to zero). In short, the result is a binary image with "thin edges".
Lag threshold (determines which edges are real and which are not. For this reason, two thresholds, minVal and maxVal, are needed. Any edge whose intensity gradient is greater than maxVal must be an edge, and an edge smaller than minVal must be a non-edge, so it is discarded. Edges between these two thresholds are classified as edges or non-edges according to their connectivity. If they are connected to the determine Edge pixel, they are considered part of the edge. Otherwise, they will also be discarded. )
Choosing the lag threshold minVal and maxVal is the key to get the correct result.
1. Effect picture
The original image VS Canny detection results are as follows:
two。 Source code # Canny edge detection is a popular edge detection algorithm. It is developed by John F and is a multi-stage algorithm; # Canny edge detection consists of four steps: # # 1. Noise reduction (using Gaussian filtering to remove high frequency noise); # 2. Calculate the edge gradient and direction (SobelX and SobleY cores filter the smoothed image horizontally and vertically to find the edge gradient and direction of each pixel); # 3. Non-maximum suppression (after the gradient size and direction are obtained, the image is fully scanned to remove any unwanted pixels, which may not form an edge. Check whether the pixel is a local maximum in the neighborhood of its gradient direction. Otherwise, it will be suppressed (return to zero). In short, the result is a binary image with "thin edges". # 4. Lag threshold (determines which edges are real and which are not. For this reason, two thresholds, minVal and maxVal, are needed. Any edge whose intensity gradient is greater than maxVal must be an edge, and an edge smaller than minVal must be a non-edge, so it is discarded. Edges between these two thresholds are classified as edges or non-edges according to their connectivity. If they are connected to the determine Edge pixel, they are considered part of the edge. Otherwise, they will also be discarded. ) # # selecting lag thresholds minVal and maxVal is the key to getting the correct result. Import cv2from matplotlib import pyplot as pltimg = cv2.imread ("zly.jpg", 0) edges = cv2.Canny (img, 80,200) plt.subplot, plt.imshow (img, cmap= "gray") plt.title ("Original Image"), plt.xticks ([]), plt.yticks ([]) plt.subplot (122), plt.imshow (edges, cmap= "gray") plt.title ("Edge Image"), plt.xticks ([]), plt.yticks ([]) plt.show ()
Reference https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_canny/py_canny.html#canny
Supplement: Canny () parameter in OpenCV-Python
Steps:
Convert color image to grayscale image (read in grayscale image or single-channel image)
Gaussian blur (denoising) on the image
Calculate the image gradient, and calculate the image edge amplitude and angle according to the gradient.
Non-maximum suppression (edge thinning) along the gradient direction
Double threshold edge connection processing
Binary image output result
"" cv2.Canny (image, # input the original image (must be a single-channel image) threshold1, threshold2, # A larger threshold 2 is used to detect obvious edges in the image [, edges [, apertureSize] # size of apertureSize:Sobel operator L2gradient]]) # Parameter (Boolean): true: calculated using a more accurate L2 norm (that is, the sum of squares of reciprocals in both directions is reopened) False: use the L1 norm (directly add the absolute values of the two directional derivatives). Import cv2import numpy as np original_img = cv2.imread ("qingwen.png", 0) # canny (): edge detection img1 = cv2.GaussianBlur (original_img, (3Power3), 0) canny = cv2.Canny (img1, 50150) # Morphology: edge detection _, Thr_img = cv2.threshold (original_img,210,255,cv2.THRESH_BINARY) # set red channel threshold 210 (threshold affects gradient operation) kernel = cv2.getStructuringElement (cv2.MORPH_RECT (5recom 5)) # defines a rectangular structural element gradient = cv2.morphologyEx (Thr_img, cv2.MORPH_GRADIENT, kernel) # gradient cv2.imshow ("original_img", original_img) cv2.imshow ("gradient", gradient) cv2.imshow ("Canny", canny) cv2.waitKey (0) cv2.destroyAllWindows ()
A program that can adjust the threshold.
Import cv2import numpy as np def CannyThreshold (lowThreshold): detected_edges = cv2.GaussianBlur (gray, (3P3), 0) detected_edges = cv2.Canny (detected_edges, lowThreshold, lowThreshold*ratio, apertureSize = kernel_size) dst = cv2.bitwise_and (img,img,mask = detected_edges) # just add some colours to edges from original image. Cv2.imshow ("canny demo", dst) lowThreshold = 0max_lowThreshold = 100ratio = 3kernel_size = 3 img = cv2.imread ("qingwen.png") gray = cv2.cvtColor (img,cv2.COLOR_BGR2GRAY) cv2.namedWindow ("canny demo") cv2.createTrackbar ("Min threshold", "canny demo", lowThreshold, max_lowThreshold, CannyThreshold) CannyThreshold (0) # initializationif cv2.waitKey (0) = 27: cv2.destroyAllWindows ()
At this point, the study of "how to achieve Opencv cv2.Canny () edge detection by Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.