gpt4 book ai didi

c++ - Ubuntu 19.10 : Enabling and using Raspberry Pi Camera Module v2. 1

转载 作者:行者123 更新时间:2023-12-02 10:33:15 26 4
gpt4 key购买 nike

我在树莓派上安装了 Ubuntu 19.10。我知道 raspbian 会是更好的选择,但出于其他一些原因,我必须使用 Ubuntu。我还安装了 opencv4 并通过加载和显示图像对其进行了测试。工作正常!

然后我想用 sudo raspi-config 配置我的 raspi 相机,但没有找到命令,所以我通过以下方式尝试:sudo apt-get install raspi-config .这会导致“无法找到包 raspi-config”。

我通过互联网阅读。接下来我尝试包含 start_x=1在我的/boot/firmware/config.txt .重新启动后,我现在可以看到 video0 /dev 下的设备.到目前为止,一切都很好。

我写了一个小文本:

#include <opencv2/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/videoio.hpp>
using namespace cv;
int main(int argc, char** argv){

VideoCapture cap;
cap.open(0);
Mat frame;
for(;;){
cap.read(frame);
if (frame.empty()){
std::cerr << "Error";}
imshow("Live", frame);
}
return 0;
}

这会导致以下错误:
[ WARN:0] global /opt/opencv/modules/videoio/src/cap_gstreamer.cpp (1758) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0] global /opt/opencv/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /opt/opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Errorterminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.3.0-dev) /opt/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Aborted (core dumped)

我认为问题可能仍然是正确安装相机,因为在我看来这个错误是由于空帧而发生的。

感谢您的帮助!

最佳答案

OpenCV 只能用于 USB 摄像头,不能用于 Raspberry Pi 摄像头。

硬件接口(interface)不同。

enter image description here
您可以从 Raspberry Pi Q&A 找到一些 Picamera C++ 存储库。 .

例如:

  • https://github.com/cedricve/raspicam

  • #include <ctime>
    #include <fstream>
    #include <iostream>
    #include <raspicam/raspicam.h>
    using namespace std;

    int main ( int argc,char **argv ) {
    raspicam::RaspiCam Camera; //Camera object
    //Open camera
    cout<<"Opening Camera..."<<endl;
    if ( !Camera.open()) {cerr<<"Error opening camera"<<endl;return -1;}
    //wait a while until camera stabilizes
    cout<<"Sleeping for 3 secs"<<endl;
    sleep(3);
    //capture
    Camera.grab();
    //allocate memory
    unsigned char *data=new unsigned char[ Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
    //extract the image in rgb format
    Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
    //save
    std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
    outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
    outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
    cout<<"Image saved at raspicam_image.ppm"<<endl;
    //free resrources
    delete data;
    return 0;
    }

    关于c++ - Ubuntu 19.10 : Enabling and using Raspberry Pi Camera Module v2. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61454838/

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