本文介绍了Ubuntu 19.10:启用和使用Raspberry Pi摄像头模块v2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在树莓派上安装了Ubuntu 19.10.我知道raspbian是更好的选择,但是出于某些其他原因,我不得不使用Ubuntu.我还安装了opencv4并通过加载和显示图像对其进行了测试.效果很好!

I have installed Ubuntu 19.10 on my raspberry pi. I know raspbian would be the better choice, but I have to use Ubuntu for some other reasons. I also have installed opencv4 and tested it with loading and showing an image. Works fine!

然后我想用sudo raspi-config配置我的raspi相机,但是没有找到命令,所以我通过以下方式尝试了它:sudo apt-get install raspi-config.这将导致无法找到软件包raspi-config".

I then wanted to configure my raspi camera with sudo raspi-config, but not command was found, so I tried it via: sudo apt-get install raspi-config. This results in "Unable to locate package raspi-config".

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

I read through the internet. Next I tried to include start_x=1 inside my /boot/firmware/config.txt. After a reboot I can see now a video0 device under /dev. So far so good.

我写了一点文字:

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

我认为问题可能仍然在于相机的正确安装,因为在我看来,此错误是由于镜框为空而引起的.

I think the problem might be still installing the camera correctly because in my opinion this error occurs because of an empty frame.

感谢您的帮助!

推荐答案

OpenCV仅适用于USB摄像头,不适用于Raspberry Pi摄像头.

OpenCV can only work for USB camera, not Raspberry Pi camera.

硬件接口不同.

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

You can find some Picamera C++ repositories from the Raspberry Pi Q&A.

例如:

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

这篇关于Ubuntu 19.10:启用和使用Raspberry Pi摄像头模块v2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:37