gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:30 27 4
gpt4 key购买 nike

我正在逐帧阅读视频:

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 参数“arr”必须是 IplImage、CvMat 或 CvMatND。使用 fromarray() 将 numpy 数组转换为 CvMat 或 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)

关于python - 使用 read() 从 VideoCapture 对象获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com