Commit d4b3c1e3 authored by Marc Modat's avatar Marc Modat

error when readin nhrd file

parent c2da3a21
......@@ -352,43 +352,45 @@ int main(int argc, char **argv)
reg_checkAndCorrectDimension(image2);
}
// Images are converted to the higher datatype
switch(image->datatype>image2->datatype?image->datatype:image2->datatype)
{
case NIFTI_TYPE_UINT8:
reg_tools_changeDatatype<unsigned char>(image);
reg_tools_changeDatatype<unsigned char>(image2);
break;
case NIFTI_TYPE_INT8:
reg_tools_changeDatatype<char>(image);
reg_tools_changeDatatype<char>(image2);
break;
case NIFTI_TYPE_UINT16:
reg_tools_changeDatatype<unsigned short>(image);
reg_tools_changeDatatype<unsigned short>(image2);
break;
case NIFTI_TYPE_INT16:
reg_tools_changeDatatype<short>(image);
reg_tools_changeDatatype<short>(image2);
break;
case NIFTI_TYPE_UINT32:
reg_tools_changeDatatype<unsigned int>(image);
reg_tools_changeDatatype<unsigned int>(image2);
break;
case NIFTI_TYPE_INT32:
reg_tools_changeDatatype<int>(image);
reg_tools_changeDatatype<int>(image2);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_changeDatatype<float>(image);
reg_tools_changeDatatype<float>(image2);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_changeDatatype<double>(image);
reg_tools_changeDatatype<double>(image2);
break;
default:
reg_print_msg_error("Unsurported data type.");
reg_exit(1);
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);
}
}
nifti_image *resultImage = nifti_copy_nim_info(image);
......@@ -519,88 +521,88 @@ int main(int argc, char **argv)
}
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// 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);
// 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);
nifti_image_free(image);
return 0;
......
......@@ -52,7 +52,7 @@ int reg_io_checkFileFormat(const char *filename)
return NR_PNG_FORMAT;
else if(b.find( ".nrrd") != std::string::npos)
return NR_NRRD_FORMAT;
else if(b.find( ".nhrd") != std::string::npos)
else if(b.find( ".nhdr") != std::string::npos)
return NR_NRRD_FORMAT;
else fprintf(stderr, "[NiftyReg WARNING]: No filename extension provided - the Nifti library is used by default\n");
......
......@@ -1085,7 +1085,8 @@ void reg_f3d<T>::GetObjectiveFunctionGradient()
if(!this->useApproxGradient)
{
// Compute the gradient of the similarity measure
if(this->similarityWeight>0)
if(this->similarityWeight>0 &&
(this->measure_nmi!=NULL || this->measure_multichannel_nmi!=NULL) )
{
this->WarpFloatingImage(this->interpolation);
this->ComputeSimilarityMeasure();
......
......@@ -1080,7 +1080,8 @@ void reg_f3d_sym<T>::GetObjectiveFunctionGradient()
if(!this->useApproxGradient)
{
// Compute the gradient of the similarity measure
if(this->similarityWeight>0)
if(this->similarityWeight>0 &&
(this->measure_nmi!=NULL || this->measure_multichannel_nmi!=NULL))
{
this->WarpFloatingImage(this->interpolation);
this->ComputeSimilarityMeasure();
......
......@@ -383,20 +383,25 @@ template double reg_getMaximalLength<double>(nifti_image *);
/* *************************************************************** */
/* *************************************************************** */
template <class NewTYPE, class DTYPE>
void reg_tools_changeDatatype1(nifti_image *image)
void reg_tools_changeDatatype1(nifti_image *image,int type)
{
// the initial array is saved and freeed
DTYPE *initialValue = (DTYPE *)malloc(image->nvox*sizeof(DTYPE));
memcpy(initialValue, image->data, image->nvox*sizeof(DTYPE));
// the new array is allocated and then filled
if(sizeof(NewTYPE)==sizeof(unsigned char)) image->datatype = NIFTI_TYPE_UINT8;
else if(sizeof(NewTYPE)==sizeof(float)) image->datatype = NIFTI_TYPE_FLOAT32;
else if(sizeof(NewTYPE)==sizeof(double)) image->datatype = NIFTI_TYPE_FLOAT64;
else
{
fprintf(stderr,"[NiftyReg ERROR] reg_tools_changeDatatype\tOnly change to unsigned char, float or double are supported\n");
exit(1);
if(type>-1){
image->datatype=type;
}
else{
if(sizeof(NewTYPE)==sizeof(unsigned char)) image->datatype = NIFTI_TYPE_UINT8;
else if(sizeof(NewTYPE)==sizeof(float)) image->datatype = NIFTI_TYPE_FLOAT32;
else if(sizeof(NewTYPE)==sizeof(double)) image->datatype = NIFTI_TYPE_FLOAT64;
else
{
fprintf(stderr,"[NiftyReg ERROR] reg_tools_changeDatatype\tOnly change to unsigned char, float or double are supported\n");
exit(1);
}
}
free(image->data);
image->nbyper = sizeof(NewTYPE);
......@@ -410,33 +415,33 @@ void reg_tools_changeDatatype1(nifti_image *image)
}
/* *************************************************************** */
template <class NewTYPE>
void reg_tools_changeDatatype(nifti_image *image)
void reg_tools_changeDatatype(nifti_image *image, int type)
{
switch(image->datatype)
{
case NIFTI_TYPE_UINT8:
reg_tools_changeDatatype1<NewTYPE,unsigned char>(image);
reg_tools_changeDatatype1<NewTYPE,unsigned char>(image,type);
break;
case NIFTI_TYPE_INT8:
reg_tools_changeDatatype1<NewTYPE,char>(image);
reg_tools_changeDatatype1<NewTYPE,char>(image,type);
break;
case NIFTI_TYPE_UINT16:
reg_tools_changeDatatype1<NewTYPE,unsigned short>(image);
reg_tools_changeDatatype1<NewTYPE,unsigned short>(image,type);
break;
case NIFTI_TYPE_INT16:
reg_tools_changeDatatype1<NewTYPE,short>(image);
reg_tools_changeDatatype1<NewTYPE,short>(image,type);
break;
case NIFTI_TYPE_UINT32:
reg_tools_changeDatatype1<NewTYPE,unsigned int>(image);
reg_tools_changeDatatype1<NewTYPE,unsigned int>(image,type);
break;
case NIFTI_TYPE_INT32:
reg_tools_changeDatatype1<NewTYPE,int>(image);
reg_tools_changeDatatype1<NewTYPE,int>(image,type);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_changeDatatype1<NewTYPE,float>(image);
reg_tools_changeDatatype1<NewTYPE,float>(image,type);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_changeDatatype1<NewTYPE,double>(image);
reg_tools_changeDatatype1<NewTYPE,double>(image,type);
break;
default:
fprintf(stderr,"[NiftyReg ERROR] reg_tools_changeDatatype\tThe initial image data type is not supported\n");
......@@ -444,14 +449,14 @@ void reg_tools_changeDatatype(nifti_image *image)
}
}
/* *************************************************************** */
template void reg_tools_changeDatatype<unsigned char>(nifti_image *);
template void reg_tools_changeDatatype<unsigned short>(nifti_image *);
template void reg_tools_changeDatatype<unsigned int>(nifti_image *);
template void reg_tools_changeDatatype<char>(nifti_image *);
template void reg_tools_changeDatatype<short>(nifti_image *);
template void reg_tools_changeDatatype<int>(nifti_image *);
template void reg_tools_changeDatatype<float>(nifti_image *);
template void reg_tools_changeDatatype<double>(nifti_image *);
template void reg_tools_changeDatatype<unsigned char>(nifti_image *, int);
template void reg_tools_changeDatatype<unsigned short>(nifti_image *, int);
template void reg_tools_changeDatatype<unsigned int>(nifti_image *, int);
template void reg_tools_changeDatatype<char>(nifti_image *, int);
template void reg_tools_changeDatatype<short>(nifti_image *, int);
template void reg_tools_changeDatatype<int>(nifti_image *, int);
template void reg_tools_changeDatatype<float>(nifti_image *, int);
template void reg_tools_changeDatatype<double>(nifti_image *, int);
/* *************************************************************** */
/* *************************************************************** */
template <class TYPE1>
......
......@@ -104,7 +104,8 @@ PrecisionTYPE reg_getMaximalLength(nifti_image *image);
* @param image Image to be updated.
*/
extern "C++" template <class NewTYPE>
void reg_tools_changeDatatype(nifti_image *image);
void reg_tools_changeDatatype(nifti_image *image,
int type=-1);
/* *************************************************************** */
/** @brief Add two images.
* @param img1 First image to consider
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment