【问题标题】:How to rotate a video with OpenCV如何使用 OpenCV 旋转视频
【发布时间】:2012-05-20 12:04:46
【问题描述】:

如何使用 OpenCV 旋转视频流中的所有帧?我尝试使用similar question 中提供的代码,但它似乎不适用于返回 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?

【问题讨论】:

    标签: python image-processing opencv


    【解决方案1】:

    如果你刚刚旋转了 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)
    

    这个: 变为:

    【讨论】:

    • 这不会旋转图像。它会翻转,所以翻转不能像 90 度那样旋转
    【解决方案2】:

    经过反复试验,我最终找到了解决方案。

    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
    

    【讨论】:

    • 这是做什么的?你能添加一个简短的描述你在做什么吗?
    【解决方案3】:

    你不需要使用warpAffine(),看看transpose()和flip()。

    This post 演示如何将图像旋转 90 度。

    【讨论】:

      【解决方案4】:

      这对我有用:

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

      More on flipCode

      【讨论】:

        【解决方案5】:

        opencv.版本 = 3.4.2

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

        【讨论】:

          猜你喜欢
          • 2021-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-08
          • 2011-04-25
          • 1970-01-01
          • 1970-01-01
          • 2015-01-15
          相关资源
          最近更新 更多