reg_tools.cpp 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  reg_tools.cpp
 *
 *
 *  Created by Marc Modat and Pankaj Daga on 24/03/2009.
 *  Copyright (c) 2009, University College London. All rights reserved.
 *  Centre for Medical Image Computing (CMIC)
 *  See the LICENSE.txt file in the nifty_reg root folder
 *
 */
#ifndef MM_TOOLS_CPP
#define MM_TOOLS_CPP

14
#include "_reg_ReadWriteImage.h"
15
#include "_reg_resampling.h"
16 17
#include "_reg_globalTransformation.h"
#include "_reg_localTransformation.h"
18 19
#include "_reg_tools.h"

Marc Modat's avatar
Marc Modat committed
20 21
#include "reg_tools.h"

22 23
int isNumeric (const char *s)
{
24
   if(s==NULL || *s=='\0' || isspace(*s))
25
      return 0;
26 27 28 29 30
   char * p;
   double a=0;
   a=a; //useless - here to avoid a warning
   a=strtod (s, &p);
   return *p == '\0';
31 32
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
typedef struct
{
   char *inputImageName;
   char *outputImageName;
   char *operationImageName;
   char *rmsImageName;
   float operationValue;
   float smoothValueX;
   float smoothValueY;
   float smoothValueZ;
   float thresholdImageValue;
} PARAM;
typedef struct
{
   bool inputImageFlag;
   bool outputImageFlag;
   bool floatFlag;
   bool downsampleFlag;
   bool rmsImageFlag;
   bool smoothSplineFlag;
   bool smoothGaussianFlag;
   bool smoothMeanFlag;
   bool binarisedImageFlag;
   bool thresholdImageFlag;
   bool nanMaskFlag;
   bool normFlag;
   int operationTypeFlag;
   bool iso;
61
   bool nosclFlag;
62
} FLAG;
63 64 65 66


void PetitUsage(char *exec)
{
67 68 69
   fprintf(stderr,"Usage:\t%s -in  <targetImageName> [OPTIONS].\n",exec);
   fprintf(stderr,"\tSee the help for more details (-h).\n");
   return;
70 71 72
}
void Usage(char *exec)
{
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   printf("Usage:\t%s -in <filename> -out <filename> [OPTIONS].\n",exec);
   printf("\t-in <filename>\tFilename of the input image image (mandatory)\n");
   printf("* * OPTIONS * *\n");
   printf("\t-out <filename>\t\tFilename out the output image [output.nii]\n");
   printf("\t-float\t\t\tThe input image is converted to float\n");
   printf("\t-down\t\t\tThe input image is downsampled 2 times\n");
   printf("\t-smoS <float> <float> <float>\tThe input image is smoothed using a cubic b-spline kernel\n");
   printf("\t-smoG <float> <float> <float>\tThe input image is smoothed using Gaussian kernel\n");
   printf("\t-add <filename/float>\tThis image (or value) is added to the input\n");
   printf("\t-sub <filename/float>\tThis image (or value) is subtracted to the input\n");
   printf("\t-mul <filename/float>\tThis image (or value) is multiplied to the input\n");
   printf("\t-div <filename/float>\tThis image (or value) is divided to the input\n");
   printf("\t-rms <filename>\t\tCompute the mean rms between both image\n");
   printf("\t-bin \t\t\tBinarise the input image (val!=0?val=1:val=0)\n");
   printf("\t-thr <float>\t\tThreshold the input image (val<thr?val=0:val=1)\n");
   printf("\t-nan <filename>\t\tThis image is used to mask the input image.\n\t\t\t\tVoxels outside of the mask are set to nan\n");
   printf("\t-iso\t\t\tThe resulting image is made isotropic\n");
91
   printf("\t-noscl\t\t\tThe scl_slope and scl_inter are set to 1 and 0 respectively\n");
92
#ifdef _SVN_REV
93
   printf("\t-v Print the subversion revision number\n");
94
#endif
95 96
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   return;
97 98 99 100
}

int main(int argc, char **argv)
{
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   PARAM *param = (PARAM *)calloc(1,sizeof(PARAM));
   FLAG *flag = (FLAG *)calloc(1,sizeof(FLAG));
   flag->operationTypeFlag=-1;

   if (argc < 2)
   {
      PetitUsage(argv[0]);
      return 1;
   }

   /* read the input parameter */
   for(int i=1; i<argc; i++)
   {
      if(strcmp(argv[i], "-help")==0 || strcmp(argv[i], "-Help")==0 ||
            strcmp(argv[i], "-HELP")==0 || strcmp(argv[i], "-h")==0 ||
            strcmp(argv[i], "--h")==0 || strcmp(argv[i], "--help")==0)
      {
         Usage(argv[0]);
         return 0;
      }
      else if(strcmp(argv[i], "--xml")==0)
      {
         printf("%s",xml_tools);
         return 0;
      }
126
#ifdef _SVN_REV
127 128 129 130 131 132 133
      if(strcmp(argv[i], "-version")==0 || strcmp(argv[i], "-Version")==0 ||
            strcmp(argv[i], "-V")==0 || strcmp(argv[i], "-v")==0 ||
            strcmp(argv[i], "--v")==0 || strcmp(argv[i], "--version")==0)
      {
         printf("NiftyReg revision number: %i\n",_SVN_REV);
         return 0;
      }
134
#endif
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      else if(strcmp(argv[i], "-in") == 0)
      {
         param->inputImageName=argv[++i];
         flag->inputImageFlag=1;
      }
      else if(strcmp(argv[i], "-out") == 0)
      {
         param->outputImageName=argv[++i];
         flag->outputImageFlag=1;
      }

      else if(strcmp(argv[i], "-add") == 0)
      {
         param->operationImageName=argv[++i];
         if(isNumeric(param->operationImageName)==true)
         {
            param->operationValue=(float)atof(param->operationImageName);
            param->operationImageName=NULL;
         }
         flag->operationTypeFlag=0;
      }
      else if(strcmp(argv[i], "-sub") == 0)
      {
         param->operationImageName=argv[++i];
         if(isNumeric(param->operationImageName))
         {
            param->operationValue=(float)atof(param->operationImageName);
            param->operationImageName=NULL;
         }
         flag->operationTypeFlag=1;
      }
      else if(strcmp(argv[i], "-mul") == 0)
      {
         param->operationImageName=argv[++i];
         if(isNumeric(param->operationImageName))
         {
            param->operationValue=(float)atof(param->operationImageName);
            param->operationImageName=NULL;
         }
         flag->operationTypeFlag=2;
      }
      else if(strcmp(argv[i], "-iso") == 0)
      {
         flag->iso=true;
      }
      else if(strcmp(argv[i], "-div") == 0)
      {
         param->operationImageName=argv[++i];
         if(isNumeric(param->operationImageName))
         {
            param->operationValue=(float)atof(param->operationImageName);
            param->operationImageName=NULL;
         }
         flag->operationTypeFlag=3;
      }
      else if(strcmp(argv[i], "-rms") == 0)
      {
         param->rmsImageName=argv[++i];
         flag->rmsImageFlag=1;
      }
      else if(strcmp(argv[i], "-down") == 0)
      {
         flag->downsampleFlag=1;
      }
      else if(strcmp(argv[i], "-float") == 0)
      {
         flag->floatFlag=1;
      }
      else if(strcmp(argv[i], "-smoS") == 0)
      {
         param->smoothValueX=atof(argv[++i]);
         param->smoothValueY=atof(argv[++i]);
         param->smoothValueZ=atof(argv[++i]);
         flag->smoothSplineFlag=1;
      }
      else if(strcmp(argv[i], "-smoG") == 0)
      {
         param->smoothValueX=atof(argv[++i]);
         param->smoothValueY=atof(argv[++i]);
         param->smoothValueZ=atof(argv[++i]);
         flag->smoothGaussianFlag=1;
      }
      else if(strcmp(argv[i], "-smoM") == 0)
      {
         param->smoothValueX=atof(argv[++i]);
         param->smoothValueY=atof(argv[++i]);
         param->smoothValueZ=atof(argv[++i]);
         flag->smoothMeanFlag=1;
      }
      else if(strcmp(argv[i], "-bin") == 0)
      {
         flag->binarisedImageFlag=1;
      }
      else if(strcmp(argv[i], "-thr") == 0)
      {
         param->thresholdImageValue=atof(argv[++i]);
         flag->thresholdImageFlag=1;
      }
      else if(strcmp(argv[i], "-nan") == 0)
      {
         param->operationImageName=argv[++i];
         flag->nanMaskFlag=1;
      }
      else if(strcmp(argv[i], "-norm") == 0)
      {
         flag->normFlag=1;
      }
242 243 244 245
      else if(strcmp(argv[i], "-noscl") == 0)
      {
         flag->nosclFlag=1;
      }
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
      else
      {
         fprintf(stderr,"Err:\tParameter %s unknown.\n",argv[i]);
         PetitUsage(argv[0]);
         return 1;
      }
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   /* Read the image */
   nifti_image *image = reg_io_ReadImageFile(param->inputImageName);
   if(image == NULL)
   {
      fprintf(stderr,"** ERROR Error when reading the target image: %s\n",param->inputImageName);
      return 1;
   }
   reg_checkAndCorrectDimension(image);

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   if(flag->floatFlag)
   {
      reg_tools_changeDatatype<float>(image);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(image, param->outputImageName);
      else reg_io_WriteImageFile(image, "output.nii");
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   if(flag->downsampleFlag)
   {
      bool dim[8]= {true,true,true,true,true,true,true,true};
      reg_downsampleImage<float>(image, true, dim);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(image, param->outputImageName);
      else reg_io_WriteImageFile(image, "output.nii");
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   // The image intensity are normalised between the 3 and 97%ile values
   if(flag->normFlag)
   {
      reg_tools_changeDatatype<float>(image);
      nifti_image *normImage = nifti_copy_nim_info(image);
      normImage->data = (void *)malloc(normImage->nvox * normImage->nbyper);
      memcpy(normImage->data, image->data, normImage->nvox*normImage->nbyper);
      reg_heapSort(static_cast<float *>(normImage->data), normImage->nvox);
      float minValue = static_cast<float *>(normImage->data)[static_cast<int>(reg_floor(03*normImage->nvox/100))];
      float maxValue = static_cast<float *>(normImage->data)[static_cast<int>(reg_floor(97*normImage->nvox/100))];
      reg_tools_substractValueToImage(image,normImage,minValue);
      reg_tools_divideValueToImage(normImage,normImage,maxValue-minValue);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(normImage, param->outputImageName);
      else reg_io_WriteImageFile(normImage, "output.nii");
      nifti_image_free(normImage);
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   if(flag->smoothGaussianFlag || flag->smoothSplineFlag || flag->smoothMeanFlag)
   {
      nifti_image *smoothImg = nifti_copy_nim_info(image);
      smoothImg->data = (void *)malloc(smoothImg->nvox * smoothImg->nbyper);
      memcpy(smoothImg->data, image->data, smoothImg->nvox*smoothImg->nbyper);
      float *kernelSize = new float[smoothImg->nt*smoothImg->nu];
      bool *timePoint = new bool[smoothImg->nt*smoothImg->nu];
      for(int i=0; i<smoothImg->nt*smoothImg->nu; ++i) timePoint[i]=true;
      bool boolX[3]= {1,0,0};
      for(int i=0; i<smoothImg->nt*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueX;
      if(flag->smoothMeanFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,2,NULL,timePoint,boolX);
      else if(flag->smoothSplineFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,1,NULL,timePoint,boolX);
      else reg_tools_kernelConvolution(smoothImg,kernelSize,0,NULL,timePoint,boolX);
      bool boolY[3]= {0,1,0};
      for(int i=0; i<smoothImg->nt*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueY;
      if(flag->smoothMeanFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,2,NULL,timePoint,boolY);
      else if(flag->smoothSplineFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,1,NULL,timePoint,boolY);
      else reg_tools_kernelConvolution(smoothImg,kernelSize,0,NULL,timePoint,boolY);
      bool boolZ[3]= {0,0,1};
      for(int i=0; i<smoothImg->nt*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueZ;
      if(flag->smoothMeanFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,2,NULL,timePoint,boolZ);
      else if(flag->smoothSplineFlag)
         reg_tools_kernelConvolution(smoothImg,kernelSize,1,NULL,timePoint,boolZ);
      else reg_tools_kernelConvolution(smoothImg,kernelSize,0,NULL,timePoint,boolZ);
      delete []kernelSize;
      delete []timePoint;
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(smoothImg, param->outputImageName);
      else reg_io_WriteImageFile(smoothImg, "output.nii");
      nifti_image_free(smoothImg);
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   if(flag->operationTypeFlag>-1)
   {
      nifti_image *image2=NULL;
      if(param->operationImageName!=NULL)
      {
         image2 = reg_io_ReadImageFile(param->operationImageName);
         if(image2 == NULL)
         {
            fprintf(stderr,"** ERROR Error when reading the image: %s\n",param->operationImageName);
356
            return 1;
357 358 359 360
         }
         reg_checkAndCorrectDimension(image2);
      }
      // Images are converted to the higher datatype
Marc Modat's avatar
Marc Modat committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
      if(image2!=NULL){
         switch(image->datatype>image2->datatype?image->datatype:image2->datatype)
         {
         case NIFTI_TYPE_UINT8:
            reg_tools_changeDatatype<unsigned char>(image,NIFTI_TYPE_UINT8);
            reg_tools_changeDatatype<unsigned char>(image2,NIFTI_TYPE_UINT8);
            break;
         case NIFTI_TYPE_INT8:
            reg_tools_changeDatatype<char>(image,NIFTI_TYPE_INT8);
            reg_tools_changeDatatype<char>(image2,NIFTI_TYPE_INT8);
            break;
         case NIFTI_TYPE_UINT16:
            reg_tools_changeDatatype<unsigned short>(image,NIFTI_TYPE_UINT16);
            reg_tools_changeDatatype<unsigned short>(image2,NIFTI_TYPE_UINT16);
            break;
         case NIFTI_TYPE_INT16:
            reg_tools_changeDatatype<short>(image,NIFTI_TYPE_INT16);
            reg_tools_changeDatatype<short>(image2,NIFTI_TYPE_INT16);
            break;
         case NIFTI_TYPE_UINT32:
            reg_tools_changeDatatype<unsigned int>(image,NIFTI_TYPE_UINT32);
            reg_tools_changeDatatype<unsigned int>(image2,NIFTI_TYPE_UINT32);
            break;
         case NIFTI_TYPE_INT32:
            reg_tools_changeDatatype<int>(image,NIFTI_TYPE_INT32);
            reg_tools_changeDatatype<int>(image2,NIFTI_TYPE_INT32);
            break;
         case NIFTI_TYPE_FLOAT32:
            reg_tools_changeDatatype<float>(image,NIFTI_TYPE_FLOAT32);
            reg_tools_changeDatatype<float>(image2,NIFTI_TYPE_FLOAT32);
            break;
         case NIFTI_TYPE_FLOAT64:
            reg_tools_changeDatatype<double>(image,NIFTI_TYPE_FLOAT64);
            reg_tools_changeDatatype<double>(image2,NIFTI_TYPE_FLOAT64);
            break;
         default:
            reg_print_msg_error("Unsurported data type.");
            reg_exit(1);
         }
400 401 402 403 404 405 406 407 408 409 410
      }

      nifti_image *resultImage = nifti_copy_nim_info(image);
      resultImage->data = (void *)malloc(resultImage->nvox * resultImage->nbyper);

      if(image2!=NULL)
      {
         switch(flag->operationTypeFlag)
         {
         case 0:
            reg_tools_addImageToImage(image, image2, resultImage);
411
            break;
412 413
         case 1:
            reg_tools_substractImageToImage(image, image2, resultImage);
414
            break;
415 416
         case 2:
            reg_tools_multiplyImageToImage(image, image2, resultImage);
417
            break;
418 419
         case 3:
            reg_tools_divideImageToImage(image, image2, resultImage);
420
            break;
421 422 423 424 425 426 427 428
         }
      }
      else
      {
         switch(flag->operationTypeFlag)
         {
         case 0:
            reg_tools_addValueToImage(image, resultImage, param->operationValue);
429
            break;
430 431
         case 1:
            reg_tools_substractValueToImage(image, resultImage, param->operationValue);
432
            break;
433 434
         case 2:
            reg_tools_multiplyValueToImage(image, resultImage, param->operationValue);
435
            break;
436 437
         case 3:
            reg_tools_divideValueToImage(image, resultImage, param->operationValue);
438
            break;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
         }
      }
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(resultImage,param->outputImageName);
      else reg_io_WriteImageFile(resultImage,"output.nii");

      nifti_image_free(resultImage);
      if(image2!=NULL) nifti_image_free(image2);
   }

   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//

   if(flag->rmsImageFlag)
   {
      nifti_image *image2 = reg_io_ReadImageFile(param->rmsImageName);
      if(image2 == NULL)
      {
         fprintf(stderr,"** ERROR Error when reading the image: %s\n",param->rmsImageName);
         return 1;
      }
      reg_checkAndCorrectDimension(image2);
      // Check image dimension
      if(image->dim[0]!=image2->dim[0] ||
            image->dim[1]!=image2->dim[1] ||
            image->dim[2]!=image2->dim[2] ||
            image->dim[3]!=image2->dim[3] ||
            image->dim[4]!=image2->dim[4] ||
            image->dim[5]!=image2->dim[5] ||
            image->dim[6]!=image2->dim[6] ||
            image->dim[7]!=image2->dim[7])
      {
         fprintf(stderr,"Both images do not have the same dimension\n");
         return 1;
      }

      double meanRMSerror = reg_tools_getMeanRMS(image, image2);
      printf("%g\n", meanRMSerror);
      nifti_image_free(image2);
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
   if(flag->binarisedImageFlag)
   {
      reg_tools_binarise_image(image);
      reg_tools_changeDatatype<unsigned char>(image);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(image,param->outputImageName);
      else reg_io_WriteImageFile(image,"output.nii");
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
   if(flag->thresholdImageFlag)
   {
      reg_tools_binarise_image(image, param->thresholdImageValue);
      reg_tools_changeDatatype<unsigned char>(image);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(image,param->outputImageName);
      else reg_io_WriteImageFile(image,"output.nii");
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
   if(flag->nanMaskFlag)
   {
      nifti_image *maskImage = reg_io_ReadImageFile(param->operationImageName);
      if(maskImage == NULL)
      {
         fprintf(stderr,"** ERROR Error when reading the image: %s\n",param->operationImageName);
         return 1;
      }
      reg_checkAndCorrectDimension(maskImage);

      nifti_image *resultImage = nifti_copy_nim_info(image);
      resultImage->data = (void *)malloc(resultImage->nvox * resultImage->nbyper);

      reg_tools_nanMask_image(image,maskImage,resultImage);

      if(flag->outputImageFlag)
         reg_io_WriteImageFile(resultImage,param->outputImageName);
      else reg_io_WriteImageFile(resultImage,"output.nii");

      nifti_image_free(resultImage);
      nifti_image_free(maskImage);
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
   if(flag->iso)
   {
      nifti_image *resultImage = reg_makeIsotropic(image,3);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(resultImage,param->outputImageName);
      else reg_io_WriteImageFile(resultImage,"output.nii");
      nifti_image_free(resultImage);
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
529 530 531 532 533 534 535 536
   if(flag->nosclFlag)
   {
      reg_tools_removeSCLInfo(image);
      if(flag->outputImageFlag)
         reg_io_WriteImageFile(image,param->outputImageName);
      else reg_io_WriteImageFile(image,"output.nii");
   }
   //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
537

Marc Modat's avatar
Marc Modat committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
   //    reg_tools_changeDatatype<float>(image);
   //    nifti_image *temp=nifti_copy_nim_info(image);
   //    temp->scl_inter=0.f;
   //    temp->scl_slope=1.f;
   //    temp->data=(void *)calloc(temp->nbyper,temp->nvox);
   //    float *imgPtr = static_cast<float *>(image->data);
   //    float *tmpPtr = static_cast<float *>(temp->data);

   //    size_t blockNumber=(image->nx/4)*(image->ny/4)*(image->nz/4);

   //    float *block_values=new float[blockNumber];

   //    size_t blockIndex=0;
   //    int znum=0;
   //    for(size_t z=0; z<image->nz; z+=4){
   //        int blockZ[2]={z,z+4};


   //        if(blockZ[1]<=image->nz){
   //            znum++;

   //            for(size_t y=0; y<image->ny; y+=4){
   //                int blockY[2]={y,y+4};

   //                if(blockY[1]<=image->ny){

   //                    for(size_t x=0; x<image->nx; x+=4){
   //                        int blockX[2]={x,x+4};

   //                        if(blockX[1]<=image->nx){

   //                            float mean=0;
   //                            for(int zz=blockZ[0];zz<blockZ[1];++zz){
   //                                for(int yy=blockY[0];yy<blockY[1];++yy){
   //                                    for(int xx=blockX[0];xx<blockX[1];++xx){
   //                                        mean+=imgPtr[(zz*image->ny+yy)*image->nx+xx];
   //                                    }
   //                                }
   //                            }
   //                            mean/=64.f;
   //                            float stddev=0;
   //                            for(int zz=blockZ[0];zz<blockZ[1];++zz){
   //                                for(int yy=blockY[0];yy<blockY[1];++yy){
   //                                    for(int xx=blockX[0];xx<blockX[1];++xx){
   //                                        stddev+=(mean-imgPtr[(zz*image->ny+yy)*image->nx+xx])*(mean-imgPtr[(zz*image->ny+yy)*image->nx+xx]);
   //                                    }
   //                                }
   //                            }
   //                            stddev/=64.f;
   //                            block_values[blockIndex]=stddev;
   //                            blockIndex++;
   //                        } //ifx
   //                    } //x
   //                } //ify
   //            } //y
   //        } //ifz
   //    } //z
   //    int *block_index=new int[blockNumber];
   //    for(int i=0;i<blockNumber;++i){
   //        block_index[i]=i;
   //    }

   //    reg_heapSort(block_values,block_index,blockNumber);

   //    for(int i=blockNumber-1;i>blockNumber/2;--i){

   //        int z=block_index[i]/((int)floor(image->nx/4)*(int)floor(image->ny/4));
   //        int temporary=block_index[i]-z*(int)floor(image->nx/4)*(int)floor(image->ny/4);
   //        int y=temporary/(int)floor(image->nx/4);
   //        int x=temporary-y*(int)floor(image->nx/4);
   //        for(int zz=z*4;zz<z*4+4;++zz){
   //            for(int yy=y*4;yy<y*4+4;++yy){
   //                for(int xx=x*4;xx<x*4+4;++xx){
   //                    tmpPtr[(zz*image->ny+yy)*image->nx+xx]=1.f;
   //                }
   //            }
   //        }
   //    }
   //    delete []block_index;
   //    delete []block_values;
   //    reg_io_WriteImageFile(temp,param->outputImageName);
   //    nifti_image_free(temp);
620

621 622
   nifti_image_free(image);
   return 0;
623 624 625
}

#endif