10

How do you rotate all frames in a video stream using OpenCV? I tried using the code provided in a similar question, but it doesn't seem to work with the Iplimage image object returned cv.RetrieveFrame.

This is the code I currently have:

import cv, cv2
import numpy as np

def rotateImage(image, angle):
    if hasattr(image, 'shape'):
        image_center = tuple(np.array(image.shape)/2)
        shape = image.shape
    elif hasattr(image, 'width') and hasattr(image, 'height'):
        image_center = (image.width/2, image.height/2)
        shape = np.array((image.width, image.height))
    else:
        raise Exception, 'Unable to acquire dimensions of image for type %s.' % (type(image),)
    rot_mat = cv2.getRotationMatrix2D(image_center, angle,1.0)
    result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR)
    return result

cap = cv.CaptureFromCAM(cam_index)
#cap = cv.CaptureFromFile(path)
fps = 24
width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT))

fourcc = cv.CV_FOURCC('P','I','M','1') #is a MPEG-1 codec

writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1)
max_i = 90
for i in xrange(max_i):
    print i,max_i
    cv.GrabFrame(cap)
    frame = cv.RetrieveFrame(cap)
    frame = rotateImage(frame, 180)
    cv.WriteFrame(writer, frame)

But this just gives the the error:

  File "test.py", line 43, in <module>
    frame = rotateImage(frame, 180)
  File "test_record_room.py", line 26, in rotateImage
    result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR)
TypeError: <unknown> is not a numpy array

Presumably because warpAffine takes a CvMat and not a Iplimage. According to the C++ cheatsheet, converting between the two is trivial, but I can't find any documentation on doing the equivalent in Python. How do I convert an Iplimage to Mat in Python?

Community
  • 1
  • 1
Cerin
  • 60,957
  • 96
  • 316
  • 522

5 Answers5

13

If you are just after a 180 degree rotation, you can use Flip on both axes,

replace:

frame = rotateImage(frame, 180)

with:

cv.Flip(frame, flipMode=-1)

This is 'in place', so its quick, and you won't need your rotateImage function any more :)

Example:

import cv
orig = cv.LoadImage("rot.png")
cv.Flip(orig, flipMode=-1)
cv.ShowImage('180_rotation', orig)
cv.WaitKey(0)

this: enter image description here becomes, this:enter image description here

fraxel
  • 34,470
  • 11
  • 98
  • 102
3

Through trial-and-error I eventually discovered the solution.

import cv, cv2
import numpy as np

def rotateImage(image, angle):
    image0 = image
    if hasattr(image, 'shape'):
        image_center = tuple(np.array(image.shape)/2)
        shape = tuple(image.shape)
    elif hasattr(image, 'width') and hasattr(image, 'height'):
        image_center = tuple(np.array((image.width/2, image.height/2)))
        shape = (image.width, image.height)
    else:
        raise Exception, 'Unable to acquire dimensions of image for type %s.' % (type(image),)
    rot_mat = cv2.getRotationMatrix2D(image_center, angle,1.0)
    image = np.asarray( image[:,:] )

    rotated_image = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR)

    # Copy the rotated data back into the original image object.
    cv.SetData(image0, rotated_image.tostring())

    return image0
Cerin
  • 60,957
  • 96
  • 316
  • 522
2

You don't need to use warpAffine(), take a look at transpose() and flip().

This post demonstrates how to rotate an image 90 degrees.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

This worked for me:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
flipped = cv2.flip(gray, flipCode = 1)
cv2.imshow('frame',flipped)

More on flipCode

Keno
  • 3,694
  • 2
  • 21
  • 40
0

opencv.version = 3.4.2

_, frame = cap.read() frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)

MSB
  • 1