调用本地图像进行灰度处理
import cv2 img = cv2.imread('D:\\learn\\python\\t01\\asd.jpg',cv2.IMREAD_GRAYSCALE)#路径为处理的文件路径 cv2.imshow("result", img )#“result”为窗口名 img为图像 #cv2.imwrite("asd.png",img) #存储名为asd.png的照片 cv2.waitKey(0)等待时间 #cv2.waitKey(1000)#等待1s (毫秒单位) cv2.destroyWindow
封装为函数
def cv_show(name,img) cv2.imshow(name,img) cv2.waitKey(0)等待时间 #cv2.waitKey(1000)等待1s (毫秒单位) cv2.destroyWindow
调用摄像头灰度
import cv2 vc = cv2.VideoCapture(0)#0,1为摄像头设备号,也可换“xxx.mp4”记得调整cv2.waitKey() if vc.isOpened(): open, frame = vc.read()#将读取到的 else: open = False while open: ret,frame = vc.read() if frame is None: break if ret == True: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('result',gray) if cv2.waitKey(10) & 0xFF ==27: break vc.release() cv2.destroyWindow()