I have an array in the shape of (105, 105, 3). When I do plt.imshow(array) it outputs: enter image description here

How can I run a clustering algorithm directly on this image? Do I need to convert the pixels to cartesian coordinates and if so, how?

I have tried:

labels = mixture.GaussianMixture(n_components=2).fit(array).predict(array)

However this outputs an error. Please help!


1 Answer 1


You are right, a clustering algorithm such as Gaussian Mixture Model would require cartesian coordinates.

This can be done as follows to get the coordinates of the dark pixels:

import numpy as np

binary = np.mean(array, axis=2) < 0.5
X = np.stack(np.where(binary)).transpose()

You should now be able to cluster X

Note: For images, segmentation is more common than clustering. Both types of processing are related and segmentation can be seen as a special kind of clustering, but often aims for sligtly different outcomes. Not knowing your desired outcome, I just want to hint you to these kind of algorithms in case they fit to your task.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.