📄 hcm.cc
字号:
@GLOBALS : @CALLS : @CREATED : Jun 3, 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void hcm_load_training(char *load_train_filename){ int i, j, k; Real feature_value; FILE *learn_file; /* open the input training file */ learn_file = fopen(load_train_filename, "r"); if ( learn_file == NULL) { fprintf(stderr, "Cannot open %s\n", load_train_filename); exit(EXIT_FAILURE); } /* scan for the number of features */ fscanf( learn_file, "num_of_features = %d\n", &num_features); /* scan for the max number of classes M\n*/ fscanf( learn_file, "num_of_classes = %d\n", &num_classes); if (debug) { fprintf( stdout, "num_of_features = %d\n", num_features); fprintf( stdout, "num_of_classes = %d\n", num_classes); } /* reserve area for the delta_vector */ ALLOC( delta_vector, num_classes ); /* reserve area for the fuzzy_hcm_vector */ ALLOC( fuzzy_hcm_vector, num_classes ); /* reserve area for the mean feature matrix */ ALLOC2D(mean_feature_matrix, num_classes, num_features); /* reserve area for the mean_feature_class_vector */ ALLOC(mean_feature_class_vector, num_classes); /* reserve a character pointer ( char *) for each class name */ ALLOC( class_name, num_classes ); /* reserve area for the number of class samples */ ALLOC(class_count, num_classes); /* load feature matrix from learn file */ if (verbose) fprintf(stdout, "Loading the training file...\n"); for_less( k, 0, num_classes) { fscanf(learn_file, "class = %d\n", &mean_feature_class_vector[k]); /* allocate some space for each class_name[k], 5 for ex and simulate itoa, this could be replaced by a more elegant looking code, when I have time */ ALLOC( class_name[k], 5); sprintf( class_name[k], "%d", mean_feature_class_vector[k]); for_less( j, 0, num_features) { fscanf(learn_file, "%lf\n", &feature_value); mean_feature_matrix[k][j] = feature_value ; } } if (debug > 2 ) { fprintf( stdout, "Printing mean_feature_matrix ...\n"); for_less( i, 0, num_classes) { for_less( j, 0, num_features) fprintf( stdout, "%f ", mean_feature_matrix[i][j]); fprintf( stdout, "%d\n", mean_feature_class_vector[i]); } fprintf( stdout, "-----\n"); } fclose(learn_file);}/* ----------------------------- MNI Header -----------------------------------@NAME : hcm_count_classes_and_set_names@INPUT : @OUTPUT : @RETURNS : @DESCRIPTION: sets num_classes@METHOD : @GLOBALS : @CALLS : @CREATED : Dec. 3 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void hcm_count_classes(void){ int i, j = 0; /* counter - classes */ Real voxel_class; /* class of voxel in training volume */ int bin_size = 5; /* var to hold number of bins */ long *count_bin; /* 1D array to hold the number of voxels / class */ /* Allocate some memory for the count_bin array */ ALLOC( count_bin, bin_size ) ; /* initialize the count bins */ for_less ( i, 0, bin_size ) count_bin[i] = 0; if (verbose) (void) fprintf(stdout, "Counting number of classes in training volume ... \n"); /* repeat for the total number of samples - the entire volume */ for_less( v1, 0, first_volume_sizes[0] ) for_less( v2, 0, first_volume_sizes[1] ) for_less( v3, 0, first_volume_sizes[2] ) { /* get the training_class from the first train volume */ voxel_class = get_volume_real_value(train_volume[0], v1, v2, v3, 0,0); /* this method won't handle cases where the number of classes is greater than 10, since the bin */ /* if you exceed the bin size, increase apropriately */ if ( (int) voxel_class > ( bin_size - 1 ) ) { int increment; /* see how much we have to increase count_bin */ increment = (int) voxel_class - ( bin_size - 1 ); SET_ARRAY_SIZE( count_bin, bin_size, bin_size+increment, 100 ); /* now initialize the rest of the slots in count_bin */ for_less( i, bin_size, bin_size+increment ) count_bin[i] = 0; bin_size += increment; } /* increase the bin count for that voxel */ count_bin[ (int) voxel_class ]++; } /* for_less v3 */ if ( debug >= 3 ) { for_less( i, 0, bin_size) fprintf( stdout, "count_bin[%d] = %d\n", i, count_bin[i] ); } /* now count the number of classes, that have non-zero bin numbers */ for_less( i, 0, bin_size) if ( count_bin[i] != 0) num_classes++; /* reserve a character pointer ( char *) for each class name */ ALLOC( class_name, num_classes ); /* set the value of max_class_index */ for_less( i, 0, bin_size) { if ( count_bin[i] != 0) { if ( max_class_index < i ) max_class_index = i ; } } /* free up stuff not needed */ FREE(count_bin);} /* count_number_of_classes(...) *//* ----------------------------- MNI Header -----------------------------------@NAME : hcm_classify_sample@INPUT : @OUTPUT : @RETURNS : sample class@DESCRIPTION: given a feature vector and its size, return a class@METHOD : @GLOBALS : @CALLS : @CREATED : May 29, 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void hcm_classify_sample(int *class_num, double *class_prob, int *class_labels){ int i, j, k; /* counters */ Real temp; /* temporary variable */ Real minimum; /* minimum distance class buffer */ int class_index; /* somehow load the learned classification from file and use it in this routine, but in order to do this there has to be an initialization routine that will do so and call this routine thereafter */ /* initialize delta vector */ for_less ( i, 0, num_classes ) delta_vector[i] = 0.0; /* calculate Euclidian distance */ for_less ( i, 0, num_classes ) { for_less ( j, 0, num_features) { temp = feature_vector[j] - mean_feature_matrix[i][j]; /* X - Z */ delta_vector[i] += temp * temp; /* Sig ( X - Z ) ^2 */ } } // minimum = DBL_MAX; /* the largest double value the system supports */ minimum = 10000000; class_index = 0; /* find the class with the MINIMUM DISTANCE */ for_less ( k, 0, num_classes ) if ( delta_vector[k] < minimum ) { minimum = delta_vector[k]; class_index = k; } if ( debug > 8 ) { fprintf( stdout, "index = %d, class = %d\n", class_index, mean_feature_class_vector[class_index]); } *class_num = mean_feature_class_vector[class_index];}/* ----------------------------- MNI Header -----------------------------------@NAME : hcm_save_training@INPUT : @OUTPUT : @RETURNS : @DESCRIPTION: @METHOD : @GLOBALS : @CALLS : @CREATED : Jun 3, 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void hcm_save_training(char *save_train_filename){ int i, j; /* counters */ FILE *train_file; /* save filename */ train_file = fopen(save_train_filename, "w"); /* see if the file could be opened */ if ( train_file == NULL) { printf("Cannot open %s\n", save_train_filename); exit(EXIT_FAILURE); } /* store in the file the number of features trained on */ fprintf(train_file, "num_of_features = %d\n", num_features); /* store in the file the max number of classes trained on */ fprintf(train_file, "num_of_classes = %d\n", num_classes); for_less ( i, 0, num_classes ) { fprintf(train_file, "class = %d\n", mean_feature_class_vector[i]); for_less ( j, 0, num_features ) fprintf(train_file, "%lf ", mean_feature_matrix[i][j]); fprintf(train_file, "\n"); } fclose(train_file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -