a sandwhich
a sandwhich

Reputation: 4448

Capture single image from webcam

What would be the best way using the best library to quickly capture a single image from a webcam with c++? EDIT:
Although the faq example showed how to capture a stream of frames, I morphed it a little to do what I need. Thank you.

#include "cv.h"  
#include "highgui.h"  
#include <stdio.h>
int main() {  
    CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);  
    if(!capture){  
        fprintf(stderr, "ERROR: capture is NULL \n");  
        getchar();  
        return -1;  
    }  
    cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);  
    IplImage* frame = cvQueryFrame(capture);  
    if(!frame) {  
        fprintf(stderr,"ERROR: frame is null.. \n");  
        getchar();        
    }  
    while(1) {  
        cvShowImage("mywindow", frame);  
        if((cvWaitKey(10) & 255) == 27) break;  
    }  
    cvReleaseCapture(&capture);  
    cvDestroyWindow("mywindow");  
    return 0;  
}  

Upvotes: 1

Views: 14706

Answers (1)

Paul R
Paul R

Reputation: 212929

OpenCV has C and C++ APIs, is cross-platform, and is very easy to use. There is an example in Learning OpenCV on page 26-27 which covers capturing a single frame from a webcam. There is also an example in the OpenCV FAQ: http://opencv.willowgarage.com/wiki/CameraCapture

Upvotes: 3

Related Questions