# Grabbing the amount of pixels in K-Means Clusters in C++

**URL:** <https://forum.opencv.org/t/grabbing-the-amount-of-pixels-in-k-means-clusters-in-c/8292>\
**Category:** C++\
**Tags:** kmeans\
**Created:** [April 7, 2022, 12:26am UTC](https://forum.opencv.org/t/grabbing-the-amount-of-pixels-in-k-means-clusters-in-c/8292 "2022-04-07T00:26:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaestroFries](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/maestrofries/32/5336_2.png) [@MaestroFries](https://forum.opencv.org/u/MaestroFries)\
**Post date:** [April 7, 2022, 12:26am UTC](https://forum.opencv.org/t/grabbing-the-amount-of-pixels-in-k-means-clusters-in-c/8292/1 "2022-04-07T00:26:44Z")

</div>

I have the following code which takes an image and displays a color palette using K-means clustering.

```auto
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"

using namespace cv;
using namespace std;

Mat showCenters(const Mat& centers, const Mat& data, int siz = 64) {

    Mat cent = centers.reshape(3, centers.rows);
    //april 4 int dat = data.total();

    // make a horizontal bar of K color patches:
    Mat draw(siz, siz * cent.rows, cent.type(), Scalar::all(0));

    cout << endl << "BGR Dominance in order\n";
    for (int i = 0; i < cent.rows; i++) {
        // set the resp. ROI to that value (just fill it):
        draw(Rect(i * siz, 0, siz, siz)) = cent.at<Vec3f>(i, 0);
        cout << i+1 << ": " <<cent.at<Vec3f>(i, 0) << endl << endl;
        //cout << dat << endl;
    }
    draw.convertTo(draw, CV_8U);

    // optional visualization:

    imshow("CENTERS", draw);
    waitKey();

    //imwrite("centers.png", draw);

    return draw;
}

int main()
{

    Mat src;
    std::string image_path = samples::findFile(" "); //put file path within parameter

    // do kmeans

    //iterate through each and every compactness to determine optimal k:
    for (int k = 1; k < 15; k++)
    {
        src = imread(image_path, IMREAD_COLOR);

        Mat labels, centers;
        Mat data;
        src.convertTo(data, CV_32F);
        data = data.reshape(1, data.total());

        double compactness = kmeans(data, k, labels, TermCriteria(TermCriteria::MAX_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 = labels.at<int>(i);
            p[i] = centers.at<Vec3f>(center_id);
        }

        // back to 2d, and uchar:
        src = data.reshape(3, src.rows);
        src.convertTo(src, CV_8U);

        std::cout << "compactness at K = " << k << ": " << compactness << endl;

        //visualize data:
        imshow("Image", src);
        showCenters(centers, data);

        destroyAllWindows();
    }

}

```

Now, I would like to grab the amount of data points (or pixels) that are initialized to a cluster. How would I be able to do that? Is there a function will allows me to do that?

Hopefully, I would be able to grab the percentage of colours using that data.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 7, 2022, 7:33am UTC](https://forum.opencv.org/t/grabbing-the-amount-of-pixels-in-k-means-clusters-in-c/8292/2 "2022-04-07T07:33:13Z")

</div>

> [@MaestroFries](#):
>
> `labels`

you have that array.

just count the occurrences of each label.

---

<div class="post-metadata">

**Author:** ![MaestroFries](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/maestrofries/32/5336_2.png) [@MaestroFries](https://forum.opencv.org/u/MaestroFries)\
**Post date:** [April 7, 2022, 6:28pm UTC](https://forum.opencv.org/t/grabbing-the-amount-of-pixels-in-k-means-clusters-in-c/8292/3 "2022-04-07T18:28:55Z")

</div>

How? Not sure how to count each “0”, “1”, and so forth.

The OpenCV article on Kmeans has this:

```auto
A = z[labels==0]

B = z[labels==1]

```

Since that’s Python, I’m not sure how to implement that for C++.
