0
1

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で出力したaviファイルが再生できない

Posted at

Pythonを用いたOpenCVで撮影した動画のフレーム間の差分を白黒の動画として出力し、その白黒の動画のファイルは生成されたのですが、再生することができない状況です。
デフォルトのOpenCVでは無圧縮のaviファイルしか再生できないと書いてあるのを見たことがあるのですがどう対処していいかわからないので教えて頂きたいです。

-- codig: utf-8 --

import cv2

import time

import numpy as np

def flame_sub(im1,im2,im3,th,blur):

d1 = cv2.absdiff(im3, im2)
d2 = cv2.absdiff(im2, im1)
diff = cv2.bitwise_and(d1, d2)

差分が閾値より小さければTrue

mask = diff < th

背景画像と同じサイズの配列生成

im_mask = np.empty((im1.shape[0],im1.shape[1]),np.uint8)
im_mask[:][:]=255

Trueの部分(背景)は黒塗り

im_mask[mask]=0

ゴマ塩ノイズ除去

im_mask = cv2.medianBlur(im_mask,blur)
return im_mask

if name == 'main':

cam = cv2.VideoCapture("toukasoku_movie.mp4")

im1 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
im2 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
im3 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

out = cv2.VideoWriter('output_toukasoku.avi',-1, 20.0, (640,480))

while True:

ret,frame = cam.read()

フレーム間差分計算

im_fs = flame_sub(im1,im2,im3,5,7)
im1 = im2
im2 = im3
im3 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
out.write(frame)
cv2.imshow("Motion Mask2",im_fs)

フレーム間差分終了

if cv2.waitKey(100) & 0xFF == ord('q'):
cam.release()
out.release()
cv2.destroyAllWindows()
break

def flame_sub2(im4,im5,im6,th2,blur2):

d3 = cv2.absdiff(im6, im5)
d4 = cv2.absdiff(im5, im4)
diff2 = cv2.bitwise_and(d3, d4)

差分が閾値より小さければTrue

mask2 = diff2 < th2

背景画像と同じサイズの配列生成

im_mask2 = np.empty((im4.shape[0],im4.shape[1]),np.uint8)
im_mask2[:][:]=255

Trueの部分(背景)は黒塗り

im_mask2[mask]=0

ゴマ塩ノイズ除去

im_mask2 = cv2.medianBlur(im_mask2,blur2)
return im_mask2

if name == 'main':

cam2 = cv2.VideoCapture("Frame_detection.MP4")
im4 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY)
im5 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY)
im6 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY)

while True:

フレーム間差分の差分計算

cv2.imshow("Motion Mask3",cam2)
im4 = im5
im5 = im6
im6 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY)
key2 = cv2.waitKey(1)

フレーム間差分の差分計算終了

if cam == True:
frame = cv2.flip(frame,0)
out.write(out)

qキーが押されたら

if cv2.waitKey(100) & 0xFF == ord('q'):
cam.release()
out.release()
cv2.destroyAllWindows()
break

if name == 'main':
main()

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?