linux – 让BeagleBone使用OpenCV捕获静态帧
我有一个运行
?ngstr?m Linux 3.2.28的BeagleBone,我正试图从我的相机中捕捉一帧.
所以我插入了我的USB网络摄像头,并检查/ dev以确保它显示出来. 它确实如同视频0(右下).我知道这是正确的,因为它在我拔掉相机后就消失了. 所以现在我启动Python并运行以下命令: root@beaglebone:/dev# python Python 2.7.2 (default,Sep 11 2012,16:15:43) [GCC 4.5.4 20120305 (prerelease)] on linux2 Type "help","copyright","credits" or "license" for more information. >>> import cv >>> capture=cv.CaptureFromCAM(-1) >>> img=cv.QueryFrame(capture) >>> type(capture) <type 'cv2.Capture'> >>> type(img) <type 'NoneType'> 如您所见,我能够充分创建捕获对象,但我无法从中拉出帧.我也用相机ID(上面代码中的-1)的不同(或没有)整数参数尝试了这个,但无济于事. 作为参考,在IPython上我的笔记本电脑上运行相同的代码如下所示: In [1]: import cv In [2]: capture=cv.CaptureFromCAM(-1) In [3]: img=cv.QueryFrame(capture) In [4]: type(capture) Out[4]: cv2.Capture In [5]: type(img) Out[5]: cv2.cv.iplimage 你可以看到我在这里正在拍摄一张图片.我不确定从这里到底要去哪里. 更新: 我在FFmpeg玩了一下,并且能够通过发出以下命令让相机响应(也就是说,它继续亮起): root@beaglebone:/# ffmpeg -f video4linux2 -i /dev/video0 这很有趣,因为apparently CaptureFromCAM使用的是V4L界面……我不知道从哪里开始. 解决方法
非常
first thing
you need to do确保CaptureFromCAM()成功:
import cv capture = cv.CaptureFromCAM(-1) if not capture: print "Unable to open device #-1" sys.exit(1) 发送-1作为参数告诉OpenCV打开默认的摄像头设备.在某些系统上,这不起作用,您需要增加数量.尝试传递0,然后传递1和更高版本2. 您需要做的第二件事是确保QueryFrame()返回有效的内容: img = cv.QueryFrame(capture) if not img: print "Unable to retrieve frame from the device" sys.exit(1) 我见过OpenCV的Python API和C(甚至是C)API之间的奇怪行为.如果以上都没有帮助您解决问题,我建议您使用OpenCV编译C程序(具有最可靠的API)从相机检索数据.在某些情况下,OpenCV的C API工作和Python没有. This C program从相机中检索帧并将其显示在窗口中: #include <stdio.h> #include <highgui.h> #include <cv.h> int main() { CvCapture* capture = NULL; if ((capture = cvCaptureFromCAM(-1)) == NULL) { fprintf(stderr,"ERROR: capture is NULL n"); return -1; } cvNamedWindow("mywindow",CV_WINDOW_AUTOSIZE); cvQueryFrame(capture); // Sometimes needed to get correct data while (1) { IplImage* frame = cvQueryFrame(capture); // check return { fprintf( stderr,"ERROR: cvQueryFrame failedn"); break; } // At this point you already have the frame! There's no need to // repeat the thing 10x with cvGrabFrame and cvRetrieveFrame. // You are probably sabotaging yourself doing this multiple times. cvShowImage("mywindow",frame); // Do not release the frame! int key = cvWaitKey(10); if (key == 0x1b) break; } cvReleaseCapture(&capture); cvDestroyWindow("mywindow"); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |