2018-6-25 北京 似是暴雨前
2.1 基本I/O脚本
2.1.2 图像与原始字节之间的转换
import cv2
import numpy
# import os
# 构建一个120,000随机字节的序列
# randomByteArray = bytearray(os.urandom(120000))
# flatNumpyArray = numpy.array(randomByteArray)
# 把这个序列转换成400*300的灰度级图像
# grayImage = flatNumpyArray.reshape(300, 400)
grayImage = numpy.random.randint(0, 256, 120000).reshape(300, 400)
cv2.imwrite('RandomGray.png', grayImage)
# 把这个序列转换成400*100的彩色图像
# bgrImage = flatNumpyArray.reshape((100, 400, 3))
bgrImage = numpy.random.randint(0, 256, 120000).reshape(100, 400, 3)
cv2.imwrite('RandomColor.png', bgrImage)
2018-6-27 天气真好啊~
2.1.3 使用numpy.array
访问图像数据
import cv2
import numpy as np
img = cv2.imread('jx3qunxiang.jpg')
# 显示像素(150, 120)当前的B值
print(img.item(150, 120, 0))
img.itemset((150, 120, 0), 255)
print(img.item(150, 120, 0))
# 将R通道的所有值置为0
img[:, :, 2] = 0
cv2.imwrite('jx3qunxiang_no_red.jpg', img)
# 一块区域替换另一块同样大的区域
my_roi = img[0:100, 0:100]
img[150:250, 150:250] = my_roi
cv2.imwrite('jx3qunxiang_replace.jpg', img)
注意到,B、G、R三个通道的索引分别为0、1、2。
2.1.4 视频文件的读写
import cv2
videoCapture = cv2.VideoCapture('j3logo.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('j3logo_output.avi', cv2.VideoWriter_fourcc('F', 'L', 'V', '1'), fps, size)
success, frame = videoCapture.read()
print('Success value is %s' % success)
while success:
# 循环到没有帧
videoWriter.write(frame)
success, frame = videoCapture.read()
注意,需要给VideoWriter()
类的构造函数指定视频文件名、视频编解码器、帧速率和帧大小。
后两者可以通过videoCapture()
类的get()
函数得到。
2.1.5 捕获摄像头的帧
import cv2
# 与视频文件cv2.VideoCapture('j3logo.avi')相比
# 构造VideoCapture类是通过摄像头的设备索引---cv2.VideoCapture(0)---0是摄像头的device index
cameraCapture = cv2.VideoCapture(0)
fps = 30
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('MySelf.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
success, frame = cameraCapture.read()
# print('Success value is %s' % success)
# VideoCapture类的get()方法不能返回摄像头帧速率的准确值,总是返回0
# 但是保存视频文件需要帧速率,需要针对摄像头创建合适的VideoWriter类
# 一种方法是对帧速率做出假设,如下方法
# 一种是使用计时器测量
numFramesRemaining = 10 * fps - 1
while success and numFramesRemaining > 0 :
videoWriter.write(frame)
success, frame = cameraCapture.read()
# print(cameraCapture.isOpened())
numFramesRemaining -= 1
cameraCapture.release()
2.1.6 在窗口显示图像
import cv2.
img = cv2.imread('jx3qunxiang.jpg')
cv2.imshow('MyPic', img)
cv2.waitKey()
# 释放所有由opencv创建的窗口
cv2.destroyAllWindows()
2.1.7 在窗口显示摄像头帧
import cv2
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.EVENT_LBUTTONUP:
clicked = True
cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('MyWindow')
# 鼠标回调函数(鼠标事件相应)
cv2.setMouseCallback('MyWindow', onMouse)
print('Showing camera feed.Click window or press any key to stop.')
success, frame = cameraCapture.read()
# waitKey()中的参数是等待键盘触发的时间,单位是ms,返回值是-1代表没有键被按下
while success and cv2.waitKey(1) == -1 and not clicked:
cv2.imshow('MyWindow', frame)
success, frame = cameraCapture.read()
cv2.destroyWindow('MyWindow')
cameraCapture.release()