示例#1
0
VIOAPI  VIO_Status  input_volume(
    VIO_STR              filename,
    int                  n_dimensions,
    VIO_STR              dim_names[],
    nc_type              volume_nc_data_type,
    VIO_BOOL             volume_signed_flag,
    VIO_Real             volume_voxel_min,
    VIO_Real             volume_voxel_max,
    VIO_BOOL             create_volume_flag,
    VIO_Volume           *volume,
    minc_input_options   *options )
{
    VIO_Status           status;
    VIO_Real             amount_done;
    volume_input_struct  input_info;
    VIO_progress_struct  progress;
    static const int     FACTOR = 1000;
    VIO_Real             volume_min=0.0,volume_max=0.0;

    status = start_volume_input( filename, n_dimensions, dim_names,
                                 volume_nc_data_type, volume_signed_flag,
                                 volume_voxel_min, volume_voxel_max,
                                 create_volume_flag, volume, options,
                                 &input_info );

    if( status == VIO_OK )
    {
        initialize_progress_report( &progress, FALSE, FACTOR, "Reading Volume");

        while( input_more_of_volume( *volume, &input_info, &amount_done ) )
        {
            update_progress_report( &progress,
                                    VIO_ROUND( (VIO_Real) FACTOR * amount_done));
        }

        if (amount_done < 1.0)
        {
          status = VIO_ERROR;
        }

        terminate_progress_report( &progress );

        delete_volume_input( &input_info );

        if( !volume_is_alloced( *volume ) ) {
          delete_volume( *volume );
          *volume = NULL;
          status = VIO_ERROR;
        }
    }
    if (status == VIO_OK)
    {
      get_volume_voxel_range( *volume, &volume_min, &volume_max );
    }
    return( status );
}
示例#2
0
int main(int argc, char *argv[])
{
   Volume volume;
   volume_input_struct input_info;
   char *filename;
   double v0, v1, v2, wx, wy, wz;
   static char *dim_names[] =
      {ANY_SPATIAL_DIMENSION, ANY_SPATIAL_DIMENSION, ANY_SPATIAL_DIMENSION};

   milog_init(argv[0]);

   /* Check arguments */
   if (ParseArgv(&argc, argv, argTable, 0) || argc != 5) {
      (void) fprintf(stderr, 
      "Usage: %s <image file> <voxel index 1 (slowest)> <index 2> <index 3>\n",
                     argv[0]);
      exit(EXIT_FAILURE);
   }
   filename = argv[1];
   v0 = atof(argv[2]);
   v1 = atof(argv[3]);
   v2 = atof(argv[4]);

   /* Open the image file */
   set_print_function(print_to_stderr);
   if (start_volume_input(filename, 3, dim_names, MI_ORIGINAL_TYPE, TRUE,
                          0.0, 0.0, TRUE, &volume, NULL, &input_info) != OK) {
      (void) fprintf(stderr, "Error opening file %s for input.\n",
                     filename);
      exit(EXIT_FAILURE);
   }

   /* Convert the voxel to world coordinates */
   convert_3D_voxel_to_world(volume, v0, v1, v2, &wx, &wy, &wz);

   /* Write out the result */
   (void) printf("%.20g %.20g %.20g\n", wx, wy, wz);

   exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
   VIO_General_transform 
     transform, 
     *grid_transform_ptr, 
     forward_transform;
   VIO_Volume 
     target_vol,
     volume;
   volume_input_struct 
     input_info;
   VIO_Real
     voxel[VIO_MAX_DIMENSIONS],
     steps[VIO_MAX_DIMENSIONS],
     start[VIO_N_DIMENSIONS],
     target_steps[VIO_MAX_DIMENSIONS],
     wx,wy,wz, inv_x, inv_y, inv_z,
     def_values[VIO_MAX_DIMENSIONS];

   static int 
     clobber_flag = FALSE,
     verbose      = TRUE,
     debug        = FALSE;
   static char  
     *target_file;

   int 
     parse_flag,
     prog_count,
     sizes[VIO_MAX_DIMENSIONS],
     target_sizes[VIO_MAX_DIMENSIONS],
     xyzv[VIO_MAX_DIMENSIONS],
     target_xyzv[VIO_MAX_DIMENSIONS],
     index[VIO_MAX_DIMENSIONS],
     i,
     trans_count;
   VIO_progress_struct
     progress;


   static ArgvInfo argTable[] = {
     {"-like",       ARGV_STRING,   (char *) 0,     (char *) &target_file,
        "Specify target volume sampling information."},
     {"-no_clobber", ARGV_CONSTANT, (char *) FALSE, (char *) &clobber_flag,
        "Do not overwrite output file (default)."},
     {"-clobber",    ARGV_CONSTANT, (char *) TRUE,  (char *) &clobber_flag,
        "Overwrite output file."},
     {"-verbose",    ARGV_CONSTANT, (char *) TRUE,     (char *) &verbose,
        "Write messages indicating progress (default)"},
     {"-quiet",      ARGV_CONSTANT, (char *) FALSE,    (char *) &verbose,
        "Do not write log messages"},
     {"-debug",      ARGV_CONSTANT, (char *) TRUE,  (char *) &debug,
        "Print out debug info."},
     {NULL, ARGV_END, NULL, NULL, NULL}
   };


   prog_name = argv[0];
   target_file = malloc(1024);
   strcpy(target_file,"");

   /* Call ParseArgv to interpret all command line args (returns TRUE if error) */
   parse_flag = ParseArgv(&argc, argv, argTable, 0);

   /* Check remaining arguments */
   if (parse_flag || argc != 3) print_usage_and_exit(prog_name);

   /* Read in file that has a def field to invert */
   if (input_transform_file(argv[1], &transform) != OK) {
      (void) fprintf(stderr, "%s: Error reading transform file %s\n",
                     argv[0], argv[1]);
      exit(EXIT_FAILURE);
   }


   for(trans_count=0; trans_count<get_n_concated_transforms(&transform); trans_count++ ) {

     grid_transform_ptr = get_nth_general_transform(&transform, trans_count );
     
     if (grid_transform_ptr->type == GRID_TRANSFORM) {

       copy_general_transform(grid_transform_ptr,
                              &forward_transform);

       /* 
          this is the call that should be made
          with the latest version of internal_libvolume_io
        
          invert_general_transform(&forward_transform); */

       forward_transform.inverse_flag = !(forward_transform.inverse_flag);

       volume = grid_transform_ptr->displacement_volume;

       if (strlen(target_file)!=0) {
         if (debug) print ("Def field will be resampled like %s\n",target_file);
         
         if (!file_exists( target_file ) ) {
           (void) fprintf(stderr, "%s: Target file '%s' does not exist\n",
                          prog_name,target_file);
           exit(EXIT_FAILURE);
         }

         start_volume_input(target_file, 3, (char **)NULL, 
                            NC_UNSPECIFIED, FALSE, 0.0, 0.0,
                            TRUE, &target_vol, 
                            (minc_input_options *)NULL,
                            &input_info);
         get_volume_XYZV_indices(volume, xyzv);
         get_volume_separations (volume, steps);
         get_volume_sizes       (volume, sizes);

         get_volume_XYZV_indices(target_vol, target_xyzv);
         get_volume_separations (target_vol, target_steps);
         get_volume_sizes       (target_vol, target_sizes);

         for(i=0; i<VIO_MAX_DIMENSIONS; i++) {
           index[i] = 0;
           voxel[i] = 0.0;
         }
         convert_voxel_to_world(target_vol, voxel, &start[VIO_X], &start[VIO_Y], &start[VIO_Z]);

         if( volume != (void *) NULL ){
           free_volume_data( volume );
         }

         for(i=VIO_X; i<=VIO_Z; i++) {
           steps[ xyzv[i] ] = target_steps[ target_xyzv[i] ] ;
           sizes[ xyzv[i] ] = target_sizes[ target_xyzv[i] ] ;
         }
         set_volume_separations(volume, steps);
         set_volume_sizes(      volume, sizes);
         set_volume_starts(volume, start);
         alloc_volume_data( volume );
       }

       get_volume_sizes(volume, sizes);
       get_volume_XYZV_indices(volume,xyzv);

       for(i=0; i<VIO_MAX_DIMENSIONS; i++){
         index[i] = 0;
       }

       if (verbose){
        initialize_progress_report(&progress, FALSE, 
                                   sizes[xyzv[VIO_X]]*sizes[xyzv[VIO_Y]]*sizes[xyzv[VIO_Z]]+1,
                                   "Inverting def field");
       }
       prog_count = 0;

       for(index[xyzv[VIO_X]]=0; index[xyzv[VIO_X]]<sizes[xyzv[VIO_X]]; index[xyzv[VIO_X]]++)
         for(index[xyzv[VIO_Y]]=0; index[xyzv[VIO_Y]]<sizes[xyzv[VIO_Y]]; index[xyzv[VIO_Y]]++)
           for(index[xyzv[VIO_Z]]=0; index[xyzv[VIO_Z]]<sizes[xyzv[VIO_Z]]; index[xyzv[VIO_Z]]++) {
             
             index[ xyzv[VIO_Z+1] ] = 0;
             for(i=0; i<VIO_MAX_DIMENSIONS; i++) voxel[i] = (VIO_Real)index[i];
       
             convert_voxel_to_world(volume, voxel, &wx, &wy, &wz);
             
             if (sizes[ xyzv[VIO_Z] ] ==1)
                general_inverse_transform_point_in_trans_plane(&forward_transform,
                                            wx, wy, wz,
                                            &inv_x, &inv_y, &inv_z);
             else
               grid_inverse_transform_point(&forward_transform,
                                            wx, wy, wz,
                                            &inv_x, &inv_y, &inv_z);
             def_values[VIO_X] = inv_x - wx;
             def_values[VIO_Y] = inv_y - wy;
             def_values[VIO_Z] = inv_z - wz;

             for(index[xyzv[VIO_Z+1]]=0; index[xyzv[VIO_Z+1]]<3; index[xyzv[VIO_Z+1]]++)
               set_volume_real_value(volume,
                                     index[0],index[1],index[2],index[3],index[4],
                                     def_values[ index[ xyzv[VIO_Z+1] ]]);

             prog_count++;
             if (verbose)
               update_progress_report(&progress, prog_count);
           }
       
       if (verbose)
         terminate_progress_report(&progress);

       delete_general_transform(&forward_transform);

       grid_transform_ptr->inverse_flag = !(grid_transform_ptr->inverse_flag);
       
     }

   }
   

   /* Write out the transform */
   if (output_transform_file(argv[2], NULL, &transform) != OK) {
      (void) fprintf(stderr, "%s: Error writing transform file %s\n",
                     argv[0], argv[2]);
      exit(EXIT_FAILURE);
   }

   exit(EXIT_SUCCESS);
}
示例#4
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : average_smoothness
@INPUT      : tmp_lambda_file - tmp file with unaveraged smoothness values
@OUTPUT     : lambda_file - file where final values are to be placed
@RETURNS    : nothing
@DESCRIPTION: routine to create buffers for calculating smoothness of field
@CREATED    : Nov. 3, 1997, J. Taylor
@MODIFIED   : 
---------------------------------------------------------------------------- */
int average_smoothness(char *tmp_lambda_file, char *lambda_file, double scalar)
{
   Volume volume;
   nc_type in_nc_type;
   int in_signed_flag = FALSE;
   volume_input_struct volume_input;
   int v1, v2, v3;
   int num_dim;
   int sizes[MAX_DIMENSIONS];
   double avg, values[8];
   int i;

   if(start_volume_input(tmp_lambda_file, 3, NULL, NC_UNSPECIFIED, FALSE,
                         0.0, 0.0, TRUE, &volume,
                         (minc_input_options *) NULL, &volume_input) != OK){
      fprintf(stderr,"\nError opening %s.\n", tmp_lambda_file); 
      exit(EXIT_FAILURE);
   }

   in_nc_type = get_volume_nc_data_type(volume, &in_signed_flag);
   get_volume_sizes(volume, sizes);
   num_dim = get_volume_n_dimensions(volume);

   cancel_volume_input(volume, &volume_input);

   if( input_volume(tmp_lambda_file, 3, NULL, NC_FLOAT, FALSE, 0.0, 0.0,
                    TRUE, &volume, (minc_input_options *) NULL) != OK) {
      (void) fprintf(stderr, "Error reading file \"%s\"\n", tmp_lambda_file);
      return FALSE;
   }

   for(v1=0; v1<sizes[0]; v1++) {
      for(v2=0; v2<sizes[1]; v2++) {
         for(v3=0; v3<sizes[2]; v3++) {

            avg = 0.0;

            if((v1<sizes[0]-1) && (v2<sizes[1]-1) && (v3<sizes[2])-1) {

               values[0] = get_volume_real_value(volume, v1+1, v2, v3, 0, 0); 
               values[1] = get_volume_real_value(volume, v1, v2+1, v3, 0, 0);
               values[2] = get_volume_real_value(volume, v1, v2, v3+1, 0, 0);
               values[3] = get_volume_real_value(volume,v1+1, v2+1, v3, 0, 0);
               values[4] = get_volume_real_value(volume, v1+1,v2, v3+1, 0, 0);
               values[5] = get_volume_real_value(volume, v1, v2+1,v3+1, 0, 0);
               values[6] = get_volume_real_value(volume, v1, v2, v3, 0, 0);
               values[7] = get_volume_real_value(volume, v1+1, v2+1,v3+1,0,0);
            
               for(i=0; i<8; i++) {
                  if(values[i] != INVALID_DATA)
                     avg += values[i];
                  else {
                     avg = INVALID_DATA;
                     i = 8;
                  }
               }
            }
            else
               avg = INVALID_DATA;

            if (avg == 0.0)
               avg = INVALID_DATA;
            if (avg != INVALID_DATA)
                avg = scalar * 8.0 / pow(avg, (1.0 / num_dim));

            set_volume_real_value(volume, v1, v2, v3, 0, 0, avg);

         }
      }
   }

   if( output_modified_volume(lambda_file, in_nc_type, in_signed_flag,
                     0.0, 0.0, volume, tmp_lambda_file, NULL,
                     (minc_output_options *) NULL) != OK) {
      (void) fprintf(stderr, "Error writing file \"%s\"\n", lambda_file);
      return FALSE;
   }

   return TRUE;
}