Capture Images and Video from Camera in OpenCV 2.3.1


OpenCV 2.3.1 python bindings have ctypes and no longer support swig bindings. OpenCV 2.3.1 python bindings are quite stable and much faster than OpenCV 2.1 swig bindings. Although OpenCV 2.3.1 has many new features, it still supports some of OpenCV 2.1’s functions. Moving on to capturing images from camera.


CaptureFromCAM

Capturing a single frame:
from cv2.cv import *
# Initialize the camera
capture = CaptureFromCAM(0)  # 0 -> index of camera
if capture:     # Camera initialized without any errors
   NamedWindow("cam-test",CV_WINDOW_AUTOSIZE)
   f = QueryFrame(capture)     # capture the frame
   if f:
       ShowImage("cam-test",f)
       WaitKey(0)
DestroyWindow("cam-test")

To capture video, capture frames in a loop with appropriate waitkey. This method of capturing frames is similar to that of OpenCV 2.1

VideoCapture

Capturing a single frame:
from cv2 import *
# initialize the camera
cam = VideoCapture(0)   # 0 -> index of camera
s, img = cam.read()
if s:    # frame captured without any errors
    namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
    imshow("cam-test",img)
    waitKey(0)
    destroyWindow("cam-test")

This method is most extensively used to capture frames in OpenCV 2.3.1.

P.S. Studying Thermodynamics. We Mech people also study cool stuff.

Advertisements

23 thoughts on “Capture Images and Video from Camera in OpenCV 2.3.1

  1. Good series of blogs on OpenCV – will definitely take a look at this sometime once I figure out something I want to do with images 🙂

  2. Hey I have some trouble in obtaining live feed using open-cv

    #include “highgui.h”
    #include “cv.h”

    int main()
    {
    char c;
    CvCapture* camera = cvCreateCameraCapture(-1);
    cvNamedWindow(“live feed”,CV_WINDOW_AUTOSIZE);
    IplImage* frame;
    while(1)
    {
    if(!frame)
    break;
    frame = cvQueryFrame(camera);
    cvShowImage(“live feed”,frame);
    c = cvWaitKey(33);
    if(c==27)
    break;
    }
    }

    Its breaking without scanning.Can you tell what is wrong.

    • scanning ?
      I think your camera is not working properly. and your code is also incorrect.

      #include<opencv2/highgui/highgui.hpp>
      #include<stdio.h>
      int main()
      {
          CvCapture* cam = cvCreateCameraCapture(-1);
          cvNamedWindow("livefeed",CV_WINDOW_AUTOSIZE);
          IplImage* frame;
          
          if (!frame)
          {
              printf("frame NULL");
          }
          else
          {
              printf("frame not NULL");
          }
      
          return (0);
      }
      

      try this code. compare it with your code. and see where you have done the mistake.

  3. Pingback: Get data from Mat / cv::Mat in OpenCV « Paranoid Android

  4. Your blog has been helpful to me in getting started with opencv on ubuntu. I am now trying to capture video from my laptop webcam and show it in a window. I tried in both c and c++, and I am having some trouble in each case. When I run the program, I get an output something as follows.

    VIDIOC_QUERYMENU: Invalid argument
    VIDIOC_QUERYMENU: Invalid argument
    VIDIOC_QUERYMENU: Invalid argument
    VIDIOC_QUERYMENU: Invalid argument
    VIDIOC_QUERYMENU: Invalid argument
    VIDIOC_QUERYMENU: Invalid argument
    select timeout
    select timeout
    select timeout
    select timeout
    select timeout
    select timeout

    the images are grabbed and shown fine for about 10 seconds after which the “select timeout” error is displayed continuously and no new frames are grabbed. I read online that this error may have to do with drivers. But I couldn’t find a satisfactory solution.

    Have you come across such an error and do you know how one may fix it?

    Thanks

    • I am happy that my blog is helping you to get started with OpenCV.

      I haven’t come across something like this. But when I was using Ubuntu 11.04 and 11.10, my webcam didn’t work. After I installed OpenCV 2.3.1 in 11.10, it started working fine. In 12.04, I haven’t had that problem. So maybe you could tell me what version of OpenCV and Ubuntu are you using ? I might find something useful.

      Cheers,
      Jay

    • So I did some Google search regarding your problem. It seems that there’s some problem with your webcam driver (as you suggested). I would advise you to install v4l driver again. If you’re using some older version of OpenCV, I would suggest you to install current version or 2.4 series of OpenCV with correct drivers. see this post.

      Cheers,
      Jay

      • Thanks for the reply. I followed the steps you have provided and installed opencv2.4.2. The problem still persists. I have tried running the face detection sample program in the opencv folder with the same result. It works fine for about 10 seconds and the video freezes, while on the terminal “select timeout” is displayed continuously.

        By the way, the bash script that you provided for installing opencv2.4.2 is useful. But if some libraries are missing (as was the case with me) it was ending up installing opencv 2.3.1 (possibly from the repositories). Manual directions are of course working, except for the statement “sudo echo “/usr/local/lib” >> /etc/ld.so.conf” – I get a Permission denied error. But I executed “sudo ldconfig” anyway and opencv started working.

      • hi , i am also facing same problem Mr Jay faces
        m using external webcam A4 tech, i have ubuntu 12.o4 lts + opencv 2.4.2

  5. Hi Jayrambhia

    I am using OpenCV 2.4.2 on Ubuntu 12.04 and trying to capture frames from a list of videos and performing some kind of description on them. I started with one video – captured frames, and this line here:

    num_frames = capture.get (CV_CAP_PROP_FRAME_COIUNT)

    gives me the total number of frames of my example video to be 4130 [I’m sampling at a rate of 1fps actually]. My issue is that when I capture the frames, after 4124th frame, it gives a capture error. Meaning these lines of code:

    if (!capture.read(frame)) {
    cout << "Can't read frame!" << endl;
    }

    yields the error message after frame 4124.
    The question is: What can be the reason behind video capture not working midway between a video [this is not a video from webcam – it's on the disk].

    Thanks very much.

  6. Thanks for the reply.

    I checked all other videos. Apparently, this is an issue with OpenCV not being able to calculate the total no. of frames correctly. It’s weird. But then I thought, heck, doesn’t matter if I miss out on a few frames and am making do with the fewer number of frames now.

  7. Iam trying to capture from SMI Grabber Camera
    and replaced the instruction p_capWebcam = cvCaptureFromCAM(0);
    with p_capWebcam = cvCaptureFromCAM(1);
    to get SMI Grabber Camera capturing but It doesn’t preview any thing and a timeout is occurred
    please I want some one Help me
    Thanks

  8. hello ……..
    i want to take picture from camera and save it hard drive…….
    i m using imwrite function…
    but it is taking picture of last frame…
    after using some hand justure i need to grab the picture..
    after reading justure it is grabs picture of last frame before justure…
    can any one help..

  9. Pingback: Python:Capture image for processing – IT Sprite

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s