使用 read() 从 VideoCapture 对象获取图像



我正在使用以下方法逐帧读取视频:

vc = cv2.VideoCapture('test.avi')

稍后我使用以下方法检查是否已读取frame

if vc.isOpened():
    rval,frame = vc.read()
else:
    rval = False

现在的问题是,如果我尝试使用以下代码将此frame转换为numpy数组:

PILImage = Image.fromstring("L",cv.GetSize(frame),frame.tostring())
NumPyArray = np.array(PILImage)

我收到一个错误,说:

CvArr argument 'arr' must be IplImage, CvMat or CvMatND. Use fromarray() to convert numpy arrays to CvMat or cvMatND

从 VideoCapture 的文档::read 我发现它返回了两件事[retVal,Image]。如何仅获取Image部分以及如何将其转换为 Numpy 数组?

更多信息:基本上这样做的全部意义在于,我正在尝试编写一个程序,该程序允许我通过按空格键浏览视频的每一帧,并从任何帧中选择一个特定区域并将其另存为 jpg。这是代码:

from ITMS import ITMS
import cv2
from cv2 import cv
import numpy as np
import matplotlib.pyplot as plt
import Image
import matplotlib.widgets as widgets
def onselect(eclick, erelease):
    if eclick.ydata>erelease.ydata:
        eclick.ydata,erelease.ydata=erelease.ydata,eclick.ydata
    if eclick.xdata>erelease.xdata:
        eclick.xdata,erelease.xdata=erelease.xdata,eclick.xdata
    ax.set_ylim(erelease.ydata,eclick.ydata)
    ax.set_xlim(eclick.xdata,erelease.xdata)
    fig.canvas.draw()
def subImager(arr):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt_image=plt.imshow(arr,cmap="Greys_r")
    rs=widgets.RectangleSelector(
        ax, onselect, drawtype='box',
        rectprops = dict(facecolor='red', edgecolor = 'red', alpha=0.2, fill=True))
    plt.show()

cv2.namedWindow("preview")
vc = cv2.VideoCapture('test.avi')
if vc.isOpened():
    rval,frame = vc.read()
else:
    rval = False
while rval:
    key = cv2.waitKey(30)
        if key==32:
            cv2.imshow("preview", frame)
            NumPyArray=ITMS.CVtoNPArray(frame)
            subImager(NumPyArray)
            rval,frame = vc.read()
        elif key==27:
            break
cv2.destroyAllWindows()

ITMS 类:

from cv2 import cv
import cv2
import numpy as np
from PIL import Image
class ITMS:
    def __init__(self):
        pass
    def CVtoNPArray(CVImage):
        PILImage = Image.fromstring("L",cv.GetSize(CVImage),CVImage.tostring())
        NumPyArray = np.array(PILImage)
        return NumPyArray
        CVtoNPArray=staticmethod(CVtoNPArray)
你把

事情复杂化了,以你的frame名返回的图像已经是一个 numpy 数组。如果要将其转换为 PIL ,只需执行Image.fromarray(frame)

相关内容

  • 没有找到相关文章