opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)

opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)

opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第1张图片
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第2张图片
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第3张图片
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第4张图片

梯度和方向opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第5张图片
非极大值抑制:
当中心点C>(dTmp1 && dTmp2)时,保留
当中心点C<(dTmp1 || dTmp2)时,非极大值抑制
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第6张图片
当A>(C&&B)时,保留
当A<(C||B)时,非极大值抑制
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第7张图片
保留潜在边缘
opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第8张图片
代码实现:

import cv2
import numpy as np

img = cv2.imread("img/lena.jpg", 0)
# canny边缘检测
canny = cv2.Canny(img, 50, 100)
# Sobel算子
sobelx = cv2.Sobel(img, cv2.CV_64F, dx=1, dy=0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img, cv2.CV_64F, dx=0, dy=1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
# Schar算子
scharrx = cv2.Scharr(img, cv2.CV_64F, dx=1, dy=0)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.Scharr(img, cv2.CV_64F, dx=0, dy=1)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
# Laplacian算子
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)

cv2.imshow('canny', canny)
cv2.imshow('Sobel', sobelxy)
cv2.imshow('scharr', scharrxy)
cv2.imshow('laplacian', laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()

opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny)_第9张图片

参考视频:

https://www.bilibili.com/video/av61678672/?p=6

你可能感兴趣的:(opencv_python 边缘检测笔记(Sobel,Scharr,Laplacian,Canny))