import cv2
import matplotlib.pyplot as plt
o = cv2.imread("F:\\jupyter\\bigdata7\\animal_final_0\\2_cir_0.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow("lena", o)
r1 = cv2.Canny(o, 100, 200)
cv2.imshow("result1", r1)
r2 = cv2.Canny(o, 64, 128)
cv2.imshow("result2", r2)
cv2.waitKey()
cv2.destroyAllWindows()
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(12,6))
# 显示图形
titles = ['原始图像', 'Canny算子']
images = [o, r2]
for i in range(2):
plt.subplot(1, 2, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
实现非常简单,最核心的部分仅为一行代码:
cv2.Canny(o, 100, 200)
实现的效果如下图所示,边缘检测较为明显,但是需要注意的是,边缘检测的效果受到图像本身的影响较大