This applet shows the processing.js implementation of the kmeans-colordisc tool presented previously.
| | |
run : start distribution of cluster centers and draw disc diagram
next : load next image
less : remove one cluster
more : add a new cluster
And here is the code for it:
download sketch.pde
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
PImage actp; PImage[] pictures; ArrayList allPix; ArrayList kmeans; int numpxs; int index=3; color bg; boolean run = false; void setup() { size(640, 360); colorMode(HSB, 255); bg = color(0, 255, 255); pictures = new PImage[4]; String path = "https://www.mathiasbernhard.ch/diary/wp-content/uploads/2012/pjs/"; pictures[0] = loadImage(path+"01.jpg"); pictures[1] = loadImage(path+"02.png"); pictures[2] = loadImage(path+"03.png"); pictures[3] = loadImage(path+"04.jpg"); actp = pictures[3]; allPix = new ArrayList(); next(); kmeans = new ArrayList(); for (int i=0; i<5; i++) { Zenter z = new Zenter(); kmeans.add(z); } } void draw() { if (run) { assignToZenters(); relocateZenters(); } background(0); if (actp!=null) image(actp, 0, 0); //disc pushMatrix(); translate(488, 180); noStroke(); float ac=0; float ad; if (kmeans!=null) { for (int j=0; j<kmeans.size(); j++) { Zenter z = kmeans.get(j); if (z!=null) { ad = (float)z.num/numpxs * TWO_PI; fill(z.x, z.y, z.z); arc(0, 0, 200, 200, ac, ac+ad); ac+=ad; } } } popMatrix(); } void loadPic(PImage p) { allPix.clear(); if (p==null) return; for (int i=0; i<p.pixels.length; i=i+4) { color c = p.pixels[i]; if (saturation(c)>5 && brightness(c)5) { Pixel f = new Pixel(c); allPix.add(f); } } numpxs = allPix.size(); } void assignToZenters() { if (kmeans==null) return; for (int j=0; j<kmeans.size(); j++) { Zenter z = kmeans.get(j); if (z!=null) z.members.clear(); } if (allPix==null) return; for (int i=0; i<allPix.size(); i++) { Pixel f = allPix.get(i); if (f!=null) f.assignToZenter(kmeans); Zenter z = f.getZ(); if (z!=null) z.members.add(f); } } void relocateZenters() { if (kmeans==null) return; for (int j=0; j<kmeans.size(); j++) { Zenter z = kmeans.get(j); if (z!=null) { z.relocate(); if (z.num<numpxs*0.01) { kmeans.remove(z); Zenter nz = new Zenter(); kmeans.add(z); } } } } void keyPressed() { switch (key) { case 'n': index = (index+1) % pictures.length; actp = pictures[index]; loadPic(actp); break; case 'm': Zenter z = new Zenter(); kmeans.add(z); break; case 'l': if (kmeans.size()>1) { int n = floor(random(kmeans.size())); kmeans.remove(n); } break; case 'r': run = !run; break; } } void go() { run = !run; } void next() { index = (index+1) % pictures.length; actp = pictures[index]; loadPic(actp); } void less() { if (kmeans.size()>1) { int n = floor(random(kmeans.size())); kmeans.remove(n); } } void more() { Zenter z = new Zenter(); kmeans.add(z); } void load(String path) { if (path == null) return; PImage p = loadImage(path); if (actp!=null) { loadPic(p); actp = p; } } class Pixel { float a, b, c; Zenter zen; Pixel(color cl) { this.a = hue(cl); this.b = saturation(cl); this.c = brightness(cl); this.zen = null; } Zenter getZ() { return this.zen; } void assignToZenter(ArrayList centers) { float mndst = 9999999; for (int j=0; j<centers.size(); j++) { Zenter z = centers.get(j); //wrap around hue float dh = min(abs(this.a-z.x), abs(255-max(this.a, z.x)+min(this.a, z.x))); float d = sq(dh)+sq(this.b-z.y)+sq(this.c-z.z); if (d<mndst) { mndst = d; this.zen = z; } } } } class Zenter { float x, y, z; ArrayList members; int num; Zenter() { this.x = random(55, 200); this.y = random(55, 200); this.z = random(55, 200); this.members = new ArrayList(); this.num = 0; } void relocate() { float suma = 0; float sumb = 0; float sumc = 0; int n = this.members.size(); for (int i=0; i<n; i++) { Pixel f = this.members.get(i); if (f.a<127) suma += f.a; else suma += f.a-255; sumb += f.b; sumc += f.c; } this.x = suma/n; if (this.x<0) this.x +=255; this.y = sumb/n; this.z = sumc/n; this.num = n; } } |
döid
geile siech!