gpt4 book ai didi

opencv - 为什么我在 Raspberry Pi 上使用 OpenCV 的工作相机会出现 VideoCapture isOpened() 失败?

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:46 25 4
gpt4 key购买 nike

硬件:

  • 树莓派 2B
  • 操作系统: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;
}

最佳答案

sudo modprobe bcm2835-v4l2

加载cv视频采集方式的linux驱动预装video

关于opencv - 为什么我在 Raspberry Pi 上使用 OpenCV 的工作相机会出现 VideoCapture isOpened() 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com