12
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

動画を早送りして任意フレームから開始し任意間隔で画面キャプチャを保存するOpenCV

Last updated at Posted at 2017-05-31

環境

  • Python 3.5.2
  • OpenCV 3.1.0

コード

slicemovie.py
# -*- coding: utf-8 -*-
import cv2

cap = cv2.VideoCapture('Video_001.avi')

frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(cap.get(cv2.CAP_PROP_FPS))

# 総フレーム数とFPSを確認
print("FRAME_COUNT: ",  frame_count)
print("FPS: ", fps )

# 20分ぐらいのところまで早送り
start_pos = fps * (60 * 20)

# フレームポジションをファイル名にして、1秒4枚ぐらいの気持ちで画像保存
print("-- start")
for idx in range(start_pos, frame_count, round(fps/4)):
	cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
	current_pos = str(int(cap.get(cv2.CAP_PROP_POS_FRAMES)))
	cv2.imwrite("pict/" + current_pos +".jpg", cap.read()[1])
print("-- done.")

cap.release()

結果
result.PNG

(おまけ)結果を横に適宜三枚繋いで一枚の画像を作る

‘Boring parking, active background’ ってタイトル、なんだかいいですよね。
 
out.jpg

concatenate.py
# -*- coding: utf-8 -*-
import cv2
import numpy as np

print ("start.")

img1= cv2.imread("30000.jpg")
img2= cv2.imread("30666.jpg")
img3= cv2.imread("32544.jpg")

vis = np.concatenate((img1, img2, img3), axis=1)
cv2.imwrite('out.jpg', vis)

print ("done.")
12
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?