我试图编写代码来旋转图像,我认为旋转图像的逻辑是正确的,但是每当我尝试显示旋转的图像时,都会出现以下错误:
TypeError:参数'mat'预期的cv :: UMat
我是python的新手,并且是第一次使用open cv,所以我对此并不了解
我检查了旋转图像的类型,它出现了“类'numpy.ndarray'”,我认为open cv可以在“ nd array”上工作
这是我的密码
import cv2
import numpy as np
def rotateToNinety(imageProcessing):
imageProcessing = imageProcessing[::-1]
rotatedImage = np.array([[[None for z in range(3)] for x in range(len(imageProcessing))]for y in range(len(imageProcessing[0]))])
for i in range(len(imageProcessing[0])):
for j in range(len(imageProcessing)):
for k in range(3):
rotatedImage[i][j][k] = imageProcessing[j][i][k]
return rotatedImage
image = cv2.imread("download (2).jpg",1)
print("Original Image".center(50,"="))
print(image)
cv2.imshow("Original",image)
cv2.waitKey(0)
print(type(image))
rotated_90 = rotateToNinety(image)
print("Rotated Image".center(50,"="))
print(rotated_90)
print(type(rotated_90))
cv2.imshow("rotated",rotated_90)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:2)
我遇到了同样的问题并使用:
image=np.uint8(image)
图像是您的cv.imread()对象。
答案 1 :(得分:0)
您可以尝试更换
def rotateToNinety(imageProcessing):
imageProcessing = imageProcessing[::-1]
rotatedImage = np.array([[[None for z in range(3)] for x in range(len(imageProcessing))]for y in range(len(imageProcessing[0]))])
for i in range(len(imageProcessing[0])):
for j in range(len(imageProcessing)):
for k in range(3):
rotatedImage[i][j][k] = imageProcessing[j][i][k]
return rotatedImage
到
def rotateToNinety(imageProcessing):
h,w,c = imageProcessing.shape
rotatedImage = np.zeros([h,w,c], dtype=np.uint8)
imageProcessing = imageProcessing[::-1]
for i in range(h):
for j in range(w):
for k in range(3):
rotatedImage[i][j][k] = imageProcessing[j][i][k]
return rotatedImage
在您的情况下,您将列与行混合在一起,请以后使用np.zeros()
来定义空白图像。
答案 2 :(得分:0)
通常,当您的图像路径错误时,可能会发生此错误。因此,您可能想再次检查图像路径。