I am reading a video frame by frame using:
vc = cv2.VideoCapture('test.avi')
and later on I check whether the frame has been read using:
if vc.isOpened():
rval,frame = vc.read()
else:
rval = False
Now the problem is , if I try to convert this frame to a numpy array using the following code:
PILImage = Image.fromstring("L",cv.GetSize(frame),frame.tostring())
NumPyArray = np.array(PILImage)
I get an error saying:
CvArr argument 'arr' must be IplImage, CvMat or CvMatND. Use fromarray() to convert numpy arrays to CvMat or cvMatND
From Documentation of VideoCapture::read I found that it returns two things [retVal,Image] . How do I get the Image part only and how do I convert it to Numpy Array?
Further Info:
Basically the whole point in doing this is that ,I am trying to write a program which allows me to go through each frame of the video by pressing spacebar, and select a particular region from any frame and save it as a jpg. Here is the Code:
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 Class:
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)
You are over-complicating things, the image returned in your
framename is already a numpy array. If you want to convert it toPIL, simply doImage.fromarray(frame).