1.基本I/O脚本
1.1图片读取
1 import time 2 3 num = 2-1-3+3 4 print(num) 5 # 打开显示图像 6 if num == 1: 7 # 读取图像,支持 bmp、jpg、png、tiff 等常用格式 8 img = cv2.imread(r"D:UIAUTO rainingSetsimpleGeometricFigure1.jpg") 9 # 创建窗口并显示图像 10 cv2.namedWindow("Image") 11 cv2.imshow("Image", img) 12 # 刷新窗口 13 cv2.waitKey(0) 14 # 释放窗口 15 cv2.destroyAllWindows()
1.2图像与原始字节之间转换
1 # 2.1.2 图像与原始字节之间的转换 2 # 随机生成两个图像,他们位于脚本所在的目录,图像名为RandomGray.png 和 RandomColor.png. 3 if num == 2-1-2: 4 randomByteArray = bytearray(os.urandom(120000)) 5 flatNumpyArray = numpy.array(randomByteArray) 6 7 grayImage = flatNumpyArray.reshape(300, 400) 8 cv2.imwrite('RandomGray.png', grayImage) 9 bgrImage = flatNumpyArray.reshape(100, 400, 3) 10 cv2.imwrite('RandomColor.png', bgrImage)
1.3使用numpy.array访问图像数据
1 # 2.1.3 使用numpy.array访问图像数据 2 if num == 2-1-3+1: 3 img = cv2.imread(r"D:UIAUTO rainingSetsimpleGeometricFigure1.jpg") 4 # 将jpg图像在左上宽50高50区域像素,处转化为红色像素 5 for a in range(0, 50): 6 for b in range(0, 50): 7 img[a, b] = [2, 3, 255] 8 cv2.imwrite("1.jpg", img) 9 10 # item(x,y,通道索引0-2) 11 print("x:49 y:49 R:", img.item(49, 49, 0)) 12 print("x:49 y:49 G:", img.item(49, 49, 1)) 13 print("x:49 y:49 B:", img.item(49, 49, 2)) 14 15 # 把区域像素设置为白色,不改变读入的图片像素 16 for x in range(0, 50): 17 for y in range(0, 50): 18 img.itemset((x, y, 0), 250) 19 img.itemset((x, y, 1), 250) 20 img.itemset((x, y, 2), 250) 21 cv2.imwrite("21.jpg", img)
1.4像素变换
1 # 像素变换 2 # 这种变化速度优于上面的循环变换 3 if num == 2-1-3+2: 4 img = cv2.imread(r"D:PythonWorkspacedeep leanrvenv21.jpg") 5 img[0:200:1, 0:200:1, 1] = 2 6 cv2.imwrite('22.jpg', img) 7 time.sleep(2)
1.5图像区域拷贝
1 # 图像区域拷贝 2 if num == 2-1-3+3: 3 img = cv2.imread(r'D:PythonWorkspacedeep leanrvenv18151.jpg') 4 my_roi = img[0:100:1, 0:100:1] 5 img[300:400:1, 300:400:1] = my_roi 6 cv2.namedWindow("Image") 7 cv2.imshow("Image", img) 8 cv2.waitKey(0) # 刷新时间 9 cv2.destroyAllWindows() # 释放窗口 10 print(img.shape) # 返回一个数组: [宽度,高度,通道数] 11 print(img.size) # 像素大小 12 print(img.dtype) # 图像的数据类型
2.视频I/O与捕获摄像头帧
2.1视频读取
1 import cv2 2 3 4 # I/O视频文件 5 if num == 1: 6 videoCapture = cv2.VideoCapture('MyInputVid.avi') 7 fps = videoCapture.get(cv2.CAP_PROP_FPS) 8 size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), 9 int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 10 ) 11 videoWriter = cv2.VideoWriter('MyOutputVid.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size) 12 success, frame = videoCapture.read() 13 while success: # Loop until there are no more frames. 14 videoWriter.write(frame) 15 success, frame = videoCapture.read()
2.2捕获摄像头的帧,写入磁盘
1 if num == 2: 2 cameraCapture = cv2.VideoCapture(0) 3 fps = 30 # an assumption 4 size = ( 5 int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), 6 int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 7 ) 8 videoWriter = cv2.VideoWriter('MyOutputVid.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size) 9 10 success, frame = cameraCapture.read() 11 numFrameRemaining = 10 * fps - 1 12 while success and numFrameRemaining > 0: 13 videoWriter.write(frame) 14 success, frame = cameraCapture.read() 15 numFrameRemaining -= 1 16 cameraCapture.release()
2.3在窗口显示摄像头帧,按键盘大部分键位关闭窗口
1 # 在窗口显示摄像头帧 2 # waitKey()的参数为等待键盘触发的时间, 单位为毫秒, 其返回值是-1(表示没有键被按下)或ASCLL码. 3 # 另外,python提供了一个标准函数ord(), 该函数可以将字符转换为ASCLL码. 例如, 输入ord('a')会返回97 4 5 # OpenCV的窗口函数和waitKey()函数互相依赖.OpenCV的窗口只有在调用waitKey()函数时才会更新, waitKey()函数只有在OpenCV 6 # 窗口成为活动窗口时,才能捕获输入信息. 7 8 # 鼠标回调函数setMouseCallback()有五个参数, 如前面的实列代码所示.param为可选参数, 9 # 它是setMouseCallback()函数的第三个参数, 默认情况下, 该参数是0. 回调事件参数可以取如下的值, 他们分别对应不同的鼠标事件. 10 # cv2.EVENT_MOUSEMOVE: 该事件对应鼠标移动 11 # cv2.EVENT_LBUTTONDOWN: 鼠标左键按下 12 # cv2.EVENT_RBUTTONDOWN: 鼠标右键按下 13 # cv2.EVENT_MBUTTONDOWN: 鼠标中间键按下 14 # _LBUTTONUP: 鼠标左键松开 15 # _RBUTTONUP: 16 # _MUBTTONUP: 17 # _LBUTTONDBLCLK: 双击鼠标左键 18 # _RBUTTONDBLCLK: 19 # _MBUTTONDBLCLK: 20 # 鼠标回调的标志参数可能是一下事件的按位组合 21 # cv2.EVENT_FLAG_LUBTTON: 该事件对应按下鼠标左键 22 # cv2.EVENT_FLAG_RBUTTON: 该事件对应按下鼠标右键 23 # cv2.EVENT_FLAG_MBUTTON: 该事件对应按下鼠标中间键 24 # _FLAG_CTRLKEY: 按下Ctrl键 25 # _FLAG_SHIFTKEY: 按下Shift键 26 # _FLAG_ALTKEY: 按下Alt键 27 # 然而,OpenCV不提供任何处理窗口事件的方法. 例如, 当单击窗口的关闭按钮时, 并不能关闭应用程序. 28 # 由于OpenCV有限的事件处理能力和GUI处理能力, 许多开发人员更喜欢将OpenCV集成到其他应用程序框架中. 29 if num == 3: 30 clicked = False 31 def onMouse(event, x, y, flags, param): 32 global clicked 33 if event == cv2.EVENT_LUBTTONUP: 34 clicked = True 35 cameraCapture = cv2.VideoCapture(0) 36 cv2.namedWindow('MyWindow') 37 cv2.setMouseCallback('MyWindow', onMouse) 38 39 print('Showing camera feed. Click window or press any key to stop') 40 success, frame = cameraCapture.read() 41 while success and cv2.waitKey(1) == -1 and not clicked: 42 cv2.imshow('MyWindow', frame) 43 success, frame = cameraCapture.read() 44 45 cv2.destroyWindow('MyWindow') 46 cameraCapture.release()
GitHub代码示例地址: