OpenCV处理文件、视频和摄像头

图像的本质(图像可以用数组来表示)

import numpy as np
import cv2

img = np.zeros((3, 3), dtype=np.uint8)
print(img, img.dtype)

img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
print(img, img.dtype, img.shape)

图片格式转换(jpg->png)

image = cv2.imread("img/1.jpg")
cv2.imwrite("img/1.png",image)

使用numpy.array访问图像数据

image = cv2.imread("img/1.jpg")
image[0:100,0:100] = [255,255,255]
cv2.imshow("Demo",image)
cv2.waitKey(0)

图像的属性

image = cv2.imread("img/1.jpg")
print(image.shape)
print(image.size)
print(image.dtype)

shape:图像的宽度、高度和通道数

size:图像的大小=宽*高*通道数

dtype:图像像素值的数据类型

视频类型转换

import numpy as np
import cv2

videoCapture = cv2.VideoCapture("img/小杰克的攻击.mp4")
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("output.avi", cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
success ,frame = videoCapture.read()
while success:
    videoWriter.write(frame)
    success,frame = videoCapture.read()

 

posted @ 2018-12-27 19:38  逐梦客!  阅读(1273)  评论(0编辑  收藏  举报