Capture Image as Array with Python OpenCV


Question

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
3
8/20/2010 7:05:09 AM

Accepted Answer

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.

3
6/21/2019 3:30:21 PM

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!


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon