Using k-means cluster algorithm to computing the dominant colors of given image.
-
Copy all .cpp and .h files except main.cpp to your project.
-
include mashiro.h
-
Init class mashiro with an instance of cv::Mat
Mat image = imread("/PATH/TO/AN/IMAGE"); mashiro mashiro(image);
-
Call color function
mashiro.color(3, [](cv::Mat& image, Cluster colors){ for_each(colors.cbegin(), colors.cend(), [](const MashiroColor& color){ cout<<"(" <<color[0]<<", " <<color[1]<<", " <<color[2]<<")" <<endl; }); });
The first parameter is the k in K-means cluster algorithm
The second parameter is a callback function, which gives the reference of the input image and clustered colors.
Just compile and install it with
$ make && make install
$ mashiro
Usage:
-i [image file] -c [number of color to cluster]
-h Print this help
Notice,
In Makefile, LIB and INCLUDE might need to be changed to give correct path to OpenCV.
mashiro -i cover.jpg -c 3
If everything is fine, you'll see
My blog post