Has anyone has success with building models using KMeans for classification? I have images that only have one band and it continues to fail. My guess is that the issue is with both size of the image as well as the single band.
from osgeo import gdal,gdal_array import numpy as np src = '/Path/ImgA.TIF' img_A = gdal.Open(src) #Getting bands (count) bands_n = img_A.RasterCount #returns 1 band = img_A.GetRasterBand(1) #read as array band_arr = band.ReadAsArray() band_sh = band.shape #eg. output# 300,300 //This is where I am getting stuck. If I pass only two inputs (rows/cols) to KMeans, it fails as it requires a 2D array not 1D. It also fails when I manually set the band:// #Attempt 1 rows, cols = band_sh #Attempt 2 rows, cols, band = band_sh, 1 X = band.reshape(rows*cols,band) // X = band.reshape(rows*cols,1) from scikit.cluster import KMeans kmeans = KMeans(n_classes = 2, random_state=2).fit(X)
Any ideas? This works just fine with RGB but fails each time when dealing with rasters that are single band.