简体   繁体   English

向量化批处理图像像素查找numpy数组

[英]Vectorizing batch image pixel lookup numpy arrays

Suppose I have an input numpy array of the form (B, H, W, C) and have a grid comprised of x values of the form (B, W) and y values of the form (B, H) . 假设我有一个形式为(B, H, W, C)的输入numpy数组,并具有一个由形式为(B, W) x值和形式为(B, H) y值组成的网格。

My goal is to use the x and y values to grab the C values of the image array. 我的目标是使用x和y值来获取图像数组的C值。 I'm able to do it when B = 1 that is when my image is of the form (H, W, C) and when x and y are of the form (H\\W, ) but have no clue how to expand this to a batch of images contained in a single numpy array. B = 1 ,即当我的图像为(H, W, C) ,当x和y为(H\\W, )形式时(H, W, C)我能够做到这一点(H\\W, )但不知道如何扩展它到单个numpy数组中包含的一批图像。

Is there some sort of reshaping I can take advantage of? 我可以利用某种形式的重塑吗?

Example

Suppose I have 2 cat images of size (400, 400, 3) . 假设我有2张大小为(400, 400, 3)猫图像。 Then input_img.shape = (2, 400, 400, 3) . 然后input_img.shape = (2, 400, 400, 3)

I have a list x.shape = (2, 400) and y.shape = (2, 400) and would like to index into input_img such that I obtain for each (x_i, y_i) an array z_i.shape = (C, ) totalling z.shape = (400, C) . 我有一个列表x.shape = (2, 400)y.shape = (2, 400)并希望索引到input_img ,以便为每个(x_i, y_i)一个数组z_i.shape = (C, )总计z.shape = (400, C)

What I'm trying to Vectorize 我正在尝试向量化

for i in range(batch_size):
    z_i = input_img[i, x[i], y[i]]

这是一种使用advanced-indexing的方法-

input_img[np.arange(batch_size)[:,None], x, y]

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

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