【问题标题】:Why does VideoCapture isOpened() fail with my working camera using OpenCV on a Raspberry Pi?为什么 VideoCapture isOpened() 在 Raspberry Pi 上使用 OpenCV 的工作相机失败?
【发布时间】: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


【解决方案1】:

sudo modprobe bcm2835-v4l2

为 cv 视频捕获方法加载 linux 驱动程序的预安装视频

【讨论】:

  • 这确实解决了我的问题。我使用的是安装了 opencv 的 Raspberry Pi 3,显然只有 v4l2 加载丢失导致 opencv 失败。
  • 太棒了!请将回复标记为您问题的答案:)
【解决方案2】:

不久前我遇到了同样的问题,并为 Raspberry Pi 摄像头模块组装了一个漂亮的小包装,这样您就可以轻松地将图像检索为 cv::Mat

有一个纯 c++ 版本:PiCapture 和一个 OpenFrameworks 插件版本:ofxCvPiCam

【讨论】:

  • 感谢您的回答。我试图从 git 克隆,但后来没有在 src 文件夹中找到文件。所以我创建了它并复制了每个相应文件中的文件内容。然后使用代码块,我尝试构建 main.cpp,但我有很多找不到的库。我为每个库提供了 /home/pi/userland/... 的完整路径,但有些文件在 userland 文件夹中不存在(我使用搜索工具进行了搜索)。所以我无法测试这段代码,太可惜了。
  • 嗨,Anais,请关注setup instructions。简而言之 1. 克隆 userland repo,2. 更新 CMakeLists.txt 文件以指向您在 Pi 上下载 userland repo 的位置,3. 继续典型的 cmake 设置(创建一个构建文件夹,输入它,运行 @987654329 @,然后是make
  • 好的,我试试。当我克隆 repo 时,它有另一个 README 文件,它说只执行“buildme”可执行文件。也许有区别......
  • CMakeLists 文件中没有 USERLAND_DIR,因此我无法将其替换为我的路径。我运行了 cmake,但最后命令 ./main 不起作用,因为 userland 文件夹中没有主文件。
  • 克隆用户态存储库后,您不需要在那里构建或编译任何东西。它包含 PiCapture 引用的相机接口的头文件。 USERLAND_DIR 位于line 9 in CMakeLists.txt。将 USERLAND_DIR 替换为您刚刚克隆的用户空间存储库的路径。之后,在终端窗口中,在 PiCapture repo 文件夹中创建一个构建文件夹并输入它:mkdir build &amp;&amp; cd build,然后运行 ​​cmake:cmake ..,然后是 make,它将为你编译 main,你可以运行它
【解决方案3】:

OpenCV 不将 raspiCam 视为相机。我更喜欢使用 system() 命令从 raspiCam 获取帧并将其保存在文件系统中的某个位置。之后,您可以将其作为图像加载到您的程序中。或者你可以试试这个BlogPost

希望对你有帮助。

【讨论】:

  • 感谢您的回答!我试过这个代码: system("raspistill -o testimage.jpg");命名窗口(“测试”,CV_WINDOW_AUTOSIZE); Mat img = imread("testimage.jpg", CV_LOAD_IMAGE_UNCHANGED); imshow("测试", img);但它没有显示带有图像的窗口。而且 raspistill 命令太长,我需要在自动驾驶汽车上使用摄像头,因此帧速率必须很高。不知道是不是正常,但是在拍照之前,我看到一段视频2-3秒后相机才真正拍照。
  • 实际上当我尝试一个非常简单的程序时: Mat img = imread("/home/pi/name.jpg", CV_LOAD_IMAGE_UNCHANGED); imshow("测试", img);我仍然在 imshow 中看不到任何内容。我只是打开了一个终端窗口,“进程返回 0”,但我看不到图像窗口...
  • 我想测试 Robidouille 包,但是在尝试构建 RaspiCam 库时出现错误(通过“make”):/usr/local/include/opencv2/core/base.hpp: 49:4: 错误: #error base.hpp header must be compiled as C++ /usr/local/include/opencv2/core/base.hpp:52:19: fatal error: climits: No such file or directory /// 编译终止。 Makefile:45: 目标“objs/RaspiCamCV.o”的配方失败。所以不幸的是,我也无法测试这个库,对于用户空间也是如此。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 2013-06-25
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多