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):
And here is the image after K-means:
please edit your question, and show us !
I have edited the question.
^^ yea, cute, thanks ;)
can it be that the code is doing "too much" (and that you don't understand, what it does ?)
did you want a visualization of the kmeans colors, not replace them in the image ? (again, once you have those, what do you need to do ?)
Yeah, that seems to be the case. And I'm just looking for just the 3 most dominant colors in the image, so hopefully it would help. Displaying the most dominant color shown followed by the second and third dominant color.
Yep.
Like I said, I just needed to display the first photo, then display the next window that would display the most dominant color in a box, followed by the second and third dominant color (or whichever way the output can be displayed)
ok, doable ;) and then ? that's not where your program ends, no ?
After the display of the 3 most dominant colors in the window, that's about it. probably save those colors as images? (that might have been too much to ask), but the main thing is just that. I'm creating a somewhat complex program, and this is just one module of the whole program.