我正在尝试从实时网络摄像头旋转帧图像,但出现一些错误,当我用于系统中任何保存的图像时,相同的代码可以正常工作,但如果有人可以,它不能与网络摄像头一起正常工作提前帮我thanx
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")
程序中的错误是
回溯(最近一次通话最后):
trackcall(0) 中的文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 44 行
文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 6 行,trackcall image0 = rotateImage(image, angle)
文件“D:\VideoRotation\imageRotationWithTrackbar.py”,第 19 行,在 rotateImage 中引发异常,“无法获取类型 %s 的图像尺寸。” %(类型(图像),)
例外:无法获取类型的图像尺寸。