VideoCapture select timeout with OpenCV 3.0.0-rc1 - c++

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.

Related

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 Stitching in real-time with Overlapping Stationary Cameras extremely slow

I'm trying to use the OpenCV Stitcher class to stitch frames from two cameras. I would just like to do this in the most simple way to start with and then get into the details of the Stitcher class.
The code is really simple and not so many line of code.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include<iostream>
#include <fstream>
#include<conio.h> // may have to modify this line if not using Windows
using namespace std;
using namespace cv;
//using namespace cv::detail;
bool try_use_gpu = true;
Stitcher::Mode mode = Stitcher::PANORAMA; //Stitcher::SCANS;
vector<Mat> imgs;
int main()
{
//initialize and allocate memory to load the video stream from camera
cv::VideoCapture camera0(2);
cv::VideoCapture camera1(3);
if (!camera0.isOpened()) return 1;
if (!camera1.isOpened()) return 1;
//Mat output_frame;
cv::Mat3b output_frame;
cv::Stitcher stitcher = cv::Stitcher::createDefault(true);
//Ptr<Stitcher> stitcher = Stitcher::create(mode, try_use_gpu);
Mat camera0_frame_resized;
Mat camera1_frame_resized;
while (true) {
//grab and retrieve each frames of the video sequentially
cv::Mat3b camera0_frame;
cv::Mat3b camera1_frame;
camera0 >> camera0_frame;
camera1 >> camera1_frame;
resize(camera0_frame, camera0_frame_resized, Size(640, 480));
resize(camera1_frame, camera1_frame_resized, Size(640, 480));
imgs.push_back(camera0_frame_resized);
imgs.push_back(camera1_frame_resized);
Stitcher::Status status = stitcher.stitch(imgs, output_frame);
output_frame.empty();
if (status != Stitcher::OK) {
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
cv::imshow("Camera0", camera0_frame);
cv::imshow("Camera1", camera1_frame);
cv::imshow("Stitch", output_frame);
//wait for 40 milliseconds
int c = cvWaitKey(5);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if (27 == char(c)) break;
}
return 0;
}
I am using OpenCV 3.2 and Visual Studio Community Edition 2017 running on windows 10.
The issue is that it is extremely slow, It seems to stitch the first frame and then it kind of gets stuck and nothing else happens, after seconds/minute maybe next frames appear.
I am running on a extremely fast CPU and top of the line GPU.
I was not expecting fast stitching but this is just way to slow and gets me thinking that I am doing something wrong.
Any ideas on why it stitches only the first frame and then gets stuck?

RTSP: error while decoding MB

I have problem with RTSP stream from my BOSH cammera. While Im getting stream to my program with this code:
VideoCapture cap("rtsp://name:password#IPaddress:554");
i get some stream, but after few images i get this error:
enter image description here
and then, my stream looks like this:
enter image description here
I saw that there are more solutions for my problem, like using VLC libraries to resolve my problem, or GStreamer. But this is no solution for my problem, because I need to use OPenCV libraries, save every image to MATRIX, and Im then using my special image processing..
Does anybody have solution ?
Is it possible to create some buffer ? If answer yes, can anybody write HOW ?
Here is my code:
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat frame;
namedWindow("video", 1);
VideoCapture cap("rtsp://name:password#IPaddress:554");
if(!cap.isOpened())
{
cout<<"Camera not found"<<endl;
return -1;
}
while(1)
{
cap >> frame;
if(frame.empty()) break;
resize(frame, frame, Size(1200, 800), 0, 0, INTER_CUBIC);
imshow("video", frame);
if(waitKey(1000) >= 0) break;
}
return 0;
}

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

VS 2010 turning on camera but only shows grey window

I'm new at this but have been doing my share of reading and trying different setups to help narrow down the problem! Any help tp get me past this road block would be much appreciated.
Currently I'm running: Win 7 Ultimate, Visual C++ 2010 Express, OpenCV 2.2.0, and a Microsoft - LifeCam Studio Webcam - Silver 1080p HD.
I'm getting no Build errors and when I run the program my camera comes on (blue light indicating it being on) and the screen pops up that i thought should show my camera feed but instead its just a grey box with nothing inside. The below code I thought would help narrow down the problem but I'm at a loss.
int main()
{
CvCapture *webcam = NULL;
webcam = cvCreateCameraCapture(-1);
if(webcam!=NULL)
{
IplImage *frame = cvQueryFrame(webcam);
cvShowImage("WEBCAM_TEST",frame);
cvWaitKey(0);
return 0;
}
else
{
std::cout<<"CAMERA NOT DETECTED"<<std::endl;
return 0;
}
}
your code is some times showing a black image sometimes showing a correct image on my system(Windows 7 64 VS2010 OpenCV 2.4.3)...how ever when I put it in a loop for non stop streaming the image is ok...so just modify your code slightly and try...
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
CvCapture *webcam = NULL;
webcam = cvCreateCameraCapture(-1);
if(webcam!=NULL)
{
while(true)
{
IplImage *frame = cvQueryFrame(webcam);
cvShowImage("WEBCAM_TEST",frame);
cvWaitKey(20);
}
}
else
{
std::cout<<"CAMERA NOT DETECTED"<<std::endl;
return 0;
}
return 0;
}
In OpenCV if you get frame just after creating camera capture usually it's grey. All you have to do is just get next frame or wait before getting first frame. This code:
int _tmain(int argc, _TCHAR* argv[])
{
VideoCapture cap(0);
if(!cap.isOpened())
return -1;
Mat frame;
namedWindow("01",1);
//cap >> frame; //option 1
//waitKey(5000); //option 2
cap >> frame;
imshow("01", frame);
int key = waitKey(30);
return 0;
}
will show grey frame, but if you uncomment option 1 or option 2 - it will work fine.