gpt4 book ai didi

python - 为什么索引错误: index 1 is out of bounds for axis 0 with size 1

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:16 24 4
gpt4 key购买 nike

以下代码用于在给定位置 x y 坐标和变量“locs”的情况下查找图像的关键点。tp=256,r=180,c=240。在调试过程中,我收到 desc[idx-1, ] = tmp 行的索引错误。

tp = compareX.shape[0]
desc = np.zeros((1, tp))
r = im.shape[0]
c = im.shape[1]
idx = 1
new_locs = np.zeros((locs.shape))


for i in range(1, locs.shape[0]):

x = math.floor(locs[i - 1, 0]) # keypoint x coordinate
y = math.floor(locs[i - 1, 1]) # keypoint y coordinate
#non-maximum suppression
if (x - 4) < 1 or (y - 4) < 1 or (x + 4) > c or (y + 4) > r:
continue

#save passing coordinates
new_locs[idx - 1,] = np.array([x, y])

#Calculate BRIEF descriptor
tmp = np.zeros((1, tp))

x = x-4
y = y-4
for j in range(1, tp):
ax = x + math.floor((compareX[j-1]-1)/patchWidth)
ay = y + ((compareX[j-1]-1) % patchWidth)
bx = x + math.floor((compareY[j-1] - 1) / patchWidth)
by = y + ((compareY[j-1] - 1) % patchWidth)
if (im[ay, ax] < im[by, bx]).all():
tmp[0, j-1] = 1
***desc[idx-1, ] = tmp***
idx = idx + 1
locs = new_locs
return locs, desc

最佳答案

您已将 desc 设置为维度 1xtp(将其视为 1 行,tp 列)。

tp = 5

desc = np.zeros((1, tp))
# array([[ 0., 0., 0., 0., 0.]])

desc.shape
# (1, 5)

使用零索引,这意味着当轴 0(行轴)中只有一行时,引用 desc[1, ] 会请求第 2 行。
这就是您收到错误的原因:当 idx == 2 时,则 desc[idx-1, ] == desc[1, ]
也许您想转而索引到轴 1?例如。 desc[,1]

关于python - 为什么索引错误: index 1 is out of bounds for axis 0 with size 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52685868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com