3

I've seen from the sample how to display an image from the webcam, but how do I get the image captured as an array?

import cv

capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)

img.tostring() gives me weird caracters. Thanks in adv.

1
  • why do you want to capture it in array? whats the exact requirement? Commented Aug 20, 2010 at 7:22

4 Answers 4

3

I think this is what you're looking for:

img=cv.LoadImage("asd.png")
mat=cv.GetMat(img)
mat[3,1]
(83.0, 88.0, 89.0)

anyway, you should check opencv python cookbook for use with PIL and NUMPY packages.

Sign up to request clarification or add additional context in comments.

Comments

2

Be sure to get an opencv with numpy support included. The sequence that works for me is capture an IPL, convert to cvMat, convert to numpy:

import cv, numpy, pylab
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
mat=cv.GetMat(img)
a = numpy.asarray(mat)
pylab.imshow(a)

Note that the representation of color is different from what pylab assumes. But there comes opencv documentation to your help!

Comments

1

I believe your problem might be the way you are printing/displaying the contents of the array.

Nevertheless, this blog post shows how to capture frames from a webcam on linux using python.

Comments

0

According to the docs, QueryFrame returns an IplImage. IplImage can be treated as an arbitrary array. It looks like you'll want to use the "Get" set of functions to access array elements.

1 Comment

Thanks, I didn't knew how to manipulate it. I had found a way to convert it to a PIL image and then accesses as an array. img_array = Image.fromstring("RGBA",cv.GetSize(img), img.tostring() )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.