【问题标题】:opencv python video rotationopencv python视频旋转
【发布时间】:2012-10-22 06:25:45
【问题描述】:

我正在尝试从实时网络摄像头旋转帧图​​像,但出现一些错误,当我用于系统中任何保存的图像时,相同的代码工作正常,但网络摄像头无法正常工作,如果有人可以提前帮助我,谢谢

import cv2.cv as cv

import cv2

import numpy as np

def trackcall(angle):

        image0 = rotateImage(image, angle)
        cv.ShowImage("imageRotation",image0);


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

angle = 720

vc = cv2.VideoCapture(0)

cv.NamedWindow('imageRotation',cv.CV_WINDOW_NORMAL)
cv.NamedWindow('imageMeter',cv.CV_WINDOW_AUTOSIZE)
cv.CreateTrackbar("rotate","imageMeter",360,angle,trackcall)
#image = cv.LoadImage('C:/Users/Public/Pictures/Sample Pictures/Desert.jpg', cv.CV_LOAD_IMAGE_COLOR)
#image0 = rotateImage(image, angle)
if vc.isOpened():

        rval = True, image = vc.read()
else:
        rval = False

while rval:

        image = vc.read()
        trackcall(0)


key = cv.WaitKey(0)
if key == 27:

    cv.DestroyWindow('imageRotation')
    cv.DestroyWindow("imageMeter")

程序中的错误是

Traceback(最近一次调用最后一次):

文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 44 行,在 跟踪调用(0)

文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 6 行,在 trackcall 中 image0 = rotateImage(图像, 角度)

文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 19 行,在 rotateImage 引发异常,“无法获取类型 %s 的图像尺寸。” % (类型(图片),)

异常:无法获取类型的图像尺寸。

【问题讨论】:

    标签: python opencv image-rotation


    【解决方案1】:

    要在相机旋转时忽略图像中的旋转,您可以使用此代码

    import cv2
    import numpy as np
    cap = cv2.VideoCapture (0)
    
    width = 400
    height = 350
    
    while True:
        ret, frame = cap.read()
        frame = cv2.resize(frame, (width, height))
        flipped = cv2.flip(frame, 1)
        framerot = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
        framerot = cv2.resize(framerot, (width, height))
        StackImg = np.hstack([frame, flipped, framerot])
        cv2.imshow("ImageStacked", StackImg)
        if cv2.waitKey(1) & 0xff == ord('q'):
            break
    cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案2】:

      您没有正确阅读网络摄像头的供稿。 vc.read() 返回两个值,retvalimage。见the documentation

      您的问题是您将两者都读入单个值,即image,这就是为什么您的图像似乎被表示为一个元组。

      所以image = vc.read() 应该变成retval, image = vc.read()

      但是,我认为在您使用 cv2.getRotationMatrix2D(image_center, angle,1.0) 时,您的代码中可能还有其他问题...如果我能看到有什么问题,我会稍后再查看。

      【讨论】:

      • 是的,你是对的,它在函数 cv2.getRotationMatrix2D(image_center, angle, 1.0) 中给出了错误,该函数只需要 2 个参数(给定 3 个),但在文档中它有 3 个参数 @ 987654322@
      • 我修复了元组问题。但我无法解决旋转功能问题。 cv2.getRotationMatrix2D(图像中心,角度,1.0)。是否有任何其他功能可以让我做与我尝试使用 cv2.getRotationMatrix2D(...) 功能相同的工作
      • 您可能应该将其作为一个新问题提出,这样您可能会得到更多回应。关闭这个问题,也许有人可以帮助你解决新问题。我的论文需要在下周提交,所以很遗憾我现在不能帮助你,但如果你阅读文档并在你的新问题中准确解释你想要做什么,我相信有人能够帮助你。有空的时候我也会看看的!
      猜你喜欢
      • 2021-08-03
      • 2012-05-20
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2016-10-06
      相关资源
      最近更新 更多