阅读视频文件VideoCapture

时间:2018-01-31 14:58:29

标签: python opencv image-processing

为什么如果我使用cv2.VideoCapture('video.avi')读取视频文件,fps非常非常低?我希望fps与使用视频播放器(30fps)播放视频时相同。我应该做些什么改变呢?顺便说一下,我正在使用带有python的树莓派3。

import cv2
import numpy as np


cap = cv2.VideoCapture('Fchecking.avi')
kernel = np.ones((5,5), np.uint8)

while(1):

    # Take each frame
    ret, frame = cap.read()
    img2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ret,mask = cv2.threshold(img2gray,140,255,cv2.THRESH_BINARY) 
    w3w = cv2.adaptiveThreshold(mask,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,47,2)   
    cv2.imshow("mask", mask)

    mask_inv = cv2.bitwise_not(w3w) 

    img2_fg = cv2.bitwise_and(frame, frame, mask=mask_inv)

    hsv = cv2.cvtColor(img2_fg, cv2.COLOR_BGR2HSV)

    lower_red= np.array([0,58,130])
    upper_red = np.array([255,255,255])

    erosion = cv2.erode(mask,kernel,iterations = 3)
    rmask = cv2.inRange(hsv, lower_red, upper_red)


    mask2 = cv2.morphologyEx(rmask, cv2.MORPH_CLOSE, kernel)
    final = cv2.bitwise_and(frame, frame, mask=mask2)
    cv2.imshow('final',final)
    cv2.imshow('original',frame)


    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

  

为什么我使用cv2.VideoCapture(' video.avi')读取视频文件时,fps非常非常低?

这个问题的答案是你在阅读框架并将其显示在屏幕之间的大量代码。当您在标准视频播放器中观看视频时,它不会执行此处理。

img2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

ret,mask = cv2.threshold(img2gray,140,255,cv2.THRESH_BINARY) 
w3w = cv2.adaptiveThreshold(mask,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,47,2)   
cv2.imshow("mask", mask)

mask_inv = cv2.bitwise_not(w3w) 

img2_fg = cv2.bitwise_and(frame, frame, mask=mask_inv)

hsv = cv2.cvtColor(img2_fg, cv2.COLOR_BGR2HSV)

lower_red= np.array([0,58,130])
upper_red = np.array([255,255,255])

erosion = cv2.erode(mask,kernel,iterations = 3)
rmask = cv2.inRange(hsv, lower_red, upper_red)


mask2 = cv2.morphologyEx(rmask, cv2.MORPH_CLOSE, kernel)
final = cv2.bitwise_and(frame, frame, mask=mask2)
  

我应该做些什么改变呢?

很有可能:

  1. 如果减少处理速度,则会加快
  2. 如果增加处理能力,它将加速