0
import rasterio as rio
from rasterio.plot import show
from sklearn import cluster
import matplotlib.pyplot as plt
import numpy as np
import glob

for filepath in glob.iglob('./dengue3/*.tiff'):
    elhas_raster = rio.open(filepath)
    elhas_arr = elhas_raster.read() # read the opened image
    vmin, vmax = np.nanpercentile(elhas_arr, (5,95))  # 5-95% contrast stretch
    # create an empty array with same dimension and data type
    imgxyb = np.empty((elhas_raster.height, elhas_raster.width, elhas_raster.count), elhas_raster.meta['dtype'])
    # loop through the raster's bands to fill the empty array
    for band in range(imgxyb.shape[2]):
        imgxyb[:,:,band] = elhas_raster.read(band+1)
    #print(imgxyb.shape)
    # convert to 1d array
    img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],imgxyb.shape[2]))
    #print(img1d.shape)

Above code I am using to read the tiff images in a folder and get the arrays. However, the output is -

ValueError: cannot reshape array of size 6452775 into shape (921825,12)

Images are 12 band. I tried using 12 in place of 7 in the above code, but the code doesnt execute. How do I resolve this? Thank you for your time.

1 Answers1

0

You have changed the size of the index you're trying to reshape, but not the reshape command parameter:

img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],imgxyb.shape[2]))

This should be:

img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],7))
jhso
  • 3,103
  • 1
  • 5
  • 13