OpenCV3 Python语言实现 笔记1

来源:互联网 发布:软件安装管理器 知乎 编辑:程序博客网 时间:2024/05/22 04:55

一、

python和numpy表示一幅图像

img = numpy.zeros((3,3), dtype=numpy.uint8)

二、

一个opencv图像是.array类型的二维或三维数组

使用numpy.array访问图像数据
1.单像素操作:获取指定位置(150,120,0)像素: image.item(150,120,0)
             设置B通道位置(150,120)像素为255: image.itemset((150,120,0),255)
2.数组索引: 所有G通道值设为0: img[:,:,1] = 0
3.设定感兴趣区域: my_roi = img[0:100, 0:100]
                  img[300:400,300:400] = my_roi
4.获得图像属性: img.shape 返回(宽,高,通道数)
                img.size 返回像素大小
                img.dtype 返回数据类型

三、

视频读/写

1.视频读:cv2.VideoWriter

import cv2videoCapture = cv2.VideoCapture('MyInputVid.avi')fps = videoCapture.get(cv2.CAP_PROP_FPS)size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),        int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))videoWriter = cv2.VideoWriter(    'MyOutputVid.avi', cv2.VideoWriter_fourcc('I','4','2','0'), fps, size)#其他编码器PIM1,XVID,THEO,FLV1success, frame = videoCapture.read()while success: # Loop until there are no more frames.    videoWriter.write(frame)    success, frame = videoCapture.read()
2.摄像头:

cameraCapture = cv2.VideoCapture(0)size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),        int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))success, frame = cameraCapture.read()cameraCapture.release()cameraCapture.isOpened()#是否正确打开#同步一组摄像头:success0=cameraCapture0.grab()success0=cameraCapture1.grab()if success0 and success1:    frame0 = cameraCapture0.retrieve()    frame1 = cameraCapture1.retrieve()


阅读全文
1 0
原创粉丝点击