python OpenCV中的帧操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python OpenCV中的帧操作相关的知识,希望对你有一定的参考价值。

#frame rate finder...........

import cv2


video = cv2.VideoCapture("v1.mp4");
 
# Find OpenCV version
# (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
 
# if int(major_ver)  < 3 :
    # fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
    # print ("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
# else :
fps = int(video.get(cv2.CAP_PROP_FPS))
# print ("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
print ("Frames per second  : {0}".format(fps))

video.release();
import cv2
import numpy as np
import os
 
# set video file path of input video with name and extension
vid = cv2.VideoCapture('6.mp4')
 
 
if not os.path.exists('images'):
    os.makedirs('images')
 
#for frame identity
index = 0
while(True):
    # Extract images
    ret, frame = vid.read()
    # end of frames
    if not ret: 
        break
    # Saves images
    name = './images/frame' + str(index) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)
 
    # next frame
    index += 1
# its working code of frame jump
import cv2
import numpy as np
# imporelse:
    # t time
 
cap = cv2.VideoCapture("6.mp4")
# here "6.mp4" used as a video reference..................
# time.sleep(2)
# fps = int(cap.get(cv2.CAP_PROP_FPS))
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# step=20
desired_frames = np.arange(frameCount,step=20)
 
if cap.isOpened() is False:
    print("Error opening video stream or file")      
 
# while cap.isOpened():
while True :
    for i in desired_frames:
        cap.set(1,i-1)  
        success,image = cap.read(1)
        # if success==True:
                  
        frameId = cap.get(1)
              
        cv2.imshow('Test', image)
          
         
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        # else:
            # break
        # cv2.waitKey(25)
        
cap.release()    
cv2.destroyAllWindows()
import cv2

cap = cv2.VideoCapture(0)

# cap.set(3, 2280)
# cap.set(4, 2020)

while(cap.isOpened()):
    ret, frame = cap.read()

     
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH )
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT )
    print(width,height)
    
    
    cv2.imshow('frame',frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

以上是关于python OpenCV中的帧操作的主要内容,如果未能解决你的问题,请参考以下文章

将相机中的帧写入OpenCV中的单个图像

opencv 提取avi视频中的帧,逐帧播放

无法使用 OpenCV 从辅助网络摄像头读取 VideoCapture 中的帧

python 3.x tkinter,将来自opencv cv2的帧集成到tkinter窗口中

OpenCV 不报告准确的帧速率/计数

如何将函数捕获的帧从另一个库传递给 OpenCV 函数?