VideoCapture()
メソッドを使用して特定のフレームを取得する方法はありますか?OpenCV/Python:VideoCaptureを使用して特定のフレームを読み取る
私の現在のコードは次のとおりです。
import numpy as np
import cv2
cap = cv2.VideoCapture('video.avi')
Thisは私のリファレンスチュートリアルです。
VideoCapture()
メソッドを使用して特定のフレームを取得する方法はありますか?OpenCV/Python:VideoCaptureを使用して特定のフレームを読み取る
私の現在のコードは次のとおりです。
import numpy as np
import cv2
cap = cv2.VideoCapture('video.avi')
Thisは私のリファレンスチュートリアルです。
GPPKありがとうございます。
ビデオパラメータは整数で指定する必要があります。各フラグには独自の値があります。コードについては、hereを参照してください。
正しい解決策は次のとおりです。
import numpy as np
import cv2
#Get video name from user
#Ginen video name must be in quotes, e.g. "pirkagia.avi" or "plaque.avi"
video_name = input("Please give the video name including its extension. E.g. \"pirkagia.avi\":\n")
#Open the video file
cap = cv2.VideoCapture(video_name)
#Set frame_no in range 0.0-1.0
#In this example we have a video of 30 seconds having 25 frames per seconds, thus we have 750 frames.
#The examined frame must get a value from 0 to 749.
#For more info about the video flags see here: https://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
#Here we select the last frame as frame sequence=749. In case you want to select other frame change value 749.
#BE CAREFUL! Each video has different time length and frame rate.
#So make sure that you have the right parameters for the right video!
time_length = 30.0
fps=25
frame_seq = 749
frame_no = (frame_seq /(time_length*fps))
#The first argument of cap.set(), number 2 defines that parameter for setting the frame selection.
#Number 2 defines flag CV_CAP_PROP_POS_FRAMES which is a 0-based index of the frame to be decoded/captured next.
#The second argument defines the frame number in range 0.0-1.0
cap.set(2,frame_no);
#Read the next frame from the video. If you set frame 749 above then the code will return the last frame.
ret, frame = cap.read()
#Set grayscale colorspace for the frame.
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Cut the video extension to have the name of the video
my_video_name = video_name.split(".")[0]
#Display the resulting frame
cv2.imshow(my_video_name+' frame '+ str(frame_seq),gray)
#Set waitKey
cv2.waitKey()
#Store this frame to an image
cv2.imwrite(my_video_name+'_frame_'+str(frame_seq)+'.jpg',gray)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
あなたが正確なフレームをしたい場合は、あなただけのそのフレームにVideoCaptureセッションを設定することができます。そのフレームを自動的に呼び出す方がはるかに直感的です。 「正しい」ソリューションでは、既知のデータ(たとえば、fps、長さ、その他)を入力する必要があります。以下のコードで知る必要があるのは、呼び出すフレームだけです。
import numpy as np
import cv2
cap = cv2.VideoCapture(video_name) #video_name is the video being called
cap.set(1,frame_no); # Where frame_no is the frame you want
ret, frame = cap.read() # Read the frame
cv2.imshow('window_name', frame) # show frame on window
あなたが終了を押すまで、ウィンドウを保持したい場合:
while True:
ch = 0xFF & cv2.waitKey(1) # Wait for a second
if ch == 27:
break
'cap.set(1、frame_no)'の1は何ですか? – Moondra
下記の私の答えを見てください。 –
はい、それは前方に非常にストレートです:
import cv2
cap = cv2.VideoCapture(videopath)
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frame_number-1)
res, frame = cap.read()
'frame_number' は範囲0の整数であります... amount_of_frames。注意: 'frame_number-1'はフレーム 'frame_number'を読み込むように設定する必要があります。それはうまく説明されていませんが、テストはVideoCaptureモジュールの動作を示しています。
'res'は論理演算結果であり、フレームが正常に読み込まれたかどうかを確認するために使用されることがあります。 一つによりフレームの量を取得してもよい: `cap.setを()`使用3.5` Pythonの `で
amount_of_frames = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
'cv2.cv.CV_CAP_PROP_FRAME_COUNT'は利用できませんでした。私は 'cv2.CAP_PROP_FRAME_COUNT'を見つけました – user3731622
あなたはcv 3.xを使っていると思います。 Opencv 3.xは、cv2モジュール内でCV_接頭辞を持たない定数を定義します。 Opencv 2.xは、それらをcv2.cvモジュール内で、CV_接頭辞で定義します。 –
、最初の引数は0.0〜1.0の範囲内のフレーム数を定義します。したがって、 'cap.set(1、frame_no)'です。 – zinon
cap.set()の最初の引数は、 '1'または '2'の魔法なしで 'cv2.cv.CV_CAP_PROP_POS_FRAMES'にする必要があります。 2番目のフレーム番号は範囲0のフレーム番号です。 - cv2.cv.CV_CAP_PROP_FRAME_COUNT - 整数であり、浮動小数点数は0.0〜1.0ではありません! –