I have also practiced using the kmean algorithm of opencv before, but the version is low and it is also developed based on C. In the past two days, due to the need to create a thesis, I have re-translated it and studied C ++, and found that some improvements have been made.
Kmeans
-
C ++:
Double Kmeans (Inputarray
Data, Int
K, Inputoutputarray
Bestlabels, Termcriteria
Criteria, Int
Attempts, Int
Flags, Outputarray
Centers= Noarray ()
)
- Data: input sample, the object to be classified, float type, one sample per line (one pixel per line if I want to classify the color );
- K: number of types;
-
Bestlabels:The matrix after classification. Each sample corresponds to a label of the type;
- Termcriteria
Criteria:End condition (maximum iterative algebra and ideal accuracy)
- Int
Attempts:Determine the optimal initial cluster center based on the last parameter (select the attempt initial center and select
CompactnessSmallest );
- Int
Flags:
-
Flag that can take the following values:
- Kmeans_random_centersSelect Random Initial centers in each attempt.
- Kmeans_pp_centersUse
Kmeans ++Center initialization by Arthur and vassilvitskii [arur2007].
- Kmeans_use_initial_labelsDuring the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random
Or semi-random centers. Use oneKmeans _ * _ centersFlag to specify the exact method.
Centers:Output cluster center, with one center per row (the first column is the cluster center, but there are other columns. I don't quite understand them here. Who knows? Ask for science !~~)
ComPActness:Test whether the initial center is optimal.
Code:
# Include <string> # include <iostream> # include <math. h> # include <vector> # include <map> # include "opencv/cv. H "# include" opencv/highgui. H "# include" opencv/cxcore. H "# define clusternum (6) using namespace CV; using namespace STD; string filename =" D:/demo1.jpg "; MAT clustering (mat src) {int ROW = SRC. rows; int Col = SRC. cols; unsigned long int size = row * Col; MAT clusters (size, 1, cv_32sc1); // clustering mat, save class label at every location; // convert SRC mat to sample srcpoint. mat srcpoint (size, 1, cv_32fc3); vec3f * srcpoint_p = (vec3f *) srcpoint. data; //////////////////////////////////////// //// vec3f * src_p = (vec3f *) SRC. data; unsigned long int I; for (I = 0; I <size; I ++) {* srcpoint_p = * src_p; srcpoint_p ++; src_p ++ ;} mat Center (clusternum, 1, cv_32fc3); double compactness; // compactness to measure the clustering center Dist sum by different flagcompactness = kmeans (srcpoint, clusternum, clusters, cvtermcriteria (cv_termcrit_eps + cv_termcrit_iter, 10, 0.1), clusternum, kmeans_pp_centers, center); cout <"center row:" <center. rows <"Col:" <center. cols <Endl; For (INT y = 0; y <center. rows; y ++) {vec3f * imgdata = center. PTR <vec3f> (y); For (INT x = 0; x <center. cols; X ++) {cout count; // Map <ID, num> Map <int, vec3f> AVG; // Map <ID, color> // compute average color value of one labelfor (INT y = 0; Y <row; y ++) {const vec3f * imgdata = SRC. PTR <vec3f> (y); int * idx = label. PTR <int> (y); For (INT x = 0; x <Col; X ++) {AVG [idx [x] + = imgdata [x]; count [idx [x] ++ ;}// output the average value (clustering Center) // The calculated cluster center is consistent with the first column of the center in the kmean function, // In the future, you can save the complex computation and directly use the center. // However, you still do not understand the meaning of for (I = 0; I <clusternum; I ++) {AVG [I]/= count [I]; If (avg [I]. val [0]> 0 & AVG [I]. val [1]> 0 & AVG [I]. val [2]> 0) {cout <I <":" <AVG [I]. val [0] <"" <AVG [I]. val [1] <"" <AVG [I]. val [2] <"count:" <count [I] <Endl ;}// show the clustering IMG; MAT showimg (SRC. size (), cv_32fc3); For (INT y = 0; y <row; y ++) {vec3f * imgdata = showimg. PTR <vec3f> (y); int * idx = label. PTR <int> (y); For (INT x = 0; x <Col; X ++) {int id = idx [X]; imgdata [X]. val [0] = AVG [ID]. val [0]; imgdata [X]. val [1] = AVG [ID]. val [1]; imgdata [X]. val [2] = AVG [ID]. val [2] ;}} normalize (showimg, showimg, 1, 0, cv_minmax); imshow ("show", showimg); waitkey (); return label;} int main () {mat IMG = imread (filename, 1); gaussianblur (IMG, IMG, size (3, 3), 0); IMG. convertize (IMG, cv_32fc3); MAT pixid = clustering (IMG );}