0
我使用的是Python 2.7和OpenCV 3.3,並且想要在每個定義的幀數或定義的秒數後從立體相機保存圖像。像每隔5幀或每2秒鐘後幀應存儲在不同的文件夾,如cam1folder和cam2folder。我能夠從兩臺相機捕捉視頻,然後使用這些視頻在預定義的秒後捕捉幀。下面給出了這些代碼。Python 2.7/OpenCV 3.3:從立體攝像頭捕獲幀
import cv2
import math
import numpy as np
videoFile = "output.avi"
vidcap = cv2.VideoCapture(videoFile)
success,image = vidcap.read()
videoFile1 = "output1.avi"
vidcap1 = cv2.VideoCapture(videoFile1)
success1,image1 = vidcap1.read()
seconds = 3
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
fps1 = vidcap1.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier1 = fps1 * seconds
while success:
frameId = int(round(vidcap.get(1))) #current frame number, rounded b/c sometimes you get frame intervals which aren't integers...this adds a little imprecision but is likely good enough
success, image = vidcap.read()
frameId1 = int(round(vidcap1.get(1))) #current frame number
success1, image1 = vidcap1.read()
if frameId % multiplier == 0:
cv2.imwrite("right/rframe%d.jpg" % frameId, image) # stored in right folder
if frameId1 % multiplier1 == 0:
cv2.imwrite("left/lframe%d.jpg" % frameId1, image1) # stored in left folder
vidcap.release()
vidcap1.release()
print "Complete"
但我想直接是圖像的實時採集來自視頻而不保存這些:用於保存視頻之後,從兩個攝像頭捕獲幀
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out1 = cv2.VideoWriter('output1.avi',fourcc, 20.0, (640,480))
while(True):
ret, frame = cap.read()
ret1, frame1 = cap1.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
out1.write(frame1)
cv2.imshow('frame',gray)
cv2.imshow('frame1', frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cap1.release()
out1.release()
cv2.destroyAllWindows()
代碼:
用於視頻捕捉代碼視頻,但保存捕獲的圖像在兩個不同的文件夾。這個怎麼做?
謝謝。
謝謝,它完美的作品。我想在這裏提到的一件事是,frame_count ++不被Python支持,因此我將其更改爲frame_count + = 1。第二,我想知道如何在不使用CV_CAP_PROP_POS_FRAMES的情況下計算上面代碼中的幀數。據我所知,初始計數爲0,而條件將爲真,計數將增加,直到frame_count%multiplier == 0爲真。然後,幀將被捕獲,並再次計數將是10.我的查詢是否計數與幀速率同步?意味着我們可以怎樣說增加1點,幀增加1? –
計數與幀頻不同步。你已經添加了一個waitKey(1),這意味着你的循環將在2次迭代之間等待1ms。顯然'cap.read()'也會將主線程保持到接收到新幀爲止 –
我認爲在cv2.waitKey(1)之前都啓動兩個相機幀之間沒有時間延遲。這是真的嗎? –