读取图像computer.jpg,运用Opencv+Python基于拉普拉斯算子,LoG算子,Canny算子等实现边缘检测并加以比较,显示结果图像python代码
时间: 2024-10-15 07:16:31 浏览: 66
在Python中,我们可以使用OpenCV库来处理图像并应用不同的边缘检测算法。首先确保已经安装了opencv-python库。下面是一个基本的示例,展示了如何读取图像`computer.jpg`,然后分别使用拉普拉斯算子、洛伦兹曲线(LoG)算子和Canny算子进行边缘检测,并将结果进行对比:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread('computer.jpg', cv2.IMREAD_GRAYSCALE)
# 拉普拉斯算子
laplacian = cv2.Laplacian(image, cv2.CV_64F)
_, laplacian_edges = cv2.threshold(laplacian, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# 洛伦兹曲线(Log算子)
log = cv2.ximgproc.logarithmicLaplace(image)
_, log_edges = cv2.threshold(log, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# Canny算子
canny = cv2.Canny(image, low_threshold=50, high_threshold=150)
# 显示原始图像和各边沿检测结果
titles = ['Original Image', 'Laplacian Edges', 'LoG Edges', 'Canny Edges']
images = [image, laplacian_edges, log_edges, canny]
for i in range(len(images)):
cv2.imshow(titles[i], images[i])
cv2.waitKey(0) # 等待用户按键
cv2.destroyAllWindows() # 关闭所有窗口
阅读全文