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

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!

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

Related

Select Timeout error in Ubuntu14.04 with camera Opencv

I am trying to use a webcam for my project. I use Opencv2.4.11 and the Logicool webcam C270.
I wrote a very simple code shown on below.
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat frame;
VideoCapture capture(0); // open the default camera
if (!capture.isOpened()) // check if we succeeded
return -1;
namedWindow("CVtest",1);
while (true) {
capture >> frame;
imshow("CVtest", frame);
waitKey(20);
}
return 0;
}
but this code is not work for me. I get this error messages.
jiehunt#ubuntu:~/work/test$ ./test.out
init done
opengl support available
select timeout
select timeout
I found this page Select Timeout error in Ubuntu - Opencv , and I followed the step
sudo rmmod uvcvideo
sudo modprobe uvcvideo nodrop=1 timeout=6000 quirks=0x80
It doesn't work for me.
When I turned on the trace of uvcvideo, I got this error message.
[13087.385542] uvcvideo: Marking buffer as bad (error bit set).
[13087.385545] uvcvideo: Frame complete (EOF found).
And now, I don't know what I should do.
My camera formats are shown bellow.
jiehunt#ubuntu:~/work/test$ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUV 4:2:2 (YUYV)
Index : 1
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : MJPEG

VideoCapture select timeout with OpenCV 3.0.0-rc1

I am using OpenCV 3.0.0-rc1 on Ubuntu 14.04 LTS Guest in VirtualBox with Windows 8 Host. I have an extremely simple program to read in frames from a webcam (Logitech C170) (from the OpenCV documentation). Unfortunately, it doesn't work (I have tried 3 different webcams). It throws an error "select timeout" every couple of seconds and reads a frame, but the frame is black. Any ideas?
The code is the following:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
// Main
int main(int argc, char **argv) {
/* webcam setup */
VideoCapture stream;
stream.open(0);
// check if video device has been initialized
if (!stream.isOpened()) {
fprintf(stderr, "Could not open Webcam device");
return -1;
}
int image_width = 640; // image resolution
int image_height = 480;
Mat colorImage,currentImage;
bool loop = true;
/* infinite loop for video stream */
while (loop) {
loop = stream.read(colorImage); // read webcam stream
cvtColor(colorImage, currentImage, CV_BGR2GRAY); // color to gray for current image
imshow("Matches", currentImage);
if(waitKey(30) >= 0) break;
// end stream while-loop
}
return 0;
}
I found the problem: When using a webcam, make sure to connect it to the Virtual Machine using Devices->Webcams and NOT Devices->USB. Even though the webcam is detected as video0 when attaching it via Devices->USB, for some reasons it does not work.

Raspberry Pi OpenCv C++ low frame rate

I am using opencv on my raspberry pi 2 B+ to do real time image processing. I am using raspberry pi's camera and have installed V4l2 driver prior to starting image processing.
The problem is that I am getting about 2-3 fps with a delay of about 3 seconds.I am doing a real-time application and am unable to achieve it due to these constraints. Also,when I try to set my frame rate and resolution i get the following errors:
VIDEOIO ERROR: V4L/V4L2 VIDIOC_CROPCAP
VIDEOIO ERROR: setting property #16 is not supported
VIDEOIO ERROR: setting propert #5 is not supported
What can be the possible reason for this problem?
Here is my code:
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
namedWindow("Camera",WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open(0);
if(!cap.isOpened())
{
cout<<"Camera is not accessible"<<endl;
}
int width,length;
double fps;
fps=60;
cap.set(CV_CAP_PROP_CONVERT_RGB,false);
cap.set(CV_CAP_PROP_FPS,fps);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,240);
cap.set(CV_CAP_PROP_FRAME_WIDTH,320);
Mat frame;
while(1)
{
cap>>frame;
fps = cap.get(CV_CAP_PROP_FPS);
width = cap.get(CAP_PROP_FRAME_WIDTH);
length = cap.get(CAP_PROP_FRAME_HEIGHT);
cout<<fps<<endl<<width<<endl<<length<<endl;
imshow("Camera",frame);
waitKey(33);
}
}

Doesn't turn on camera by using OpenCV & arch linux

My develop H/W environment is Raspberry Pi2 & VX-1000 camera
My develop S/W environment is Arch linux & c++ & opencv
I could stream on webpage by using mjpg-streamer
with this command mjpg_streamer -i "./input_uvc.so -d /dev/video0 -n -f 30 -r 320x240" -o "./output_http.so -n -w ./www"
This run is good. (turns on camera (means turns on led on camera) )
But when I execute the program made by opencv code, it doesn't turn on camera (not turn on led) and the function cvCaptureFromCAM() returns NULL.
My source code is
#include <iostream>
#include <time.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv;
using namespace std;
int main(int argc,char** argv)
{
char c;
IplImage* frame;
CvCapture* capture;
capture = cvCaptureFromCAM(-1);
if(capture == NULL)
cout << "Strange!!" << endl;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH , 320);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT , 240);
bool bLoop = true;
cout << "Start streaming" << endl;
while (bLoop){
cvGrabFrame(capture); // Get a frame from cam
frame = cvRetrieveFrame(capture, 0); // get a frame from capture
cvSaveImage("save.jpg", frame);
cvWaitKey(33); // wait key input for 33ms
}
cvReleaseCapture(&capture);
cvDestroyAllWindows();
return 0;
}
Let's change this line:
capture = cvCaptureFromCam(-1);
To:
capture = cvCaptureFromCam(0);
This will use the correct index that your video camera is in!
Try to pass 0 as the argument of cvCaptureFromCAM() or just simply listen to some connected camera by a loop:
CvCapture* capture = NULL;
for(int i = 0; i < 100 && capture == NULL; i++)
capture = cvCaptureFromCAM(i);
if(capture == NULL)
return;
Last but not least you should avoid the old C API, because it is deprecated and supported poorly, try to use cv::VideoCapture class instead.

Capture image with OpenCV without GUI(on console linux)

I am using console linux and I have a camera capture application. I need to capture an image without GUI(The camera should start and capture some images, save it to disk and close). The following code works well on my laptop but doesn't start on console. Any suggestions?
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
Mat frame;
namedWindow("feed",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("feed", frame);
imwrite("/home/zaif/output.png", frame);
if(waitKey(1) >= 0) break;
}
return 0;
}
After the release of OpenCV 2.4.6 there were bug fixes for video capture on Linux. Go straight to 2.4.6.2 and you should get the fixes. Specifically, this revision is probably the relevant fix for you, although there were a number of other revisions pertaining to video capture on android that might effect Linux compilation too.