Ask Your Question

OpenCV c++ K-Means Color Clustering

asked 2018-01-09 04:33:12 -0500

piowes86 gravatar image


I found some interesting article about colour clustering with OpenCV and Python:

I am trying to convert that into c++. Until now without success. Did someone do that already?

Currently I have a problem with following lines:

// reshape the image to be a list of pixels image = image.reshape((image.shape[0] * image.shape[1], 3))

// cluster the pixel intensities clt = KMeans(n_clusters = args["clusters"])

edit retag flag offensive close merge delete



you probably should not try to solve a c++ problem from a python blog ..

berak gravatar imageberak ( 2018-01-09 04:34:53 -0500 )edit

I know, but I need to perform image so like in provided link. Needed results in my situation is to obtain information about 3-4 main colours on the image with the information about quantity of the colour on the image.

piowes86 gravatar imagepiowes86 ( 2018-01-09 04:45:27 -0500 )edit

"Needed results in my situation is to obtain information about 3-4 main colours on the image with the information about quantity" -- can it be you wanted a histogram, instead, not clustering (which makes "artificial" centers ?

berak gravatar imageberak ( 2018-01-09 05:17:49 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-09 04:48:10 -0500

berak gravatar image

assuming a 3channel, bgr image (CV_8UC3):

Mat ocv = ...

// convert to float & reshape to a [3 x W*H] Mat 
//  (so every pixel is on a row of it's own)
Mat data;
data = data.reshape(1,;

// do kmeans
Mat labels, centers;
kmeans(data, 8, labels, TermCriteria(CV_TERMCRIT_ITER, 10, 1.0), 3, 
       KMEANS_PP_CENTERS, centers);

// reshape both to a single row of Vec3f pixels:
centers = centers.reshape(3,centers.rows);
data = data.reshape(3,data.rows);

// replace pixel values with their center value:
Vec3f *p = data.ptr<Vec3f>();
for (size_t i=0; i<data.rows; i++) {
   int center_id =<int>(i);
   p[i] =<Vec3f>(center_id);

// back to 2d, and uchar:
ocv = data.reshape(3, ocv.rows);
ocv.convertTo(ocv, CV_8U);
edit flag offensive delete link more


Thank you Berak for the code. I was trying to execute the code and I received the following error: The total width is not divisible by the new number of channels in function reshape . Do you know what can be the reason of that and how to solve that?

piowes86 gravatar imagepiowes86 ( 2018-01-09 05:10:15 -0500 )edit

you probably tried with a grayscale image as input

berak gravatar imageberak ( 2018-01-09 05:16:11 -0500 )edit

I was able to solve the problem I have changed centers.reshape(3,centers.rows); into centers.reshape(0,centers.rows); The results are like follow: (original image) (after perform). I do not convert image into grayscale. I strictly use your code.

piowes86 gravatar imagepiowes86 ( 2018-01-09 05:26:54 -0500 )edit

ok, that's the same, for a single channel image.

berak gravatar imageberak ( 2018-01-09 05:28:04 -0500 )edit

Question Tools

1 follower


Asked: 2018-01-09 04:33:12 -0500

Seen: 7,733 times

Last updated: Jan 09 '18