【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】

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)

# 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)

实现效果

【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】_第1张图片
【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】_第2张图片【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】_第3张图片
【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】_第4张图片
【Python+OpenCV 边缘检测—Laplacian算子/Sobel算子/Canny算子】_第5张图片

你可能感兴趣的:(opencv,python,计算机视觉)