OpenCV 2.4.3 camera capture not working on Ubuntu 12.04

3.3k Views Asked by At

I am trying to execute the following program using openCV 2.4.3 on Ubuntu 12.04 LTS. But I get "camera not initialized as the output" Can anybody help me.

here is the code:

include <iostream>
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main()
{
  VideoCapture cap(1);

  if (!cap.isOpened())
    {
      cout <<"Failed to initialize camera\n";
      return 1;
    }

  namedWindow("CameraCapture");

  Mat frame;
  while (1)
    {
      cap>> frame;
      imshow("cameraCapture",frame);
      if (waitKey(30)>0)break;
    }
  destroyAllWindows();

  return 0;

 }

Please help me!

Thanks, Kushal

2

There are 2 best solutions below

1
On

try the following...

#include "iostream"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
    CvCapture *webcam = cvCaptureFromCAM(-1);
    IplImage *img = NULL;

    while(true)
    {
        img = cvQueryFrame(webcam);
        cvShowImage("TEST",img);
        cvWaitKey(20);
    }

    return 0;
}
2
On

did u check the default capture device? by default it is 0

VideoCapture cap(0);