Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子
代码如下
import cv2 as cv
import numpy as np
img = cv.imread('Photos/NewYork.jpg')
cv.imshow('Original img', img)
imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray img', imgGray)
imgLap = cv.Laplacian(imgGray, cv.CV_64F)
imgLap = np.uint8(np.absolute(imgLap))
cv.imshow('Laplacian img', imgLap)
sobelx = cv.Sobel(imgGray, cv.CV_64F, 1, 0)
sobely = cv.Sobel(imgGray, cv.CV_64F, 0, 1)
imgSobel = cv.bitwise_or(sobelx, sobely)
cv.imshow('Sobel img', imgSobel)
imgCanny = cv.Canny(imgGray, 150, 175)
cv.imshow('Canny img', imgCanny)
cv.waitKey(0)
实现效果