如何使用OpenCV在python中处理视频(逐帧)

问题描述 投票:-1回答:1

我正在尝试将视频读入python(无论是实时录制还是无关紧要的视频),然后使用阈值算法处理每一帧,以将视频转换为2色格式。问题是在使用后:

'''ret, frame = cap.read()
    if ret==True:
        frame = otsu.Otsu(frame)'''

使用otsu.Otsu作为图像转换器,然后变量“ frame”不能在otsu模块中与cv.imread一起使用,因为cv.imread需要文件路径,而不是直接将图像传递给它。有没有比阅读更好的功能,还是可以解决的?通过指针而不是图像很有意义,但是我不想保存和删除视频的每一帧。

编辑:这是完整的代码(现在使用的简单阈值仍然不起作用)

import numpy as np
import cv2
from PIL import Image
import PIL

cap = cv2.VideoCapture('Loop_1.mov')

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

        threshed = cv2.threshold(frame,50,255,cv2.THRESH_BINARY)
        newFrame = np.array(threshed)

        cv2.imshow('newFrame',newFrame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
#out.release()
cv2.destroyAllWindows()
python opencv video-capture imread image-thresholding
1个回答
0
投票

这是您需要的。

import cv2

video = cv2.VideoCapture('test_video.mp4')
count = 0
while (True):
    success, image = video.read()
    # cv2.imwrite('frame')
    # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
# cap.release()
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.