简体   繁体   English

使用Python OpenCV将图像捕获为数组

[英]Capture Image as Array with Python OpenCV

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. img.tostring()给了我奇怪的字符。 Thanks in adv. 谢谢你。

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. 无论如何,你应该检查opencv python cookbook与PIL和NUMPY包一起使用。

Be sure to get an opencv with numpy support included. 一定要获得包含numpy支持的opencv。 The sequence that works for me is capture an IPL, convert to cvMat, convert to numpy: 适合我的序列是捕获IPL,转换为cvMat,转换为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. 请注意,颜色的表示与pylab假设的不同。 But there comes opencv documentation to your help! 但是有来自opencv文档的帮助!

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. 不过, 这篇博客文章展示了如何使用python从linux上的网络摄像头捕获帧。

According to the docs, QueryFrame returns an IplImage . 根据文档, QueryFrame返回一个IplImage IplImage can be treated as an arbitrary array . IplImage可以视为任意数组 It looks like you'll want to use the "Get" set of functions to access array elements. 您似乎希望使用“Get”函数集来访问数组元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM