问题
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!
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".
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.
I've written a little textscript:
#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;
}
This results in the following errors:
[ 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.
Thank for helping!
回答1:
OpenCV can only work for USB camera, not Raspberry Pi camera.
The hardware interface is different.
You can find some Picamera C++ repositories from the Raspberry Pi Q&A.
For example:
- 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;
}
来源:https://stackoverflow.com/questions/61454838/ubuntu-19-10-enabling-and-using-raspberry-pi-camera-module-v2-1