简体   繁体   中英

How to transform 3rd dimension of Numpy array loaded with ImageIO

I have a numpy array of shape (128,128,3) loaded from a PNG using ImageIO .

Dimension 3 seems to represent RGB values. In this instance all values for Dimension 3 are either [255,255,255] or [0,0,0] (ie white or black).

I want to get rid of the third dimension and replace it with a single 1D array containing 0 for black, and 1 for white. So the end result shape should be (128,128,1) .

I've attempted to use combinations of numpy.reshape and numpy.transpose but I'm really struggling to understand how to do this. I am a beginner to numpy and Python so I may be missing something very simple.

It's not missing, and it is very simple. Just index the channel you want:

im[:, :, 0]

To convert to zeros and ones, you can either make a boolean array:

im[:, :, 0].astype(np.bool)

or set 255 to one:

im = im[:, :, 0]
im[im > 0] = 1

A more advanced approach to creating a boolean array would be to view the underlying data as a boolean. This will only work well out of the box if the input is uint8 :

im[:, :, 0].view(dtype=np.bool)

Finally, to index the last dimension of an N dimensional array, you can use ellipsis:

 im[..., 0]

... (or the actual name Ellipsis ) in an index means "use : for all dimensions not listed explicitly." You can use it at most once in an index.

In general, you will want to read the documentation on indexing and later on broadcasting . There are gentler introductions it there, but the numpy documentation is quite comprehensive and straight from the horse's mouth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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