前言:
前几日发表了在matlab进行简单的图像边缘处理的文章,后来继续进行了相应的学习,决定还是回归python环境跑一下opencv进行图像边缘检测的处理。对其有兴趣的可以看我上一篇的博客
图像处理边缘处理:Roberts算子和canny算子,对圆与矩阵进行识别_靳小锅er的博客-CSDN博客
代码部分:
import cv2
import matplotlib.pyplot as plt
#读取图像信息
img0 = cv2.imread('C:\\Users\\Lenovo\\Desktop\\Pic1_2.jpg')
img1 = cv2.resize(img0, dsize=None, fx = 0.5, fy = 0.5)
img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
cv2.imwrite('C:\\Users\\Lenovo\\Desktop\\Gray.jpg',img2) #保存灰度图
h, w = img1.shape[:2]
print(h, w)
cv2.namedWindow("GREY")
cv2.imshow("GREY", img1)
cv2.waitKey(delay = 0)
#Sobel 算子
img3 = cv2.Sobel (img2, cv2.CV_64F, 0, 1, ksize=5)
cv2.namedWindow("Sobel")
cv2.imshow("Sobel", img3)
cv2.waitKey(delay = 0)
#Laplacian 算子
img7 = cv2.Laplacian(img2, cv2.CV_64F)
cv2.namedWindow("Laplacian")
cv2.imshow("Laplacian", img7)
cv2.waitKey(delay = 0)
#canny 算子
img4 = cv2.Canny(img2, 100, 200)
cv2.namedWindow("Canny")
cv2.imshow("Canny", img4)
cv2.waitKey(delay = 0)
撰写代码时遇到的问题:
CV读取图片所在位置失败:
解决方法:Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案_一路彷徨的专栏-CSDN博客_unicodeescape
代码效果展示:
?
|