opencv 使用C++访问raspberry Pi摄像头

zfciruhq  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(183)

我尝试在C++中运行openCV并捕获摄像头输入。
程序如下所示:

#include <iostream>
#include <sstream>
#include <new>
#include <string>
#include <sstream>

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#define INPUT_WIDTH 3264
#define INPUT_HEIGHT 2464

#define DISPLAY_WIDTH 640
#define DISPLAY_HEIGHT 480

#define CAMERA_FRAMERATE 21/1
#define FLIP 2

void DisplayVersion()
{
    std::cout << "OpenCV version: " << cv::getVersionMajor() << "." << cv::getVersionMinor() << "." << cv::getVersionRevision() << std::endl;
}

int main(int argc, const char** argv)
{
    DisplayVersion();

    std::stringstream ss;

    ss << "nvarguscamerasrc !  video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method=2 ! video/x-raw, width=480, height=680, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink";

    //ss << "nvarguscamerasrc !  video/x-raw(memory:NVMM), width=" << INPUT_WIDTH <<
    //", height=" << INPUT_HEIGHT <<
    //", format=NV12, framerate=" << CAMERA_FRAMERATE <<
    //" ! nvvidconv flip-method=" << FLIP <<
    //" ! video/x-raw, width=" << DISPLAY_WIDTH <<
    //", height=" << DISPLAY_HEIGHT <<
    //", format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink";

    cv::VideoCapture video;

    video.open(ss.str());

    if (!video.isOpened())
    {
        std::cout << "Unable to get video from the camera!" << std::endl;

        return -1;
    }

    std::cout << "Got here!" << std::endl;

    cv::Mat frame;

    while (video.read(frame))
    {
        cv::imshow("Video feed", frame);

        if (cv::waitKey(25) >= 0)
        {
            break;
        }
   }

    std::cout << "Finished!" << std::endl;

    return 0;
}

运行此代码时,我得到以下输出:

OpenCV version: 4.6.0
nvbuf_utils: Could not get EGL display connection
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:751 Failed to create CaptureSession
[ WARN:0@0.269] global /tmp/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Got here!
Finished!

如果我对www.example.com()运行另一个注解过的命令video.open,我会得到以下输出:

OpenCV version: 4.6.0
nvbuf_utils: Could not get EGL display connection
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:751 Failed to create CaptureSession

我目前正在jetson nano上以无头模式运行这个程序。
我还知道OpenCV和xlaunch可以工作,因为我可以从笔记本电脑使用mjpeg流,并通过使用video.open(http://laptop-ip:laptop-port/);成功地将笔记本电脑相机输出流到我的jetson nano,而且工作正常(OpenCV能够使用xlaunch显示实时视频源,效果很好)。
我认为此命令是在告诉我摄像头已成功安装:

$ v4l2-ctl -d /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'RG10'
        Name        : 10-bit Bayer RGRG/GBGB
                Size: Discrete 3264x2464
                        Interval: Discrete 0.048s (21.000 fps)
                Size: Discrete 3264x1848
                        Interval: Discrete 0.036s (28.000 fps)
                Size: Discrete 1920x1080
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 1640x1232
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 1280x720
                        Interval: Discrete 0.017s (60.000 fps)

任何帮助都将不胜感激

cczfrluj

cczfrluj1#

警告似乎是说,你不能使用egl,即OpenGL时,在无头模式(因为没有屏幕)
如果你在无头模式下运行。不尝试打开一个窗口做显示会不会更有意义吗?

cv::imshow("Video feed", frame);

        if (cv::waitKey(25) >= 0)
        {
            break;
        }

删除此代码,改为使用cv::imwrite写入文件,或对数据执行任何操作。
或者如果你运行ssh,用-X选项来运行ssh,在你的客户端计算机上显示窗口。可能会很慢,但是如果你真的想使用cv::imshow,它可能是一个选项。

p8ekf7hl

p8ekf7hl2#

我通过重新启动修复了它。我已经重新启动了,但是现在每当我运行程序时都会出现一些错误。我重新编译了dlib库,但是我认为当你更新gstreamer库时,你需要重新启动你的机器才能成功地使用它。

相关问题