void Clusteringclass::radial_basis_kmean() { typedef dlib::matrix<float> sample_type; typedef dlib::radial_basis_kernel<sample_type> kernel_type; dlib::kcentroid< kernel_type > kc; dlib::kkmeans< kernel_type > kmean(kc); std::vector<sample_type> initial_centers; // tell the kkmeans object we made that we want to run k-means with k_clusters sets kmean.set_number_of_centers(_k_clusters); // You need to pick some initial centers for the k-means algorithm. So here // we will use the dlib::pick_initial_centers() function which tries to find // n points that are far apart (basically). pick_initial_centers(_k_clusters, initial_centers, _input_data, kmean.get_kernel()); // now run the k-means algorithm on our set of samples. kmean.train(_input_data, initial_centers); // now loop over all our samples and get their predicted class. for (unsigned long i = 0; i < _input_data.size(); ++i) { _assigned_cluster.push_back(kmean(_input_data[i])); std::cout << _input_data[i] << std::endl; std::cout << _assigned_cluster[i] << std::endl; } }
int main(int argc, char** argv){ IplImage* img = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR); IplImage* img_apply = cvCreateImage(cvSize(img->width,img->height),8,3); cvNamedWindow("cluster demo",1); unsigned char* imgData = (unsigned char*)img->imageData; unsigned char* img_apply_data = (unsigned char*)img_apply->imageData; Cluster* cluster=NULL; int cl_num = initCluster(&cluster,atoi(argv[2])); for (int i=0;i<30;i++){ kmean(imgData,img->width*img->height,cluster, cl_num); dumpCluster(cluster,5); kmeanApply(img_apply_data,imgData,img->width*img->height,cluster,cl_num); cvShowImage("cluster demo",img_apply); cvWaitKey(100); } cvWaitKey(0); return 0; }
int shotodol_kmeans_cluster(char*infile, char*outfile, int kval, int*ecode) { struct netpbm_image img, img_out; netpbm_init_img(&img); netpbm_init_img(&img_out); int i; img.filename = infile; img_out.filename = outfile; if(outfile == NULL || infile == NULL) { *ecode = 526; return -1; } if(netpbm_open_and_read(&img, ecode) == -1) { return -1; } if(img.type != NETPBM_IMAGE_TYPE_PPM) { *ecode = 512; netpbm_destroy(&img); return -1; } if(netpbm_alloc_from_src(&img_out, &img)) { goto cleanup_memory; } //printf("*** performing kmeans cluster ***\n"); Cluster* cluster=NULL; int cl_num = initCluster(&cluster, kval); int total = img.width*img.height; for (i=0;i<30;i++){ kmean(img.pixels.color, total, cluster, cl_num); dumpCluster(cluster,5); kmeanApply(img_out.pixels.color, img.pixels.color, total,cluster,cl_num); } netpbm_write(&img_out, NULL); cleanup_memory: netpbm_destroy(&img); netpbm_destroy(&img_out); return 0; }