OpenCV, webcam capture result in black screen - c++

I have tried several proposals about this problem.
Unfortunately i was not able to get it working.
I have tried opencv 2.4.13.3 and even 3.3.0 but it does not make any difference.
Here is my code:
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
cap.retrieve(frame);
int i = 0; //index, we use it for testing
while ((i++ < 100) && !cap.read(frame)) //skip unread frames
{
cout << "frame " << i << " skipped" << endl;
}
if (i >= 100) //check webcam_0 failure
{
cout << "cannot read frames from webcam_0, check drivers" << endl;
waitKey(0);
return -1;
}
else
{
cout << "cam is ready" << endl;
}
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
It always skipps frames (as it should until it was able to get a correct frame). But after 100 skipped frames, it still get no frames.
The Webcam works fine with the camera app from Win10. So the camera itself is not the problem.
System: Win10 x64.
Tried with x86 and x64 version of opencv
I hope you can help me.
Thanks.

Related

How to switch cameras to get a 360 degrees view on an object?

I'm trying to find a way to switch between several cameras in different positions around an object, to get a 360 degrees view in real time using C++. I'm working with opencv and I succeeded to open all the cameras, but I couldn't find the documentation or a way to have that 360 degree fluent switch.
this is the code I made so far
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
int a = 0;
VideoCapture cap(0); // open the video camera no. 0
VideoCapture cap1(1);
//if (!cap.isOpened()) // if not success, exit program
//{
// cout << "Cannot open the video cam" << endl;
// return -1;
//}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess;
if(a==0)
bSuccess = cap.read(frame); // read a new frame from video
else if (a == 1)
bSuccess = cap1.read(frame);
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(1) == 'a')
{
if (!a) a = 1;
else a = 0;
}
/*else if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
I have 6 other cameras, and an object in the middle
is it possible to do it in C++? or do I need to use something else. Keeping in mind that it has to be done in real time.

ERROR: Background Subtraction Coding Doesn't Functioning using openCV and VS2013

I had ran a coding for background subtraction from the video for offline. I used openCV 2.4.9 and Visual Studio 2013 to run the coding. The video can display and play but the problem is the coding for background subtraction does not functioning. Can anybody help me what is the wrong for my coding? Someone had tell me the error is
if (strcmp(argv[1], "-vid") == 0)
{
//input data coming from a video
processVideo(argv[2]);
}
so, what i need to do? Help me please...
// BackgroundSubtraction_Success.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\flann\flann.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\photo\photo.hpp>
#include <opencv2\video\video.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\ml\ml.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\contrib\contrib.hpp>
//#include "opencv\cv.h"
//#include <opencv2\core\core_c.h>
//#include <opencv2\highgui\highgui_c.h>
//#include <opencv2\imgproc\imgproc_c.h>
using namespace cv;
using namespace std;
//global variables
Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Ptr <BackgroundSubtractorMOG> pMOG; //MOG Background Subtractor
int keyboard;
//function declarations
void help();
void processVideo(char*Background);
void help()
{
cout
<< "----------------------------------------" << endl
<< endl
<< "This program begins with Motion Detection" << endl
<< "using Background Subtraction" << endl
<< endl
<< "------------------------------------------" << endl;
}
int main(int argc, char*argv[])
{
VideoCapture cap("C:/Users/user/Documents/Visual Studio 2013/Projects/cobaan/NewOpenCV_Success/sample1.avi"); // open the video file for reading
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
//print help information
help();
//check for the input parameter correctness
if (argc != 3)
{
cerr << "incorrect input list" << endl;
cerr << "exiting..." << endl;
return EXIT_FAILURE;
}
//create GUI windows
namedWindow("Frame");
namedWindow("FG Mask MOG");
//call the constructor
BackgroundSubtractorMOG bgmog;
bgmog(frame, fgMaskMOG);
//create background subtractor objects
//pMOG = createBackgroundSubtractorMOG(); //MOG approach
if (strcmp(argv[1], "-vid") == 0)
{
//input data coming from a video
processVideo(argv[2]);
}
else
{
//error in reading input parameter
cerr << "Please check the input parameters." << endl;
cerr << "Exiting..." << endl;
return EXIT_FAILURE;
}
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}
//call video
void processVideo(char* MyVideo)
{
//create the capture object
VideoCapture capture(MyVideo);
if (!capture.isOpened())
{
//error in opening the video input
cerr << "Unable to open video file: " <<MyVideo << endl;
exit(EXIT_FAILURE);
}
//read input data ESC or 'q' for quitting
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
//read the current frame
if (!capture.read(frame))
{
cerr << "Unable to read next frame" << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
//update the background model
//pMOG->apply(frame, fgMaskMOG);
//get the frame number and write it on the current frame
stringstream ss;
rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
ss << capture.get(CV_CAP_PROP_POS_FRAMES);
string frameNumberString = ss.str();
putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
//show the current frame and fg masks
imshow("Frame", frame);
imshow("FG Mask MOG", fgMaskMOG);
//get input from the keyboard
keyboard = waitKey(30);
}
//delete capture object
capture.release();
}

Opencv; How to free IplImage*?

I am trying to get a hang of OpenCV. At the moment I am trying to subtract two frames from each other and display the result. I have found example code which will do that just fine.
My problem is that I am getting a memory allocation error.
Well, nothing to special about that, because I am feeding the program with HD video.
So my question is how do I release the allocated memory of an IplImage*?
The Mat type has something like Mat.release().
IplImage does not have that, nor does free(IplImage) work.
Here is my code:
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\highgui\highgui_c.h>
using namespace cv;
int main()
{
std::string videofilename;
std::cout << "Please specify the video name (make sure it is in the same folder\nas the application!):" << std::endl;
std::cin >> videofilename;
std::cout << "The name you provided: " << videofilename << std::endl;
VideoCapture video(videofilename);
if(!video.isOpened())
{
std::cout << "Could not open video file" << std::endl;
return -1;
}
std::cout << "Number of frames: " << video.get(CV_CAP_PROP_FRAME_COUNT) << std::endl;
std::cout << "Duration: "<< static_cast<int>(video.get(CV_CAP_PROP_FRAME_COUNT))/(30*60) << "min " << static_cast<int>((video.get(CV_CAP_PROP_FRAME_COUNT)))%(30*60)/30 << "sek" << std::endl;
// Close it before opening for playing
video.release();
CvCapture* capture = cvCaptureFromAVI(videofilename.c_str());
IplImage* frame = cvQueryFrame(capture);
IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
if ( !capture )
{
std::cout << "Could not open video file" << std::endl;
return -1;
}
cvNamedWindow("dest", CV_WINDOW_AUTOSIZE);
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)
{
printf("Capture Finished\n");
break;
}
currframe = cvCloneImage(frame); // copy frame to current
frame = cvQueryFrame(capture); // grab frame
if(!frame)
{
printf("Capture Finished\n");
break;
}
cvSub(frame, currframe, destframe); // subtraction between the last frame to cur
cvShowImage("dest", destframe);
//cvReleaseImage(currframe); // doesn't work
//free(currframe); // doesnt work either
//delete(currframe); //again, no luck
cvWaitKey(30);
}
cvDestroyWindow("dest");
cvReleaseCapture(&capture);
return 0;
}
You can free IplImage using cvReleaseImage. It takes address of a pointer to an IplImage, i.e. IplImage** as argument, so you have to do this:
cvReleaseImage(&currframe);
instead of cvReleaseImage(currframe);.
But keep in mind, the image returned by cvQueryFrame, (frame in your case) is a special case and it should not be released or modified. Also, you don't have to preallocate currFrame if you are going to initialize it using cvCloneImage eventually.
The final code would look like this:
int main()
{
std::string videofilename;
std::cout << "Please specify the video name (make sure it is in the same folder\nas the application!):" << std::endl;
std::cin >> videofilename;
std::cout << "The name you provided: " << videofilename << std::endl;
VideoCapture video(videofilename);
if(!video.isOpened())
{
std::cout << "Could not open video file" << std::endl;
return -1;
}
std::cout << "Number of frames: " << video.get(CV_CAP_PROP_FRAME_COUNT) << std::endl;
std::cout << "Duration: "<< static_cast<int>(video.get(CV_CAP_PROP_FRAME_COUNT))/(30*60) << "min " << static_cast<int>((video.get(CV_CAP_PROP_FRAME_COUNT)))%(30*60)/30 << "sek" << std::endl;
// Close it before opening for playing
video.release();
CvCapture* capture = cvCaptureFromAVI(videofilename.c_str());
IplImage* frame = cvQueryFrame(capture);
IplImage* currframe;
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
if ( !capture )
{
std::cout << "Could not open video file" << std::endl;
return -1;
}
cvNamedWindow("dest", CV_WINDOW_AUTOSIZE);
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)
{
printf("Capture Finished\n");
break;
}
currframe = cvCloneImage(frame); // copy frame to current
frame = cvQueryFrame(capture); // grab frame
if(!frame)
{
printf("Capture Finished\n");
break;
}
cvSub(frame, currframe, destframe); // subtraction between the last frame to cur
cvShowImage("dest", destframe);
cvWaitKey(30);
cvReleaseImage(&currframe);
}
cvDestroyWindow("dest");
cvReleaseCapture(&capture);
return 0;
}
Use cvReleaseImage(). For example, if you want to release IplImage* frame, use cvReleaseImage(&frame).
For Mat, you don't need to release it explicitly. It will release automatically when out of its code block.
Edit: take a look at here on more details about cvReleaseImage(), which addresses on some wrong releasing situations.

OpenCV VideoCapture::get gives wrong outputs

I am using OpenCV 2.3.1 version. When I try to get various Videocapture properties, but opencv gives wrong outputs except for Height and Width :
My code:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double msec = cap.get(CV_CAP_PROP_POS_MSEC ); //Current position of the video file in milliseconds or video capture timestamp
cout << "current position : " << msec << endl;
double fra = cap.get(CV_CAP_PROP_POS_FRAMES); //0-based index of the frame to be decoded/captured next
cout << "fra: " << fra << endl;
double width = cap.get(CV_CAP_PROP_FRAME_WIDTH); //width of the frames in the video stream.
cout << "width: " << width << endl;
double height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //Height of the frames in the video stream.
cout << "height: " << height << endl;
double fps = cap.get(CV_CAP_PROP_FPS); //frame rate
cout << "fps: " << fps << endl;
double fourcc = cap.get(CV_CAP_PROP_FOURCC); //4-character code of codec
cout << "fourcc: " << fourcc << endl;
double frame_count = cap.get(CV_CAP_PROP_FRAME_COUNT ); //Frame count in the video
cout << "frame count: " << frame_count << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while(1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Output is similiar to any property except height and width:
HIGHGUI ERROR: V4L2: Unable to get property <unknown property string>(0) - Invalid argument
current position : -1
HIGHGUI ERROR: V4L2: Unable to get property <unknown property string>(1) - Invalid argument
fra: -1
width: 640
height: 480
HIGHGUI ERROR: V4L2: Unable to get property <unknown property string>(5) - Invalid argument
fps: -1
HIGHGUI ERROR: V4L2: Unable to get property <unknown property string>(6) - Invalid argument
fourcc: -1
HIGHGUI ERROR: V4L2: Unable to get property <unknown property string>(7) - Invalid argument
frame count: -1

Functions in OpenCV

Hi I have written the following code in OpenCV. Basically it reads a video from file. Now, I want to create a function to resize the video but I am unsure how to call the "VideoCapture" class from the main function. I have written a sample function to see if it'll read anything but it compiles fine showing stuff from the main function but nothing from the newly created function. Any help? P.S I'm not very experienced, bear with me LOL.
using namespace cv;
using namespace std;
void resize_video(VideoCapture capture);
int main(int argc, char** argv)
{
VideoCapture capture; //the C++ API class to capture the video from file
if(argc == 2)
capture.open(argv[1]);
else
capture.open(0);
if(!capture.isOpened())
{
cout << "Cannot open video file " << endl;
return -1;
}
Mat frame;
namedWindow("display", CV_WINDOW_AUTOSIZE);
cout << "Get the video dimensions " << endl;
int fps = capture.get((int)CV_CAP_PROP_FPS);
int height = capture.get((int)CV_CAP_PROP_FRAME_HEIGHT);
int width = capture.get((int)CV_CAP_PROP_FRAME_WIDTH);
int noF = capture.get((int)CV_CAP_PROP_FRAME_COUNT);
CvSize size = cvSize(width , height);
cout << "Dimensions: " << width << height << endl;
cout << "Number of frames: " << noF << endl;
cout << "Frames per second: " << fps << endl;
while(true)
{
capture >> frame;
if(frame.empty())
break;
imshow("display", frame);
if (waitKey(30)== 'i')
break;
}
//resize_video();
}
void resize_video(VideoCapture capture)
{
cout << "Begin resizing video " << endl;
//return 0;
}
you want to call your function INSIDE the while loop, not after it (too late, program over)
so, it might look like this:
void resize_video( Mat & image )
{
//
// do your processing
//
cout << "Begin resizing video " << endl;
}
and call it like:
while(true)
{
capture >> frame;
if(frame.empty())
break;
resize_video(frame);
imshow("display", frame);
if (waitKey(30)== 'i')
break;
}