Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get values of specific indexes for numpy array?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading