我已经在树莓派上安装了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)不同。
您可以从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;
}