编写一段python opencv图像预处理的代码,并每步都用一张结果图片展示
时间: 2023-03-13 11:14:26 浏览: 138
基于Python+OpenCV对多张图片进行全景图像拼接源码+项目使用说明(课程设计大作业).zip
5星 · 资源好评率100%
import cv2
import numpy as np # 读取图像
img = cv2.imread("example.jpg")# 灰度化
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", grayImg)
cv2.waitKey(0) # 二值化
ret, thresh = cv2.threshold(grayImg, 127, 255, 0)
cv2.imshow("thresh", thresh)
cv2.waitKey(0) # 腐蚀
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(thresh, kernel, iterations=1)
cv2.imshow("erosion", erosion)
cv2.waitKey(0) # 膨胀
dilation = cv2.dilate(erosion, kernel, iterations=1)
cv2.imshow("dilation", dilation)
cv2.waitKey(0) # 边缘检测
edges = cv2.Canny(dilation, 150, 200)
cv2.imshow("edges", edges)
cv2.waitKey(0) # 显示最终结果
cv2.imshow("final", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文