Reputation: 482
I have trouble with accessing webcam using opencv 2.4.3.
My System:
Hp Probook 4530s - HP Fixed HD Webcam
Ubuntu 12.10
OpenCV 2.4.3
İf I want to capture my built-in camera i get ERROR: capture is NULL
I'm using http://opencv.willowgarage.com/wiki/CameraCapture sample code.
Sample code is:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
I also tried with xawtv -hwscan using typing terminal. I get this output:
looking for available devices
port 129-144
type : Xvideo, image scaler
name : Intel(R) Textured Video`
/dev/video0: OK
[ -device /dev/video0 ]
type : libv4l
name : HP HD Webcam [Fixed]
flags: capture
then I can access my webcam typing xawtv video0. I think I have no trouble with my webcam. I have trouble with opencv.
Upvotes: 5
Views: 17075
Reputation: 68
In some cases it is down to the inbuilt cameras response time (as it was in my case). I discovered that the in-built webcam on my HP G62 only "wakes up" after the first opencv cap.read(frame) call. Therefore to get a positive read from the camera (and hence no error later in the code) I made the call several times before proceeding:
if (!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
printf("Cam read error");
}
}
}
}
For me the optimum was 4 read calls, which ensured my camera was awake and on before running through the main block of code. It is possible that a simple "waitKey" call would work and only two read calls, although I haven't tried this.
Upvotes: 1
Reputation: 482
I solved my problem few minutes ago. And I decided share my solution for people who handling similar error.
First I didnt install some of below packets ( I dont remember which of them, so I paste all of them)
libjpeg62-dev
libtiff4-dev
zlib1g-dev
libjasper-dev
libavcodec-dev
libdc1394-22-dev
libgstreamer0.10-dev
libgstreamer-plugins-base0.10-dev
libavformat-dev
libv4l-dev
libswscale-dev
Then You should configure your cmake process with this code
cmake -D CMAKE_BULD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..
Please notice USE_V4L=ON this code..
I hope you solve after reading my solution.
Upvotes: 6
Reputation: 3076
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture webcam;
webcam.open(0);
if(!webcam.isOpened())//**EDITED**
{
std::cout<<"CANNOT OPEN CAM"<<std::endl;
return -1;
}
Mat frame;
while(true)
{
webcam >> frame;
imshow("TEST",frame);
waitKey(20);
}
return 0;
}
Try the above code...
Upvotes: 2