Ask Your Question
2

Getting single frames from video with python

asked 2012-07-25 04:19:00 -0600

Kea gravatar image

updated 2012-09-25 10:39:30 -0600

Kirill Kornyakov gravatar image

I am just getting started on using OpenCV using Python, and I will work with video. I have established connection to the web camera, using the script below. Now I would like to take out a couple of frames that can be analyzed. Is there some one who can help me get started on that?

I have the following code:

import cv2.cv as cv
import time
from scipy import *
import numpy
import sys, os, random, hashlib
from math import *

cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
        break

cv.DestroyAllWindows()
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2012-07-25 19:31:00 -0600

Hi Kea,

well, on each iteration the current frame captured from the camera is stored in the variable img. After the call to:

img = cv.QueryFrame(capture)

you can do whatever you want with img: Store it to disk, modify it in some way, detect objects... anything.

All the awesomeness is documented here

edit flag offensive delete link more

Comments

1

if you're going to store or modify the Mat (frame) provided by VideoCapture, you need to do it with its clone (use Mat::clone()) - because the returned frame refers to some internal VideCaptures's buffer that can be overwritten by the next requested frame.

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2012-07-26 01:30:27 -0600 )edit
1

Thank you!

Kea gravatar imageKea ( 2012-07-26 02:53:26 -0600 )edit
1

answered 2012-07-26 02:44:05 -0600

Kirill Kornyakov gravatar image

I suggest you to carefully read all the existing python samples: http://code.opencv.org/projects/opencv/repository/revisions/master/show/samples/python2. There are a lot of interesting use cases.

You could also study all the questions with the python tag here.

@Abid Rahman K has very interesting blog on the subject: http://www.opencvpython.blogspot.com/

edit flag offensive delete link more

Comments

Thank you!

Kea gravatar imageKea ( 2012-07-26 02:53:09 -0600 )edit
0

answered 2012-07-26 02:49:24 -0600

Kea gravatar image

updated 2012-07-26 02:55:00 -0600

Kirill Kornyakov gravatar image

Thanks a lot. That cleared up something. The following code will take a snapshot with the web cam and save it in the directory of the code:

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    # cv.ShowImage("camera", img)

    cv.SaveImage('test1.jpg', img)

    if cv.WaitKey(10) == 27:
        break
cv.DestroyAllWindows()
edit flag offensive delete link more

Comments

Hi Instead of opening a new question i would like to ask the same how to accomplish the same task capture snapshot using opencv 3.0 ?. Please point me to some examples or tutorials.

Thanks, krishna

vkichu gravatar imagevkichu ( 2016-01-19 18:11:50 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2012-07-25 04:19:00 -0600

Seen: 11,096 times

Last updated: Sep 25 '12