Raspberry Pi OpenCv C++ low frame rate - c++

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

Related

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

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

Opencv 3.2 read mpeg files too slow

I am going through the book Learning OpenCV 3 and test out the video example 2.3. I could edit, compile and run it, but the problem is that it closed down immediately.
// DisplayPicture.cpp : Defines the entry point for the console application.
//
//#include "opencv2/opencv.hpp" // Include file for every supported OpenCV function
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <opencv2/videoio.hpp>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
namedWindow("video3", WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open( string(argv[1]));
int tell = 0;
Mat frame;
for (;;) {
cap >> frame;
//waitKey(30);
if (frame.empty())
{
break;
//end of film
}
imshow("video3", frame);
}
return 0;
}
I found that my computer processed the data too fast. It could not read the next frame fast enough. if (frame.empty()) became true the program reached the break statement and ended.
By adding a waitkey of 30 millisec before viewing the image frame, the video program works very well. At least I can view the video. Since this example is from the 'bible' it should work, but not with my computer.
I am running a MSI gt72 2PE computer with nvidia gtx880m. Not sure if that matters.
I assume that adding a waitKey(30) is not appropriate, so I am seeking suggestions as to what could be done differently.

opencv imshow show gray in Opencv 3.1

Opencv imshow showing gray image.
#include "stdafx.h"
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap;
Mat frame;
cap.open(0);
while (true)
{
cap >> frame;
imshow("test", frame);
waitKey(30);
}
return 0;
}
I recently upgraded to windows insider preview 14316 and it stopped working after that i think.
I checked if the camera work in skype, and it did. I also tried the code on another computer and it worked.
Any ideas?

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.

Webcam Streaming is mirrored using OpenCV 3.0 + Visual Studio 2013

Every time i compile this code the webcam streaming is mirrored like I lift up my right hand and it appears like if it was my left hand on the screen instead and after a couple of re-compiling an error message appeared and the code never worked again.
The error:
Unhandled exception at 0x00007FFB3C6DA1C8 in Camera.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000D18AD5F610.
And no other option left except to break the process.
The code:
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
imshow("window", image);
waitKey(33);
}
}
I don't know if something wrong with my code I've just started learning opencv !
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
flip(image,image,1)
imshow("window", image);
waitKey(33);
}
}
just Flip the image horizontally that will do Find More Here
There is nothing wrong when your image is mirrored on the vertical (/x) axis (I guess in your example you are using a built-in (laptop) webcam).
A (very) simple code for capturing and showing your image could be the following:
// imgcap in opencv3
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
int main() {
cv::VideoCapture cap(0); // camera 0
cv::Mat img;
while(true) {
cap >> img;
cv::flip(img,img,1);
cv::imshow("live view", img);
cv::waitKey(1);
}
return 0;
}
When using OpenCV 3 you should include headers like:
#include <opencv2/highgui.hpp>