I have clustered an image with 3 classes using the K_menas, but I would like the isolated pixel groups to be incorporated into the nearest large class. Is there a quick way to do this in the K_means parameter tuning, or do you need to change the classification algorithm? I have read that some use ISODATA, maybe it can be useful in my case, but I don't know.

with rasterio.open(raster)as ds:
    arr = arr[0,:, :]

K = KMeans(n_clusters=n_clusters, init='k-means++', n_init=10, max_iter=400, tol=0.0001,
           verbose=False, random_state=None, copy_x=True, n_jobs=1)
X_clustered = K.labels_
point = X_clustered.shape[0]
X_clustered = X_clustered.reshape(point,1)

enter image description here

  • This might work: stackoverflow.com/questions/46043048/…
    – BERA
    Oct 6 '20 at 13:07
  • Thanks for the suggestion, but it looks like I didn't solve much with that code, there are still isolated pixels
    – vins_26
    Oct 6 '20 at 13:56
  • Convert to raster, then Sieve
    – BERA
    Oct 7 '20 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.