Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Sobel, Laplacian and Canny for Edge Detection in OpenCV

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

OpenCV how to use Sobel, Laplacian, Canny for edge detection, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Brief introduction

The general steps of OpenCV edge detection are:

Filter

Enhancement

Detection

The commonly used edge detection operators and filters are:

Sobel operator

Laplacian operator

Canny operator

Scharr filter

The following uses Sobel, Laplacian and Canny operators for edge detection. The picture is a random download from the Internet.

Code import cv2#*Sobel Edge Detection * * def edge_sobel (src): kernelSize = (3,3) gausBlurImg = cv2.GaussianBlur (src, kernelSize, 0) # convert to grayscale image channels = src.shape [2] if channels > 1: src_gray = cv2.cvtColor (gausBlurImg Cv2.COLOR_RGB2GRAY) else: src_gray = src.clone () scale = 1 delta = 0 depth = cv2.CV_16S # find X direction gradient (create grad_x, grad_y matrix) grad_x = cv2.Sobel (src_gray, depth, 1,0) abs_grad_x = cv2.convertScaleAbs (grad_x) # find Y direction gradient grad_y = cv2.Sobel (src_gray, depth, 0 1) abs_grad_y = cv2.convertScaleAbs (grad_y) # merge gradient (approximate) edgeImg = cv2.addWeighted (abs_grad_x, 0.5, abs_grad_y, 0.5 0) return edgeImg#*Laplacian edge detection * * def edge_laplacian (src): scale = 1 delta = 0 depth = cv2.CV_16S if src.shape [2] > 1: src_gray = cv2.cvtColor (src Cv2.COLOR_RGB2GRAY) else: src_gray = src.clone () kernelSize = (3,3) gausBlurImg = cv2.GaussianBlur (src_gray, kernelSize, 0) laplacianImg = cv2.Laplacian (gausBlurImg, depth) KernelSize) edgeImg = cv2.convertScaleAbs (laplacianImg) return edgeImg#*Canny Edge Detection * * def edge_canny (src, threshold1, threshold2): kernelSize = (3,3) gausBlurImg = cv2.GaussianBlur (src, kernelSize, 0) edgeImg = cv2.Canny (gausBlurImg, threshold1) Threshold2) return edgeImg#* main function * * imgSrc = cv2.imread ("1.jpg") sobelImg = edge_sobel (imgSrc) laplacianImg = edge_laplacian (imgSrc) cannyImg = edge_canny (imgSrc, 20,60) cv2.imshow ("Origin", imgSrc) cv2.imshow ("Sobel", sobelImg) cv2.imshow ("Laplacian" LaplacianImg) cv2.imshow ("Canny", cannyImg) cv2.waitKey (0) cv2.destroyAllWindows () effect

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report