我有一个gray_image
shape 的图像(名为 )(1830, 1830)
。经过一些图像处理(我创建了超像素),我得到了一个名为segments
(形状(1830, 1830)
)的二维数组,其中包含从 0 到 72 的值。
我需要从找到值“0”的位置获取索引,segments
并使用该索引将值保存gray_image
在新数组(名为:)中arr
。
我认为一个示例将帮助您更好地理解我的问题:
假设我有这个图像,一个 3x3 维度的二维数组:
gray_image = numpy.array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
这是我的分段数组,一个 3x3 维度的 2D 数组:
segments = numpy.zeros([[0, 0, 1],
[0, 1, 2],
[1, 2, 2]])
我需要创建一个算法来输出arr
带有 shape的数组(3, 3)
。
arr = np.array([[1, 1, 2] # on the first line are the values from gray_image that correspond with value 0 from segments
[1, 2, 3] # on the second line are the values from gray_image that correspond with value 1 from segments
[2, 3, 3]]) # on the third line are the values from gray_image that correspond with value 2 from segments
我刚刚意识到每个段的数字计数是不同的,所以我不确定这是否可以使用 2D 数组。我正在考虑使用诸如字典之类的集合来保存与索引相关的所有信息。
所以,这是我到目前为止所做的:
i = 0
j = 0
k = 0
n = 0
m = 0
arr = np.empty([1830, 1830]) # empty array
for k in range(0, 72):
for i in range(0,1829):
for j in range(0,1829):
if segments[i][j] == k:
arr[m][n] = gray_image[i][j]
n = n + 1
if i == 1829 and j == 1829:
m = m + 1
但这根本不起作用,我遇到了这个错误:
arr[m][n] = gary_image[i][j]
IndexError: index 1830 is out of bounds for axis 0 with size 1830
我有点坚持了几天,所以任何建议都将受到高度赞赏。