【发布时间】:2015-08-10 15:18:59
【问题描述】:
硬件:
- 树莓派 2 B
- 操作系统:Raspbian(Debian Wheezy,2015 年 5 月 5 日发布)
- OpenCV 3.0.0(2015 年 6 月 4 日发布)
我正在使用 Raspberry Pi 的 CSI 端口上的摄像头。命令
raspistill -o test.jpg
工作正常:它会拍照并将其保存在我的 Raspberry 上。所以相机工作得很好。
6 月份,我展示了相机并可以应用一些其他功能,例如检测边缘或线条。前几天想更进一步,突然imshow出现问题。
例如,下面的代码在 6 月运行,但现在返回错误
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/pi/opencv-3.0.0/modules/highgui/src/window.cpp, line 271
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/opencv-3.0.0/modules/highgui/src/window.cpp:271: error: (-215) size.width>0 && size.height>0 in function imshow
这是在 6 月份运行的代码:
#include <cv.hpp>
#include <cxcore.hpp>
#include <stdlib.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
int c;
Mat image;
VideoCapture video;
video.open(0);
while(1)
{
video >> image;
imshow("test", image);
c=waitKey(10);
if (c==27)
break;
}
video.release();
return 0;
}
我尝试通过添加代码(如延迟、调整大小、检查视频是否打开、检查要显示的图像是否不为空)或删除一些代码(如 vid.open)来解决我的问题。但是,它仍然不起作用。我得到的总是“打开视频......视频没有打开”。这意味着测试 vid.isOpened 总是返回 false。我试图打开另一个视频(即使我的相机不是问题,因为它与 raspistill 一起使用,如下所述)但错误是相同的,isOpened 是错误的。
这是我修改后的代码:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat image;
VideoCapture vid(0); // open the default camera
//VideoCapture vid("/home/pi/video.mp4"); // open a video file
cout << "opening video... ";
waitKey(1000); // add delay if the camera did not have time to open correctly
//vid.set(CV_CAP_PROP_FRAME_WIDTH, 640); // resize
//vid.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
//vid.open(0); // do not open twice, already done in VideoCapture vid(0)
if(!vid.isOpened()) // check if video is successfully opened
{
cout << "video did not open ";
return -1;
}
cout << "video opened correctly ";
namedWindow( "test", CV_WINDOW_AUTOSIZE ); // prepare the window
waitKey(1000); // add delay if the camera did not have time to open correctly
cout << "before while loop ";
while(1)
{
cout << "inside while loop ";
vid >> image; // get a frame from camera
if(!image.empty()) // wait for the image to be taken
{
cout << "displaying image ";
imshow("test", image); // display it
} else {
cout << "image empty ";
}
if (waitKey(10)==27) // waiting for esc key to be pressed for 10ms
break;
}
vid.release();
return 0;
}
有人可以帮我解决这个问题吗?我真的不明白为什么我现在会遇到这个问题,因为它在 6 月份运行良好,而且我没有对我的 Raspberry 进行任何更改。 提前感谢您的帮助!
编辑:我认为我能做的最后一件事是重新安装所有内容(OS Raspbian 和 OpenCV),因为它在 6 月份运行良好,无需安装您在答案中提出的任何其他库。我真的不知道 6 月到现在之间发生了什么变化,因为没有人在 Raspberry 上碰过任何东西,我的代码不再工作了 :(
EDIT2:与 PiCapture 库一起使用的代码:
#include <cv.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include "/home/pi/PiCapture/src/PiCapture.cpp"
using namespace cv;
using namespace std;
int main()
{
PiCapture cap;
namedWindow("PiCapture");
cap.open(320, 240, true); // 320 width, 240 height, color true
while(1)
{
Mat img;
img = cap.grab(); // get a frame from camera
if(!image.empty()) // wait for the image to be taken
{
imshow("PiCapture", img); // display it
}
if (waitKey(10)==27) // waiting for esc key to be pressed for 10ms
break;
}
return 0;
}
【问题讨论】:
-
尝试使用
vid(CV_CAP_ANY)连接。这实际上可能只相当于vid(0)...我忘记了CV_CAP_ANY的值是什么。 -
感谢您的回答,但它也不起作用。我已经在 VideoCapture 类定义文件中读到 0 已经是默认相机。而且我只有一个摄像头连接到我的树莓派...
标签: opencv video-capture raspberry-pi2