我有两个数组[nx1],分别存储x像素(样本)和y像素(行)坐标。我还有另一个数组[nxn]存储一张图片。我想做的是创建第三个数组,它存储图像数组在给定坐标处的像素值。我已经使用以下代码实现了这一点,但想知道是否有内置的numpy函数可以更高效地完成。
#Create an empty array to store the values from the image.
newarr = numpy.zeros(len(xsam))
#Iterate by index and pull the value from the image.
#xsam and ylin are the line and sample numbers.
for x in range(len(newarr)):
newarr[x] = image[ylin[x]][xsam[x]]
print newarr
一个随机生成器决定了xsam和ylin的长度以及在图像中行进的方向。因此,每次迭代都是完全不同的。
ylin
和xsam
。如果你不这样做,newarr
将保持与ylin
或xsam
相同的形状,这非常有用(当然,OP 的代码返回一个 1D 的newarr
,但是如果你想要那样的话,你可以在最后使用.squeeze
压缩newarr
)。 - jorgeca