LoginSignup
7
3

More than 3 years have passed since last update.

[OpenCV]動画を表示する方法

Last updated at Posted at 2020-12-03

はじめに

OpenCVによる画像処理の基礎を整理します。
100本ノックのように、毎日追加していきたいと思います。

やりたいこと

mp4の動画ファイル、WebCamからの映像をパソコン画面上に表示したいと思います。

プログラム構造

cv2.VideoCapture()を利用します。

#動画ファイル
cap = cv2.VideoCapture('video.mp4')
#内臓カメラ
cap = cv2.VideoCapture(0)
#USBカメラ
cap = cv2.VideoCapture(1)

そして、cap.read()から動画のフレームをimgに入れ、それをimshow()で表示します。
このプロセスをWhile文を利用して繰り返し実行します。←ここがみそ。

while True :
    ret, img = cap.read()
    cv2.imshow('Video', img)

実行結果

bandicam-2020-12-02-17-27-43-031.gif

まとめ

  1. cv2.videoCapture(), while文でビデオ映像を画面に表示する方法を確認しました。

全体コード

import cv2

# Video
frameWidth = 640
frameHeight = 480

#Video Source
#cap = cv2.VideoCapture('videos/traffic.mp4') #自分のmp4のpathを入力
cap = cv2.VideoCapture(0)



while True:
    ret, img = cap.read()
    img = cv2.resize(img, (frameWidth, frameHeight))
    cv2.imshow('Video', img)
    print('ret=', ret)

    # qを押すと止まる。
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


おまけ

cv2.imshow()で表示されたウィンドウを拡大すると画素のRGB値が確認可能です。
Video_screenshot_02.12.2020.png

7
3
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
7
3