1

The problem is about using rasterio to generate mask from GeoJSON file which contains geographic coordinates.

The function that I used is from:

from rasterio.tools.mask import mask
with rasterio.open(img_path) as src:
        img_mask, out_transform = mask(src, [poly], crop = False)

The shape of the mask that I generated is:

(4, 256, 256)

I converted this mask to (256,256) black and white image by taking the first channel of the mask. I used for loop to do this process couple times, and then I appended all the (256,256) masks into a list. I tried to use np.stack to convert the list to a numpy array with dimension (256,256,?). However, I got an array with dimension (?,256,256). I tried to set axis=-1; however, the image shape becomes (256,?) rather than (256,256).

Is there any method that can convert the channel first array to a channel last array with numpy?

1 Answer 1

2

If you just want to reorder your array axes:

array.transpose((1, 2, 0))

This will order the dimensions 0,1,2 (?,256,256) to 1,2,0 (256,256,?). Also take a look at the examples here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.moveaxis.html

1
  • Thank you so much. I just figured out where the error happens, and it is also a way to do that. I finally use np.stack with axis=-1, and it also solved the problem. The problem is that I did some mistake in indexing the dimension. Commented Oct 5, 2018 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.