Python - 从视频中提取独特的帧

5

我正在尝试从视频中提取不同的帧。我使用了opencv(以下代码),它可以让我从视频中提取所有帧,但我要找的是不重叠的独特图像。有什么建议吗?谢谢。

import cv2
import os

video = cv2.VideoCapture("INPUT.MOV")

currFrame =0

while(True):
   ret,frame = video.read()

   if ret:
       name = './data/frame'+ str(currFrame) + '.jpg'

       cv2.imwrite(name, frame)

       currFrame += 1
   else:
       break

video.release()
cv2.destroyAllWindows()

编辑: 这是一段连续的视频。例如,https://www.youtube.com/watch?v=JBLQbOG8Z1Y,我需要天际线的不同镜头,即第1秒和第6秒各一个。

第1秒
第6秒


3
从一个框架到另一个框架,“唯一”图像的定义是什么? - alkasm
如果第一张图片覆盖了视频的第一和第二秒,则第二张图片应该包含视频的第三和第四秒。因此,第二帧应该从第一帧的边缘开始。如果这样说不清楚,请告诉我,否则我会用另一种方式解释。 - Nandhiny Durai
你能详细说明一下吗?你的视频是包含不同图片的幻灯片,还是一个连续的视频?请定义“独特”。 - A.k.
我已经更新了问题并提供了一个例子。希望它清晰明了。 - Nandhiny Durai
1个回答

1
如果您想要按秒计算,可以将代码分割并检查每秒的第一帧(每秒帧数)。
video = cv2.VideoCapture("INPUT.MOV")
  
try:
    # creating a folder named data
    if not os.path.exists('secondWiseData'):
        os.makedirs('secondWiseData')

except OSError:
    print ('Error: Creating directory of data')

currentframe = 0

#Check the version of opencv you are using
# Find OpenCV version

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

# if int(major_ver)  < 3 :
fps = int(video.get(cv2.cv.CV_CAP_PROP_FPS))
#Else    
fps = int(video.get(cv2.CAP_PROP_FPS))

#The FPS generated could be decimal as well. We get its greatest interger function by casting to int().

print('The FPS of the video is ', str(fps))
time_start=time.time()
    
while(True):
      
    # reading from frame
    ret,frame = cam.read()
  
    if ret:
        # if video is still left continue creating images
        name = './secondWiseData/frame' + str(currentframe) + '.jpg'
        
        if currentframe % fps == 1: 
            print ('Creating...' + name)
            cv2.imwrite(name, frame)
            currentframe += 1
            continue
        else:
            currentframe += 1
            continue
  
        # increasing counter so that it will
        # show how many frames are created
        
    else:
        break

time_end=time.time()
print('Total time taken is ', time_end-time_start)    
print('Total frames are ', currentframe)

#Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
这将为您提供考虑视频中每秒的帧数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接