[Python+OpenCV edge detection - Laplacian operator/Sobel operator/Canny operator]

Python+OpenCV edge detection - Laplacian operator/Sobel operator/Canny operator

code show as below


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)

# Laplacian算子
imgLap = cv.Laplacian(imgGray, cv.CV_64F)
imgLap = np.uint8(np.absolute(imgLap))
cv.imshow('Laplacian img', imgLap)

# Sobel算子
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)

# Canny算子
imgCanny = cv.Canny(imgGray, 150, 175)
cv.imshow('Canny img', imgCanny)

cv.waitKey(0)

achieve effect

insert image description here
insert image description hereinsert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/LPYchengxuyuan/article/details/122046196