错误:只能将size1数组转换为Python标量

2024-09-30 01:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究numpt矩阵,我可以创建一个HSV图像,然后在RGB图像中转换。我创建了一个矩阵HSV,包括: matrix_=np.zeros([3, H-kernel+1, W-kernel+1], dtype=np.float32)

在我用255,θ角的大小填充它之后,每个值: matrix_[:, row, column]=np.array([th, 255, mag]) 最后,我将其转换为: cv2.cvtColor(matrix_, matrix_, cv2.COLOR_HSV2RGB)

但是它在cv2.cvtColor中抛出“TypeError: only size-1 arrays can be converted to Python scalars”。为什么?我哪里错了


Tags: 图像npzeroscolumn矩阵rgbcv2kernel
1条回答
网友
1楼 · 发布于 2024-09-30 01:20:01

您正在混合参数的顺序

cvtColor的文件:

Python: cv2.cvtColor(src, code[, dst[, dstCn]]) → dst

如您所见,目标矩阵是第三个参数。
code是第二个参数。
code应该是标量,但是您正在将matrix_作为第二个参数传递,因此您得到了错误:
"TypeError: only size-1 arrays can be converted to Python scalars"

为避免错误,您可以使用:

cv2.cvtColor(matrix_, cv2.COLOR_HSV2RGB, matrix_)

最好使用返回值(语法更清晰):

matrix_ = cv2.cvtColor(matrix_, cv2.COLOR_HSV2RGB)

现在还有另一个例外:"Invalid number of channels in input image"

您将矩阵形状设置错误:

它应该是:

matrix_=np.zeros([H-kernel+1, W-kernel+1, 3], dtype=np.float32)

语法matrix_[:, row, column]=np.array([th, 255, mag])看起来很奇怪。
您没有发布rowcolumnthmag的值。
我不能说它是否正确。
我必须假设这超出了你的问题范围

相关问题 更多 >

    热门问题