MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / kmeans_demo

Function kmeans_demo

examples/machine_learning/kmeans.cpp:112–164  ·  view source on GitHub ↗

K-Means image recoloring. Shifts the hues of an image to the k mean hues.

Source from the content-addressed store, hash-verified

110// K-Means image recoloring.
111// Shifts the hues of an image to the k mean hues.
112int kmeans_demo(int k, bool console) {
113 printf("** ArrayFire K-Means Demo (k = %d) **\n\n", k);
114
115 array img =
116 loadImage(ASSETS_DIR "/examples/images/spider.jpg") / 255; // [0-255]
117
118 int w = img.dims(0), h = img.dims(1), c = img.dims(2);
119 array vec = moddims(img, w * h, 1, c);
120
121 array means_full, clusters_full;
122 kmeans(means_full, clusters_full, vec, k);
123
124 array means_half, clusters_half;
125 kmeans(means_half, clusters_half, vec, k / 2);
126
127 array means_dbl, clusters_dbl;
128 kmeans(means_dbl, clusters_dbl, vec, k * 2);
129
130 if (!console) {
131 array out_full =
132 moddims(means_full(span, clusters_full, span), img.dims());
133 array out_half =
134 moddims(means_half(span, clusters_half, span), img.dims());
135 array out_dbl =
136 moddims(means_dbl(span, clusters_dbl, span), img.dims());
137
138 af::Window wnd(800, 800, "ArrayFire K-Means Demo");
139 wnd.grid(2, 2);
140 std::stringstream out_full_caption, out_half_caption, out_dbl_caption;
141 out_full_caption << "k = " << k;
142 out_half_caption << "k = " << k / 2;
143 out_dbl_caption << "k = " << k * 2;
144 while (!wnd.close()) {
145 wnd(0, 0).image(img, "Input Image");
146 wnd(0, 1).image(out_full, out_full_caption.str().c_str());
147 wnd(1, 0).image(out_half, out_half_caption.str().c_str());
148 wnd(1, 1).image(out_dbl, out_dbl_caption.str().c_str());
149 wnd.show();
150 }
151 } else {
152 means_full =
153 moddims(means_full, means_full.dims(1), means_full.dims(2));
154 means_half =
155 moddims(means_half, means_half.dims(1), means_half.dims(2));
156 means_dbl = moddims(means_dbl, means_dbl.dims(1), means_dbl.dims(2));
157
158 af_print(means_full);
159 af_print(means_half);
160 af_print(means_dbl);
161 }
162
163 return 0;
164}
165
166int main(int argc, char **argv) {
167 int device = argc > 1 ? atoi(argv[1]) : 0;

Callers 1

mainFunction · 0.85

Calls 8

loadImageFunction · 0.85
moddimsFunction · 0.85
kmeansFunction · 0.85
gridMethod · 0.80
closeMethod · 0.80
imageMethod · 0.80
showMethod · 0.80
dimsMethod · 0.45

Tested by

no test coverage detected