Ask Your Question
0

Problem with Camera Capturing

asked 2014-04-19 04:58:11 -0600

wtswkz gravatar image

updated 2014-04-20 10:48:54 -0600

Moster gravatar image

I've got two ways to capture a camera. 1.using the functions in the opencv

 #include < opencv\cv.h
 #include < opencv\highgui.h 
 #include < iostream 
 using namespace std;
 using namespace cv;
 int main()
 {
     char c;
     CvCapture* capture = cvCreateCameraCapture(0);   //0 is the external camera ID,1 is the internal camera ID of my laptop.
     IplImage* src;
     for (;;)
    {
        src = cvQueryFrame(capture);         
        cvShowImage("Input", src);
        c = waitKey(10);
        if (c == 27) break;
    } 
   cvReleaseCapture(&capture);
   return 0;
 }
2.using the functions in the ***opencv2***
 #include < opencv2\core\core.hpp
 #include < opencv2\highgui\highgui.hpp 
 #include < iostream 
 using namespace cv;
 using namespace std;
 int main()
 {
     char c;
     VideoCapture inputVideo(0);    //0为外部摄像头的ID,1为笔记本内置摄像头的ID
     Mat src;
     for(;;)
     {
        inputVideo >> src;
        imshow("input",src);
        c = waitKey(10);
        if (c == 27) break;
     }
   return 0;
 }

By using the method 1, the program can read both the external and the internal camera's data;but the program can only read the internal camera's data with method 2. I don't know why method 1 and method 2 so different.
What should I do if I want to get the data of the external camera with method 2?
// my operation system is Window8.1, the version of my OPENCV is 2.4.8, the version of my Visual Studio is 2013. I need your help.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2014-04-23 00:22:50 -0600

wtswkz gravatar image

updated 2014-04-23 00:24:27 -0600

I have solved this problem. It occurred to me that by using method 2 the first frame got from the external camera might be blank while the others are normal, so I add something to the code:
#include < opencv2\core\core.hpp >
#include < opencv2\highgui\highgui.hpp >
#include < iostream >
using namespace cv;
using namespace std;
int main()
{
char c;
int i = 0;
VideoCapture inputVideo(0); //0为外部摄像头的ID,1为笔记本内置摄像头的ID
Mat src;
for(;;)
{
inputVideo >> src;
if(src.empty() & i > 1) return 1;
if(i > 1) imshow("input",src);
i ++; c = waitKey(10);
if (c == 27) break;
}
return 0;
}

edit flag offensive delete link more

Comments

3

some camera drivers seem to need a warmup time, and deliver (some) empty frames when starting.

there are some differences between the old c-api and the c++ one,

  • cvQueryFrame will return a NULL pointer, and cvShowImage won't complain(just show a gray rect)

  • VideoCapture::read will return an empty Mat, and imshow will complain about that

so, basically, your first frame was empty with the c-api, too, you just did not notice it.

berak gravatar imageberak ( 2014-04-23 00:41:19 -0600 )edit

Question Tools

Stats

Asked: 2014-04-19 04:58:11 -0600

Seen: 868 times

Last updated: Apr 23 '14