📄 classify.cc
字号:
for_less( i, 0, num_classes ) { if ( create_fuzzy_volume[i] != 0 ) { if (verbose) fprintf(stdout,"Writing fuzzy volume %s to file ...\n", fuzzy_filename[i]); status = output_modified_volume(fuzzy_filename[i], fuzzy_type, FALSE, fuzzy_voxel_min, fuzzy_voxel_max, fuzzy_volume[i], input_filename[0], history, (minc_output_options *) NULL ) ; /* now delete the fuzzy volumes so that cache is flushed - as per David */ delete_volume( fuzzy_volume[i] ); if ( status != OK ) exit(EXIT_FAILURE); } /* if ( create_fuzzy_volume ) */ } /* for_less (i...) */ } /* write_fuzzy_volumes *//* ----------------------------- MNI Header -----------------------------------@NAME : set_classifier_functions@INPUT : @OUTPUT : @RETURNS : @DESCRIPTION: set the various classifier functions into a generic format functions@METHOD : @GLOBALS : @CALLS : @CREATED : Sep. 14, 1995 ( Vasco KOLLOKIAN )@MODIFIED : ---------------------------------------------------------------------------- */void set_classifier_functions( int classifier_index ){ switch ( classifier_index ) { case MD: supervised = TRUE; fuzzy_available = TRUE; use_apriori = FALSE; init_training = minimum_distance_init_training; load_training = minimum_distance_load_training; save_training = minimum_distance_save_training; train = minimum_distance_train_samples; classify = minimum_distance_classify_sample; break; case KNN: supervised = TRUE; fuzzy_available = TRUE; use_apriori = FALSE; init_training = knn_init_training; load_training = knn_load_training; save_training = knn_save_training; train = knn_train_samples; classify = knn_classify_sample; break; case ANN: supervised = TRUE; fuzzy_available = TRUE; use_apriori = FALSE; init_training = ann_init_training; load_training = ann_load_training; save_training = ann_save_training; train = ann_train_samples; classify = ann_classify_sample; break;// case C45:// supervised = TRUE;// fuzzy_available = TRUE;// use_apriori = FALSE;// init_training = c4_5_init_training;// load_training = c4_5_load_training;// save_training = c4_5_save_training; // train = c4_5_train_samples;// classify = c4_5_classify_sample;// break; case HCM: supervised = FALSE; fuzzy_available = FALSE; use_apriori = FALSE; init_training = hcm_init_training; load_training = hcm_load_training; save_training = hcm_save_training; train = hcm_train_samples; classify = hcm_classify_sample; break; case FCM: supervised = FALSE; fuzzy_available = TRUE; use_apriori = FALSE; init_training = fcm_init_training; load_training = fcm_load_training; save_training = fcm_save_training; train = fcm_train_samples; classify = fcm_classify_sample; break; case BAY: supervised = TRUE; fuzzy_available = TRUE; use_apriori = TRUE; init_training = bayesian_init_training; load_training = bayesian_load_training; save_training = bayesian_save_training; train = bayesian_train_samples; classify = bayesian_classify_sample; break; }}/* ----------------------------- MNI Header -----------------------------------@NAME : classify_volume()@INPUT : in_volume[j]@OUTPUT : @RETURNS : @DESCRIPTION: classifies the n-dimensional feature volumes@METHOD : @GLOBALS : @CALLS : @CREATED : May 29, 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void classify_volume(void) { int j, k; /* counters for sample and volume, fuzzy vols */ int class_val; /* class of the classified sample */ Real mask_value; /* holds value of mask to consider */ /* reserve space for class probabilities and their labels */ if ( fuzzy || classifier == FCM ) { ALLOC( class_probs, num_classes ); ALLOC( class_labels, num_classes ); initialize_fuzzy_volumes(); } if (verbose) (void) fprintf(stdout, "Classifying volume... \n"); /* take the size of volume 0, since they should all be the same */ for_less (v1_ptr, 0, first_volume_sizes[0]) { if ( verbose ) write(2,"*",1); for_less (v2_ptr, 0, first_volume_sizes[1]) { if ( debug > 10 ) write(2,"-",1); for_less (v3_ptr, 0, first_volume_sizes[2]) { if ( debug >= 20 ) write(2,"+",1); /* extract the feature vector from volumes */ for_less(j, 0, num_features) { /*GET_VALUE_3D(feature_vector[j],in_volume[j],v1_ptr,v2_ptr,v3_ptr);*/ feature_vector[j] = get_volume_real_value(in_volume[j], v1_ptr, v2_ptr, v3_ptr, 0,0); } /* and if apriori switch set, then extract apriori vector values from the probability maps, which in this case are residing in train_volume[j] */ if ( apriori ) { if ( debug > 31 ) write(2,"%",1); for_less(j, 0, num_classes) apriori_vector[j] = get_volume_real_value(train_volume[j], v1_ptr, v2_ptr, v3_ptr, 0,0); } /* if a mask is chosen, ( by having a non-NULL mask_filename ) and mask value is >= some specified value, classify only that voxel, otherwise set to to user defines class value, default being zero */ if ( mask_filename ) { /* make sure the map is previously loaded, and has the same size */ /*GET_VALUE_3D( mask_value, mask_volume, v1_ptr, v2_ptr, v3_ptr);*/ mask_value = get_volume_real_value(mask_volume, v1_ptr, v2_ptr, v3_ptr, 0,0); if ( mask_value < user_mask_value ) { class_val = user_mask_class; goto WRITE_VOXEL; } } /********************* S T A R T ***************************/ /* knn has to train on each sample before classifying */ if (classifier == KNN) train(); if ( fuzzy || classifier == FCM) { classify(&class_val, class_probs, class_labels); /* for each fuzzy_volume[k], get the voxel value, and write it */ for_less ( k, 0, num_classes ) { if ( debug > 31 ) fprintf(stdout, "%f, ", class_probs[k] ) ; /* debug value of 100 is to test the volume cache - is temp */ if ( create_fuzzy_volume[k] != 0 && debug < 100) { /*SET_VOXEL_3D(fuzzy_volume[k],v1_ptr,v2_ptr,v3_ptr,voxel_prob );*/ set_volume_real_value(fuzzy_volume[k], v1_ptr, v2_ptr, v3_ptr, 0,0, class_probs[k] ); } /* if */ } /* for_less ( k, 0, num_classes )*/ if ( debug > 31 ) fprintf(stdout, "\n "); } /* if (fuzzy || classifier == FCM) */ else /* crisp */ classify(&class_val, 0, 0); /********************** E N D ******************************/ WRITE_VOXEL: /*SET_VOXEL_3D(classified_volume,v1_ptr,v2_ptr,v3_ptr,class_val);*/ set_volume_real_value(classified_volume, v1_ptr, v2_ptr, v3_ptr, 0,0, class_val ); } /* for_less v3_ptr */ } /* for_less v2_ptr */ } /* for_less v1_ptr */}/* ----------------------------- MNI Header -----------------------------------@NAME : cleanup_memory@INPUT : @OUTPUT : @RETURNS : @DESCRIPTION: cleans memory to test for leaks in debuging @METHOD : @GLOBALS : @CALLS : @CREATED : Nov 5, 1995 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */void cleanup_memory(void) { int loop_idx; /* get rid of input filenames */ for_less( loop_idx, 0, num_features ) FREE( input_filename[loop_idx]); /* get rid of class names */ for_less( loop_idx, 0, num_classes ) FREE( class_name[loop_idx] ); FREE( class_name ); FREE( feature_vector ); FREE2D( first_volume_sizes ); if ( supervised && tagfile_filename) { FREE2D( feature_matrix ); FREE( class_column ); } FREE( class_count ); /* now clean the feature volumes */ for_less( loop_idx, 0, num_features ) { delete_volume( in_volume[loop_idx] ); } FREE( in_volume ); /* now clean the mask volume */ if ( mask_filename ) { delete_volume( mask_volume ); } /* now free tag points */ free_tag_points(n_tag_volumes, num_samples, tags, NULL, NULL, NULL, NULL, labels ); /* dump unreleased memory to a file */ if ( debug >= 10 ) { char mem_filename[] = "./memory.debug"; fprintf( stdout, "Writing memory stuff to %s\n", mem_filename); output_alloc_to_file(mem_filename); }} /* cleanup_memory(void) *//* ----------------------------- MNI Header -----------------------------------@NAME : volume_size_is_ok@INPUT : @OUTPUT : @RETURNS : @DESCRIPTION: verifies that volume sizes are OK@METHOD : @GLOBALS : @CALLS : @CREATED : Feb 10, 1996 (Vasco KOLLOKIAN)@MODIFIED : ---------------------------------------------------------------------------- */int volume_size_is_ok( Volume loaded_volume) { int *loaded_volume_sizes; int loaded_volume_num_dims; STRING *loaded_volume_dim_names; /* allocate memory for first volume sizes */ ALLOC(loaded_volume_sizes, MAX_DIMENSIONS); /* get dim size, nums, order */ get_volume_sizes(loaded_volume, loaded_volume_sizes); loaded_volume_num_dims = get_volume_n_dimensions(loaded_volume); loaded_volume_dim_names = get_volume_dimension_names(loaded_volume); if ( debug > 2 ) { int k; /* local counter */ fprintf(stdout, "Vol number of dims. = %d\n", loaded_volume_num_dims); fprintf(stdout, "Vol dimension names = "); for_less ( k, 0, loaded_volume_num_dims ) fprintf(stdout, "%s ", loaded_volume_dim_names[k]); fprintf(stdout, "\n"); } /* all the volume dimensions should be the same as the first volume */ /* check for number of dimensions mismatch */ if (loaded_volume_num_dims != first_volume_num_dims ) { (void) fprintf(stderr,"Error - Number of dimensions mismatch "); return FALSE; } /* check for volume size mismatches */ if (loaded_volume_sizes[X] != first_volume_sizes[X]) { (void) fprintf(stderr,"Error - Volume size mismatch in X dimension "); return FALSE; } if (loaded_volume_sizes[Y] != first_volume_sizes[Y]) { (void) fprintf(stderr,"Error - Volume size mismatch in Y dimension "); return FALSE; } if (loaded_volume_sizes[Z] != first_volume_sizes[Z]) { (void) fprintf(stderr,"Error - Volume size mismatch in Z dimension "); return FALSE; } /* check for dimensions order mismatch */ if ( strcmp(loaded_volume_dim_names[0], first_volume_dim_names[0]) ) { (void) fprintf(stderr,"Error - First dimension order mismatch "); return FALSE; } /* if there are more than 1 dimension - check dim order of second dim */ if ( loaded_volume_num_dims > 1) if ( strcmp(loaded_volume_dim_names[1], first_volume_dim_names[1]) ) { (void) fprintf(stderr,"Error - Second dimension order mismatch "); return FALSE; } /* if there are more than 2 dimensions - check dim order of third dim*/ if ( loaded_volume_num_dims > 2) if ( strcmp(loaded_volume_dim_names[2], first_volume_dim_names[2]) ) { (void) fprintf(stderr,"Error - Third dimension order mismatch "); return FALSE; } /* if there are more then 3 dimensions - warn and die */ if ( loaded_volume_num_dims > 3) { (void) fprintf(stderr,"Support is limited to 3 spatial dimensions "); exit(EXIT_FAILURE); } /* free the reserved memory locations */ delete_dimension_names( loaded_volume, loaded_volume_dim_names ); FREE(loaded_volume_sizes); return TRUE; } /* volume_size_is_ok */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -