【问题标题】:Ubuntu 19.10: Enabling and using Raspberry Pi Camera Module v2.1Ubuntu 19.10:启用和使用 Raspberry Pi 摄像头模块 v2.1
【发布时间】:2020-08-10 18:28:02
【问题描述】:

我已经在我的树莓派上安装了 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 中。重新启动后,我现在可以在/dev 下看到一个video0 设备。到目前为止一切顺利。

我写了一个小文本:

#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)

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

感谢您的帮助!

【问题讨论】:

  • 那是USB摄像头吗?您正在尝试读取为 USB 摄像头。
  • 不,那是摄像头模块

标签: c++ opencv ubuntu raspberry-pi camera


【解决方案1】:

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

硬件接口不同。

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

例如:

#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;
}

【讨论】:

  • 我很确定摄像头模块也应该与 opencv 一起使用
  • 我尝试构建它,但我的系统上没有 /opt/vc。因此也没有 libmmal_core 文件。我能做些什么?也许我应该提到我已经安装了 64 位的 Ubuntu
  • @mAI 也许你可以试试 Ubuntu mate。
  • 据我所知 RPI4 不支持 Ubuntu Mate?
猜你喜欢
  • 2022-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 2018-12-11
相关资源
最近更新 更多