I am implementing Fuzzy C-means to work with image segmentation following the given algorithm :
However when updating the centroids (this is the first thing that I do) all clusters centers converge to the very same point, here is my code :
//Initializing centroids to random points in the image, points are different from each others.
for(int j=0; j<c; j++) {
vertex pixel = vertices[rand()%n];
while(!far(pixel, assigned)) pixel = vertices[rand()%n];
add_element_list(assigned, pixel);
centers[j].r = pixel.r; centers[j].g = pixel.g; centers[j].b = pixel.b;
centers[j].a = pixel.a;
}
//Updating clusters centers
for(int j = 0; j < c; j++) {
double numeratorR = 0.0, numeratorG = 0.0, numeratorB = 0.0, numeratorA = 0.0;
double denominator = 0.0;
for(int i = 0; i < n; i++) {
double weight = fmax(pow(U[i][j], FUZZINESS), EPSILON);
numeratorR += weight * (vertices[i].r);
numeratorG += weight * (vertices[i].g);
numeratorB += weight * (vertices[i].b);
numeratorA += weight * (vertices[i].a);
denominator += weight;
}
denominator = fmax(denominator, EPSILON);
centers[j].r = numeratorR / denominator;
centers[j].g = numeratorG / denominator;
centers[j].b = numeratorB / denominator;
centers[j].a = numeratorA / denominator;
}
//Initialize each point membership to each cluster, this is done before updating the clusters
void initialize_membership_matrix(double** U, int n, int c) {
srand(time(NULL));
for(int i = 0; i<n; i++) {
double sum = 0.0;
for(int j = 0; j<c; j++) {
U[i][j] = (double) rand() / RAND_MAX;
sum += U[i][j];
}
for(int j = 0; j<c; j++) {
U[i][j] /= sum;
}
}
}
For instance all clusters centers converge to RGB(117,225,14) after the cluster update, and this point change for each image.
I though it would be a problem with my random initialization of centers clusters but even with the ‘far’ option in the code I have the same issue. Then I also though of how random I initialize membership matrix in the first place (is C random very random in this case, I have over 600k pixels of data).
Thank,