Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image.

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image.

image. (Akin similar to the resulting cluster color bar displayed in the sample program in this site: OpenCV and Python K-Means Color Clustering

Here is the snippet code I'm currently using for the program:

Mat ocv = resulthsv; //resulthsv is the image made after converting to hsv and masking.

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

// 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 = labels.at<int>(i);
    p[i] = centers.at<Vec3f>(center_id);
}

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

imshow(windowName, resulthsv); 
imwrite("t1.png", resulthsv);
imshow("Dominant color", ocv);
waitKey(0); 
destroyWindow(windowName); 

return 0;

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image. (Akin similar to the resulting cluster color bar displayed in the sample program in this site: OpenCV and Python K-Means Color Clustering

Here is the snippet code I'm currently using for the program:

Mat ocv = resulthsv; //resulthsv is the image made after converting to hsv and masking.

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

// 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 = labels.at<int>(i);
    p[i] = centers.at<Vec3f>(center_id);
}

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

imshow(windowName, resulthsv); 
imwrite("t1.png", resulthsv);
imshow("Dominant color", ocv);
waitKey(0); 
destroyWindow(windowName); 

return 0;

Here is the initial image (after masking):

image description

And here is the image after K-means:

image description

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image. (Akin similar to the resulting cluster color bar displayed in the sample program in this site: OpenCV and Python K-Means Color Clustering

Here is the snippet code I'm currently using for the program:

Mat ocv = resulthsv; //resulthsv is the image made after converting to hsv and masking.

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

// 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 = labels.at<int>(i);
    p[i] = centers.at<Vec3f>(center_id);
}

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

imshow(windowName, resulthsv); 
imwrite("t1.png", resulthsv);
imshow("Dominant color", ocv);
waitKey(0); 
destroyWindow(windowName); 

return 0;

Here is the initial image (after masking):

image description

And here is the image after K-means:

image description

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image. (Akin similar to the resulting cluster color bar displayed in the sample program in this site: OpenCV and Python K-Means Color Clustering

Here is the snippet code I'm currently using for the program:

Mat ocv = resulthsv; //resulthsv is the image made after converting to hsv and masking.

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

// 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 = labels.at<int>(i);
    p[i] = centers.at<Vec3f>(center_id);
}

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

imshow(windowName, resulthsv); 
imwrite("t1.png", resulthsv);
imshow("Dominant color", ocv);
waitKey(0); 
destroyWindow(windowName); 

return 0;

Here is the initial image (after masking):

image description

And here is the image after K-means:

image description

Finding Dominant colors in an image in c++

I have been scouring the net for quite some time in regards to finding the same answer, but in c++. So far, nothing yielded any usable sample codes or guides in regards to this topic. And the only thing I have found for this subject is through an old forum answer just nearly 2 years ago (See this for reference), but the result from that was still erroneous.

With that being said, I have to ask.

How to find dominant colors in OpenCV in C++?

Edit: I have managed to make the program from the reference work, but all I'm left is a simplified image. It may make things easier, but I'm still looking for a way to find the dominant color in the image. (Akin similar to the resulting cluster color bar displayed in the sample program in this site: OpenCV and Python K-Means Color Clustering

Here is the snippet code I'm currently using for the program:

Mat ocv = resulthsv; //resulthsv is the image made after converting to hsv and masking.

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

// 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 = labels.at<int>(i);
    p[i] = centers.at<Vec3f>(center_id);
}

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

imshow(windowName, resulthsv); 
imwrite("t1.png", resulthsv);
imshow("Dominant color", ocv);
waitKey(0); 
destroyWindow(windowName); 

return 0;

Here is the initial image (after masking):

image description

And here is the image after K-means:

image description