简体   繁体   English

ValueError:使用序列设置数组元素

[英]ValueError:setting an array element with a sequence

When I am trying to run this piece of the code i am getting following exceptions. 当我尝试运行这段代码时,我遇到了以下异常。 I have tried a lot of things. 我已经尝试了很多东西。 I know I am trying to assign a sequence to numpy array single value. 我知道我想分配一个序列给numpy数组单个值。

  grid = np.zeros((401, 401))
        for timestamps,poly in sorted(left_camera.iteritems()):
            polygonal = left_camera[timestamps]
            for i in range(0,len(polygonal)):
                if len(polygonal)>0:
                    row = np.int32(polygonal[i][0] * -10 + 200)
                    coloumn = np.int32(polygonal[i][1] * -10 + 200)
                    grid[row,coloumn] = np.array([255,0,0])
        cv2.imshow("image_name", grid)
        cv2.waitKey(0)

exception i am getting here: 例外,我要来这里:

grid[row,coloumn] = np.array([255,0,0])

ValueError: setting an array element with a sequence. ValueError:使用序列设置数组元素。

Here left_camera is my dict value with time stamps and some polygon is a list. 在这里,left_camera是我的带时间戳的dict值,而某些多边形是一个列表。

any solution will be much appreciated. 任何解决方案将不胜感激。 I am new to this . 我是新来的 。

Thanks in advance!! 提前致谢!!

The error points that np.array([255,0,0]) cannot fit in the matrix as it is not an expected value to be inserted in the matrix of shape (401, 401) . 错误点是np.array([255,0,0])无法放入矩阵中,因为它不是要插入到形状(401, 401) np.array([255,0,0])的矩阵中的期望值。 When you create the matrix = np.zeros((401, 401)) , it creates a matrix with height = 401 and width = 401. Since it has no third dimension, you can only substitute a single integer in this matrix as: 当您创建matrix = np.zeros((401, 401)) ,它将创建一个高度为401且宽度为401的矩阵。由于它没有三维尺寸,因此只能在该矩阵中替换为一个整数:

grid = np.zeros((401, 401))
grid[10, 10] = 20      # Works perfectly fine
grid[10, 10] = np.array([10, 20, 20])   # Raises Error

To fix this issue, you need to pass the third dimension while creating the matrix as: 要解决此问题,您需要在创建矩阵时传递第三维:

grid = np.zeros((401, 401, 3))
grid[10, 10] = np.array([10, 20, 20])   # Works perfectly fine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM