如何使用OpenCV旋转视频

10

如何使用OpenCV旋转视频流中的所有帧?我尝试了类似问题提供的代码,但它似乎无法与返回cv.RetrieveFrame的Iplimage图像对象一起使用。

这是我目前拥有的代码:

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)

但这只会产生错误:
  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

据推测,这是因为warpAffine需要一个CvMat而不是Iplimage。根据C++ cheatsheet,在两者之间进行转换很简单,但我找不到有关在Python中进行等效操作的文档。如何在Python中将Iplimage转换为Mat?
5个回答

13
如果您只需要进行180度旋转,可以在两个轴上使用Flip函数。
替换:
frame = rotateImage(frame, 180)

使用:

cv.Flip(frame, flipMode=-1)

这是“就地”操作,因此速度很快,您将不再需要使用rotateImage函数 :)

示例:

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

这个: enter image description here 变成了这个:enter image description here

1
这并不是对图像进行旋转,而是翻转,因此不能像旋转90度那样翻转。 - 김선달

3

通过不断的试错,我最终找到了解决方案。

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

这是做什么的?你能加上一个简短的描述吗? - Nicolas Gervais

2

你不需要使用warpAffine()函数,可以尝试使用transpose()flip()函数。

这篇帖子演示了如何将图像旋转90度。


2
这对我有用:

这个方法适用于我:

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

More on flipCode


0

opencv.版本 = 3.4.2

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接