#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
return sum(
abs(data2 - means2), 2);
}
array dists = distance(data, means);
return idx;
}
sum(data * (clustersd == ii)) / (
sum(clusters == ii) + 1e-5);
}
return means;
}
int iter = 100) {
}
for (int i = 0; i < iter; i++) {
prev_clusters = curr_clusters;
curr_clusters = clusterize(data, means);
unsigned num_changed = count<unsigned>(prev_clusters != curr_clusters);
if (num_changed < (n / 1000) + 1) break;
means = new_means(data, curr_clusters, k);
}
maximum(ii) * means(
span,
span, ii) + minimum(ii);
}
clusters = prev_clusters;
}
int kmeans_demo(int k, bool console) {
printf("** ArrayFire K-Means Demo (k = %d) **\n\n", k);
loadImage(ASSETS_DIR
"/examples/images/spider.jpg") / 255;
array means_full, clusters_full;
kmeans(means_full, clusters_full, vec, k);
array means_half, clusters_half;
kmeans(means_half, clusters_half, vec, k / 2);
array means_dbl, clusters_dbl;
kmeans(means_dbl, clusters_dbl, vec, k * 2);
if (!console) {
af::Window wnd(800, 800,
"ArrayFire K-Means Demo");
wnd.grid(2, 2);
std::stringstream out_full_caption, out_half_caption, out_dbl_caption;
out_full_caption << "k = " << k;
out_half_caption << "k = " << k / 2;
out_dbl_caption << "k = " << k * 2;
while (!wnd.close()) {
wnd(0, 0).image(img, "Input Image");
wnd(0, 1).image(out_full, out_full_caption.str().c_str());
wnd(1, 0).image(out_half, out_half_caption.str().c_str());
wnd(1, 1).image(out_dbl, out_dbl_caption.str().c_str());
wnd.show();
}
} else {
means_full =
means_half =
moddims(means_half, means_half.dims(1), means_half.dims(2));
means_dbl =
moddims(means_dbl, means_dbl.dims(1), means_dbl.dims(2));
}
return 0;
}
int main(int argc, char **argv) {
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
int k = argc > 3 ? atoi(argv[3]) : 8;
try {
return kmeans_demo(k, console);
return 0;
}