Skip to main content
added 8 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191

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)

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?

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?

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?

Source Link

How to convert masks generated by rasterio to channel last mask

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?