Google+ My Python Projects: Accessing Images and Videos using Python OpenCV Google+

Tuesday, July 16, 2013

Accessing Images and Videos using Python OpenCV

In this post I am going to introduce you to OpenCV with a program to access image and video files.

Note: As I mentioned in a previous post accessing Video files with audio channels using OpenCV does NOT work on windows.

Access and display image from file:

The OpenCV library enables access to a wide variety of image files like jpg,png,bmp etc...
I assume the image to be displayed is on the same directory of this program.

download as text display_im.py.txt


I think I have put enough comments on the program itself.

      The cv2.imread() takes the function name as its argument.The cv2.waitKey() is used here in order to make the program display the image as long as you want.You will learn about it more in the following section.The 'im' object is a set of 3 two dimensional arrays in case of a 3-channel image(such 'RGB',actually it is in the order of 'BGR' in OpenCV)


sample image window displayed using above program:


Acess and display Video from file using OpenCV Python:

 OpenCV can access a wide variety of  video files too avi,mp4,vob,ogg....

I assume the video to be accessed resides in same directory of the program.

download as text display_vid.py.txt





Lines with comments ** are optional

A video is nothing but a sequence of image displayed above a critical frame rate.
So the underlying principle of video processing is,processing the video frame by frame with image processing techniques.

The cap.open() returns 'True' if video file access is successful,this property is used in the first if conditional.

The while loop iterates through frames of the video till the condition f=='True'

The resolution of video when displayed can be varied to any size using the cv2.resize() which takes two arguments:

1.image object(here frame)

2.tuple which have width and height as elements(here width=640,height=480)
and returns the resized image object.

This function can be used to any image,and applicable the image display program too.

The use cv2.waitKey() here is to display the video in proper frame rate.Without this line you will see virtually nothing even if the program displays the video.
The waitKey() slows down the display/frame rate.The cv2.waitKey() takes the duration in milliseconds(ms) as the argument.On encountering cv2.waitKey(x) program will wait for 'x' milliseconds and then executes the next line.

With cv2.waitKey(20) will wait for 20ms in every loop.So the time duration between display successive frames is approx. 20ms.(Actually it will be slightly more since,execution of other lines in the loop will contribute to some delay~1-2ms)

So the frame rate can be calculated as:

frame rate = no.of frames displayed/one second or

frame rate = 1/duration between frames

so here frame rate = 1/20ms =50frames/seconds

which is actually high.The movies you see are about 24 frames/sec or fps.

Anyway as long as your frame rate is above 16 fps you will see a seem less display of frame(Persistence of vision).

Sample window:

 

2 comments: