Getting video data via OpenCV

I was able to get image data out of a .mpg file using the OpenCV Python wrapper.

After installing the following:

sudo apt-get install
gstreamer0.10-plugins-bad
gstreamer0.10-plugins-really-bad
gstreamer0.10-plugins-ugly
gstreamer0.10-ffmpeg

OpenCV was able to to read the .mpg file form the “CaputreFromFile” function.

I then grabbed the frame with “QueryFrame” which returned an iplImage object which has useful information such as width, height and nChannels among other things.

The raw pixel data can be retrieved by calling Get2D and passing in the image and the [x,y] coordinates, which retrieves a pixel. The pixel is an array of 3  (nChannels) different integers which can be accessed like so: pixel[0] pixel[1] pixel[2].

After all that here is the code:

#!/usr/bin/env python
import cv2.cv as cv
import sys

files = sys.argv[1:]

for f in files:
    print f

    capture = cv.CaptureFromFile(f)

    frame = cv.QueryFrame(capture)

    width = frame.width
    height = frame.height

    #just get the first 10 frame for brevity
    for i in xrange(10):   
        frame = cv.QueryFrame(capture)
        if frame:
            cloned_frame = cv.CloneImage(frame)
            print "pixel BGR"
            for i in range(0, int(height)):
                for j in range(0, int(width)):
                    pixel = cv.Get2D(frame, i, j)
                    #in the order: Blue Green Red
                    print repr(pixel[0]) + ", " + repr(pixel[1]) + ", " + repr(pixel[2])

It prints out all the pixel values, which does take quite some time, but isn’t necessary for any other reason but demonstration.

 

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *