5

I am trying to capture video from a USB camera using OpenCV.

#include <highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    VideoCapture cap (-1);
    if (!cap.isOpened())
         cout << "Cam initialize failed";
    else cout << "Cam initialized";

    return 0;
}

It is failing to initialize the camera. cap.isOpened() is returning zero.

The same program, with same version of OpenCV and the same USB camera, is correctly running in my friend's machine. I am running Fedora 16. The camera is properly working in another application (for example, Cheese).

I did some searching in Google and Stack Overflow. But no useful help. Any idea?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Barshan Das
  • 3,677
  • 4
  • 32
  • 46
  • 1
    Your camera works in another application? – vfcosta Oct 15 '12 at 15:50
  • Have you read this: http://stackoverflow.com/questions/4749498/cant-access-webcam-with-opencv – james Oct 15 '12 at 16:22
  • @vfcosta As I mentioned earlier, the camera is working fine with other applications. I used it in Cheese,Kamoso and vlc. – Barshan Das Oct 16 '12 at 06:46
  • @james from the link you have given, it seems its problem in highgui. But the solution or solution hint given there was for Windows. I am running linux fedora 16. I do not know where to find the appropriate preprocessor to be included. – Barshan Das Oct 16 '12 at 06:50
  • @brainbarshan, I'm not sure how to include Preprocesser Definitions in linux but you can simply add #define HAVE_VIDEOINPUT #define HAVE_DSHOW in an appropriate global header file (try modifying the existing opencv header file) and try recompiling the highgui lib. btw, are you using the latest OpenCV? – james Oct 16 '12 at 11:43

2 Answers2

1

Try running your program as root. You may not have permission, and opencv doesn't tell you if thats the reason cap.isOpened() failed.

dmagree
  • 265
  • 1
  • 2
  • 10
0

The manual here says that the VideoCapture::VideoCapture(int device) accepts

device: id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0.

I think you should change the -1 to 0 if you have 1 camera in your system.

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • VideoCapture cap(0) fails to find the default camera also. Passing -1, searches for all available cameras, hence its better than using 0. – Barshan Das Oct 16 '12 at 17:33