1.Sobel边缘检测
Sobel算子由两种组成:垂直边沿检测和水平边沿检测
import cv2
import numpy as np
img = cv2.imread('try.jpg', cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape
sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
cv2.imshow('Original', img)
cv2.imshow('Sobel horizontal', sobel_horizontal)
cv2.imshow('Sobel vertical', sobel_vertical)
cv2.waitKey(0)
2.拉普拉斯算子
the Sobel filter detects edges in either a horizontal or vertical direction and it doesn’t give us a
holistic view of all the edges. To overcome this, we can use the Laplacian filter.
Sobel算子只能在垂直方向或者水平方向进行边沿检测,并不能在整体视野上检测所有边沿。这时,我们可以使用拉普拉斯算子。
import cv2
img=cv2.imread('try.jpg',cv2.IMREAD_COLOR)
img=cv2.Laplacian(img,cv2.CV_64F)
cv2.imshow('laplacian',img)
cv2.waitKey(0)
但是拉普拉斯算子并不能很好应用于所有的image,有些图片会产生很多noise。To overcome this problem, we use the Canny edge detector. To use the Canny edge detector
3.canny
import cv2
img=cv2.imread('try.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('',img)
#It takes two numbers
# as arguments to indicate the thresholds. The second argument is called the low threshold
# value, and the third argument is called the high threshold value. If the gradient value is
# above the high threshold value, it is marked as a strong edge.
img=cv2.Canny(img,20,240)
cv2.imshow('Canny',img)
cv2.waitKey()
cv2.destroyAllWindows()