Skip to content

Instantly share code, notes, and snippets.

@Roger8
Last active November 7, 2018 07:59
Show Gist options
  • Save Roger8/266bd516fb667ae56d7949e33a69e8e9 to your computer and use it in GitHub Desktop.
Save Roger8/266bd516fb667ae56d7949e33a69e8e9 to your computer and use it in GitHub Desktop.
【opencv】

opencv 中函数的用法例子**

@Roger8
Copy link
Author

Roger8 commented Sep 3, 2018

写视频

import cv2
import os

#图片路径
im_dir = '/home/suanfa/data/out/201708231503440'
#输出视频路径
video_dir = '/home/suanfa/data/out/201708231503440-1018.avi'
#帧率
fps = 30  
#图片数 
num = 426
#图片尺寸
img_size = (841,1023)

#fourcc = cv2.cv.CV_FOURCC('M','J','P','G')#opencv2.4
fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0
videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)

for i in range(1,num):
    im_name = os.path.join(im_dir, str(i).zfill(6)+'.jpg')
    frame = cv2.imread(im_name)
    videoWriter.write(frame)
    print im_name

videoWriter.release()
print 'finish'

@Roger8
Copy link
Author

Roger8 commented Oct 23, 2018

读视频 获取视频参数:帧数, 宽/高,fps

cap = cv2.VideoCapture('MOT16-07.mp4')
# 帧数
cap.get(cv2.CAP_PROP_FRAME_COUNT)
# fps
cap.get(cv2.CAP_PROP_FPS)
#  以帧为单位的当前位置
cap.get(cv2.CAP_PROP_POS_FRAMES)
# 跳到指定帧
# jump to specific frame
framestart = 150
cap.set(cv2.CAP_PROP_POS_FRAMES, int(framestart))
ret ,frm= cap.read()
# 宽/高
cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
cap.get(cv2.CAP_PROP_FRAME_WIDTH)
# 编码方式
codec = int(cap.get(cv2.CAP_PROP_FOURCC)) # 828601953
chr(codec&0xFF) + chr((codec>>8)&0xFF) + chr((codec>>16)&0xFF) + chr((codec>>24)&0xFF) # 转换为字符 avc1 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment