I have a piece of c++ code:
#include <opencv/highgui.h>
#include <iostream>
int main()
{
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); //tried (0) too
if(capture == NULL)
std::cout<<"NULL"<<std::endl;
return 0;
}
and compile it using:
g++ main.cpp `pkg-config opencv --cflags` `pkg-config opencv --libs`
and the result of ./a.out is
NULL
I use OpenCV 2.4.2 and Ubuntu 12.04
But I am sure that my webcam is good because the following python code works fine.
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('frame', frame) # it shows a correct image from the webcam
cv2.waitKey(0)
Does anyone have idea why I cannot read my webcam in the C++ code?
Update:
I then upgrade OpenCV to 2.4.8 and compile the C++ code again. The output is still NULL be it shows some error message:
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT
NULL