|
@ -90,16 +90,14 @@ void init(double ***vectors, char **labels, parameters *params){ |
|
|
int meanshift(double **original_points, double ***shifted_points, int deviation |
|
|
int meanshift(double **original_points, double ***shifted_points, int deviation |
|
|
, parameters *opt){ |
|
|
, parameters *opt){ |
|
|
static int iteration = 0; |
|
|
static int iteration = 0; |
|
|
|
|
|
static double **mean_shift_vector, **kernel_matrix, *denominator; |
|
|
|
|
|
|
|
|
// allocates memory and copies original points on first iteration
|
|
|
// allocates memory and copies original points on first iteration
|
|
|
if (iteration == 0 || (*shifted_points) == NULL){ |
|
|
if (iteration == 0 || (*shifted_points) == NULL){ |
|
|
iteration = 0; |
|
|
|
|
|
(*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); |
|
|
(*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); |
|
|
duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); |
|
|
duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// allocates memory for mean shift vector
|
|
|
// allocates memory for mean shift vector
|
|
|
double **mean_shift_vector; |
|
|
|
|
|
mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); |
|
|
mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); |
|
|
// initializes elements of mean_shift_vector to inf
|
|
|
// initializes elements of mean_shift_vector to inf
|
|
|
for (int i=0;i<NUMBER_OF_POINTS;i++){ |
|
|
for (int i=0;i<NUMBER_OF_POINTS;i++){ |
|
@ -109,8 +107,9 @@ int meanshift(double **original_points, double ***shifted_points, int deviation |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// allocates memory for other arrays needed
|
|
|
// allocates memory for other arrays needed
|
|
|
double **kernel_matrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS); |
|
|
kernel_matrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS); |
|
|
double *denominator = malloc(NUMBER_OF_POINTS * sizeof(double)); |
|
|
denominator = malloc(NUMBER_OF_POINTS * sizeof(double)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// finds pairwise distance matrix (inside radius)
|
|
|
// finds pairwise distance matrix (inside radius)
|
|
|
// [I, D] = rangesearch(x,y,h);
|
|
|
// [I, D] = rangesearch(x,y,h);
|
|
@ -160,17 +159,19 @@ int meanshift(double **original_points, double ***shifted_points, int deviation |
|
|
double current_norm = norm(mean_shift_vector, NUMBER_OF_POINTS, DIMENSIONS); |
|
|
double current_norm = norm(mean_shift_vector, NUMBER_OF_POINTS, DIMENSIONS); |
|
|
printf("Iteration n. %d, error %f \n", iteration, current_norm); |
|
|
printf("Iteration n. %d, error %f \n", iteration, current_norm); |
|
|
|
|
|
|
|
|
|
|
|
/** iterates until convergence **/ |
|
|
|
|
|
if (current_norm > opt->epsilon) { |
|
|
|
|
|
++iteration; |
|
|
|
|
|
meanshift(original_points, shifted_points, deviation, opt); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (iteration == 0){ |
|
|
// cleans up this iteration's allocations
|
|
|
// cleans up this iteration's allocations
|
|
|
free(mean_shift_vector[0]); |
|
|
free(mean_shift_vector[0]); |
|
|
free(mean_shift_vector); |
|
|
free(mean_shift_vector); |
|
|
free(kernel_matrix[0]); |
|
|
free(kernel_matrix[0]); |
|
|
free(kernel_matrix); |
|
|
free(kernel_matrix); |
|
|
free(denominator); |
|
|
free(denominator); |
|
|
|
|
|
|
|
|
/** iterates until convergence **/ |
|
|
|
|
|
if (current_norm > opt->epsilon) { |
|
|
|
|
|
++iteration; |
|
|
|
|
|
return meanshift(original_points, shifted_points, deviation, opt); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return iteration; |
|
|
return iteration; |
|
|