void glintFinder::smoothBlob(ofxCvBlob& blob, int smoothingSize, float smoothingAmount) { vector<ofPoint> ref = blob.pts; vector<ofPoint>& cur = blob.pts; int n = cur.size(); for(int i = 0; i < n; i++) { cur[i].set(0, 0); float weightSum = 0; for(int j = 1; j <= smoothingSize; j++) { int leftPosition = (n + i - j) % n; int rightPosition = (i + j) % n; ofPoint& left = ref[leftPosition]; ofPoint& right = ref[rightPosition]; float weight = ofMap(j, 0, smoothingSize, 1, smoothingAmount); weightSum += weight; cur[i] += (left + right) * weight; } ofPoint& center = ref[i]; cur[i] += center; cur[i] /= 1 + 2 * weightSum; } recomputeCentroid(blob); }
void selectSeeds(cluster* clusters, entryList &l, int k) { int index = 0; for(int i=0; i<l.size(); i++) { index = i % k; cluster &cl = clusters[index]; article_entry* e = l.get(i); cl.members.add(e); } printf("Computing centroids "); for(int i=0; i<k; i++) { printf("%d ", i); recomputeCentroid(clusters[i]); } printf("\n"); }
void clusterize(int k, entryList &l) { printf("Clusterizing in %d clusters\n", k); cluster *clusters = new cluster[k]; selectSeeds(clusters, l, k); double RSS = printRSS(clusters, k); double prevRSS = 1e+20; int iterations = 0; while(iterations < MAX_ITERATIONS && (prevRSS - RSS) > THRESHOLD) { iterations++; printf("Iteration %d\n", iterations); // Clear cluster members. for(int i=0; i<k; i++) clusters[i].members.clear(); // Find the best cluster for each entry // and add as a member for(int i=0; i<l.size(); i++) { article_entry* entry = l.get(i); cluster& cl = findBestCluster(clusters, k, entry->v); cl.members.add(entry); } // Recompute centroids printf("Computing centroids "); for(int i=0; i<k; i++) { printf("%d ", i); recomputeCentroid(clusters[i]); } printf("\n"); prevRSS = RSS; RSS = printRSS(clusters, k); } delete[] clusters; }