Thursday, January 20, 2011

Loading images from a Camera or Video

If you would like to do real time video processing you can even integrate your cameras into OpenCV.

It is easy to load an image from a camera. Ensure that camera is properly connected and detected by your computer.

CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
CvCapture* capture = cvCaptureFromAVI("infile.avi");

Incase there is only one camera, or to select any camera from multiple connecterd cameras, 0 can be replaced with -1.

The camera link is stored in the capture structure.

Alternatively, frames can also be processed from a video :

CvCapture* capture = cvCaptureFromAVI("filename.avi");

Retreiving the image frame from the link:

IplImage* img = 0; // Declare a new image pointer

if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");

img=cvRetrieveFrame(capture); // retrieve the captured frame

Important Caution :

One must release the image source after using it ->

cvReleaseCapture(&capture); // releases the capture link


  1. Please change code color. It making eyes pain.

  2. Thanks for sharing this Info Here I have samsung omnia I900 and I want to download pics from my phone to my laptop can you brefly let me know all that

    Khurram Shahzad
    Laptop is Much Batter