Perspective transform with OpenCV

Standard

We can change perspective in OpenCV using the functions cv2.getPerspectiveTransform(pts1,pts2) and cv2.warpPerspective(img,M,(640,480)). The cv2.getPerspectiveTransform() function calculates a perspective transform from four pairs of the corresponding points. The points pts1 and pts2 are two rectangles, where on is the source and the other the destination.  Putting it together you get something like this:

Which you can combine with any image input, such a video playing from the web:

 Output:

Mixing OpenGL and OpenCV

Standard

In this example we mix OpenGL (a computer graphics library) and OpenCV. We will play a video on a 3d rotating cube. (Yes, it will be animated!) We mix the code for video playing with code to render a 3d cube with PyOpenGL. If you do not have PyOpenGL installed install that first:

If you are totally unfamiliar with OpenGL, I recommend reading these tutorials.  If you are familiar with OpenGL, the code should be straight forward.

The program will grab frames from the video using OpenCVs  cv2.VideoCapture().  These frames will be converted to OpenGL texture data structure. OpenGL will render the cube like it would render any textured 3d cube. The difference being, that texture are now frames of a video stream. The conversion of the OpenCV Mat datastructure format to the OpenGL texture format is:

Finally the full code:

Output: