Question about rasterio, imageio and opencv python


Keygen
 

rasterio is working for me in situations where PIL isn't. Might be a hacky workaround, but it's doing its job for the moment.

I get the same behavior if I open the file with img = rasterio.open(path)

Is this because it's opening the file and turning it into a dataset?

If so, what is the preferred way to convert the dataset into an image for performing transformations on it with something like opencv?

Thanks


Sean Gillies
 

Hi,

reshape_as_image is intended for use with data read using rasterio. If you're not using rasterio to read your data, you probably don't need it.

On Sun, Dec 12, 2021 at 8:26 AM Keygen <projects@...> wrote:
I have a question about using rasterio.plot.reshape_as_image() and rasterio.plot.reshape_as_raster() in python.

I noticed that reshape_as_image seems to put the shape in the correct order if I print with numpy.shape, however opencv throws an exception when trying to resize() as if the size is empty (which usually happens if the shape is out of order). (-215:Assertion failed) !dsize.empty() in function 'resize'

If instead I use rasterio.plot.reshape_as_raster(my_image).transpose() then opencv works normally and resizes properly without exception.

The shape seems to be correct with both if I print the shape, so not sure why opencv is complaining about it.

So far I've only tested this with code that is reading from float32 RGB TIFF files with imageio, which uses tifffile for the tiff handling, so it should be a normal numpy array. Was curious if anyone noticed this behavior and what might be causing the exception.

Basic Example:
img = imageio.imread(path)
img2 = rasterio.plot.reshape_as_image(img)
resized = cv2.resize(img2, (300,300), interpolation=cv2.INTER_LANCZOS4)



--
Sean Gillies


Keygen
 

I forgot to note that later in the script I'm working on, the image being resized is pulling the w,h from the original (img2 in the example), so it is possible that something funky is happening there, though rasterio is still using the same code for that other image before it hits the resize.


Keygen
 

I have a question about using rasterio.plot.reshape_as_image() and rasterio.plot.reshape_as_raster() in python.

I noticed that reshape_as_image seems to put the shape in the correct order if I print with numpy.shape, however opencv throws an exception when trying to resize() as if the size is empty (which usually happens if the shape is out of order). (-215:Assertion failed) !dsize.empty() in function 'resize'

If instead I use rasterio.plot.reshape_as_raster(my_image).transpose() then opencv works normally and resizes properly without exception.

The shape seems to be correct with both if I print the shape, so not sure why opencv is complaining about it.

So far I've only tested this with code that is reading from float32 RGB TIFF files with imageio, which uses tifffile for the tiff handling, so it should be a normal numpy array. Was curious if anyone noticed this behavior and what might be causing the exception.

Basic Example:
img = imageio.imread(path)
img2 = rasterio.plot.reshape_as_image(img)
resized = cv2.resize(img2, (300,300), interpolation=cv2.INTER_LANCZOS4)