I have a numpy array (shape : (256, 256, 3)) of image and
I have numpy array for indexing as below.
array([[ 3, 230],
[ 3, 231],
[ 3, 232],
...,
[114, 151],
[115, 149],
[115, 150]], dtype=int64)
However, I want to get values of numpy array (shape : (256, 256, 3)) of image for
indexing as below. So that, I can get 3 channels values for specific index.
array([[ 3, 230, :],
[ 3, 231, :],
[ 3, 232, :],
...,
[114, 151, :],
[115, 149, :],
[115, 150, :]], dtype=int64)
How can I get values?
>Solution :
You can just index into the first to dimensions of the image array like so:
import numpy as np
# create an example image with different entries for each channel
image = (np.ones((256, 256)) * np.arange(3).reshape(3, 1, 1)).T
idx = np.asarray([[3, 230],
[3, 231],
[3, 232],
[114, 151],
[115, 149],
[115, 150]], dtype=np.int64)
values = image[idx[:, 0], idx[:, 1]]
print(values)
Which prints:
[[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]]
I hope this helps!