Commit f9894a42 authored by Marc Modat's avatar Marc Modat

Some arguments have been added in reg_tools

parent d37dfef8
......@@ -203,6 +203,8 @@ int main(int argc, char **argv)
positionFieldImage = nifti_copy_nim_info(targetImage);
positionFieldImage->cal_min=0;
positionFieldImage->cal_max=0;
positionFieldImage->scl_slope = 1.0f;
positionFieldImage->scl_inter = 0.0f;
positionFieldImage->dim[0]=positionFieldImage->ndim=5;
positionFieldImage->dim[1]=positionFieldImage->nx=targetImage->nx;
positionFieldImage->dim[2]=positionFieldImage->ny=targetImage->ny;
......@@ -236,6 +238,8 @@ int main(int argc, char **argv)
/* Generate the jacobian map */
if(flag->outputJacobianFlag){
nifti_image *jacobianImage = nifti_copy_nim_info(targetImage);
jacobianImage->scl_slope = 1.0f;
jacobianImage->scl_inter = 0.0f;
jacobianImage->datatype = NIFTI_TYPE_FLOAT32;
jacobianImage->nbyper = sizeof(float);
jacobianImage->data = (void *)calloc(jacobianImage->nvox, jacobianImage->nbyper);
......@@ -251,6 +255,8 @@ int main(int argc, char **argv)
nifti_image *jacobianImage = nifti_copy_nim_info(targetImage);
jacobianImage->cal_min=0;
jacobianImage->cal_max=0;
jacobianImage->scl_slope = 1.0f;
jacobianImage->scl_inter = 0.0f;
jacobianImage->dim[0]=jacobianImage->ndim=5;
jacobianImage->dim[1]=jacobianImage->nx=targetImage->nx;
jacobianImage->dim[2]=jacobianImage->ny=targetImage->ny;
......@@ -347,6 +353,8 @@ int main(int argc, char **argv)
nifti_image *resultImage = nifti_copy_nim_info(targetImage);
resultImage->cal_min=gridImage->cal_min;
resultImage->cal_max=gridImage->cal_max;
resultImage->scl_slope = 1.0f;
resultImage->scl_inter = 0.0f;
resultImage->datatype = gridImage->datatype;
resultImage->nbyper = gridImage->nbyper;
resultImage->data = (void *)calloc(resultImage->nvox, resultImage->nbyper);
......@@ -374,6 +382,8 @@ int main(int argc, char **argv)
/* Output the deformation field */
if(flag->outputDefFlag){
nifti_image *deformationFieldImage = nifti_copy_nim_info(positionFieldImage);
deformationFieldImage->scl_slope = 1.0f;
deformationFieldImage->scl_inter = 0.0f;
deformationFieldImage->data = (void *)calloc(deformationFieldImage->nvox, deformationFieldImage->nbyper);
nifti_set_filenames(deformationFieldImage, param->outputDefName, 0, 0);
memcpy(deformationFieldImage->data, positionFieldImage->data, deformationFieldImage->nvox*deformationFieldImage->nbyper);
......
......@@ -22,6 +22,13 @@ typedef struct{
char *inputImageName;
char *outputImageName;
char *addImageName;
char *subImageName;
char *mulImageName;
char *divImageName;
float addValue;
float subValue;
float mulValue;
float divValue;
char *rmsImageName;
int smoothValue;
}PARAM;
......@@ -29,6 +36,13 @@ typedef struct{
bool inputImageFlag;
bool outputImageFlag;
bool addImageFlag;
bool subImageFlag;
bool mulImageFlag;
bool divImageFlag;
bool addValueFlag;
bool subValueFlag;
bool mulValueFlag;
bool divValueFlag;
bool rmsImageFlag;
bool smoothValueFlag;
bool gradientImageFlag;
......@@ -49,7 +63,14 @@ void Usage(char *exec)
printf("* * OPTIONS * *\n");
printf("\t-out <filename>\t\tFilename out the output image [output.nii]\n");
printf("\t-grad\t\t\t4D spatial gradient of the input image\n");
printf("\t-add <filename>\t\tThis image is added to the input\n");
printf("\t-add <filename>\t\tThis image is added to the input\n");
printf("\t-sub <filename>\t\tThis image is subtracted to the input\n");
printf("\t-mul <filename>\t\tThis image is multiplied to the input\n");
printf("\t-div <filename>\t\tThis image is divided to the input\n");
printf("\t-addV <float>\t\tThis value is added to the input\n");
printf("\t-subV <float>\t\tThis value is subtracted to the input\n");
printf("\t-mulV <float>\t\tThis value is multiplied to the input\n");
printf("\t-divV <float>\t\tThis value is divided to the input\n");
printf("\t-smo <int>\t\tThe input image is smoothed using a b-spline curve\n");
printf("\t-rms <filename>\tCompute the mean rms between both image\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
......@@ -80,10 +101,41 @@ int main(int argc, char **argv)
else if(strcmp(argv[i], "-grad") == 0){
flag->gradientImageFlag=1;
}
else if(strcmp(argv[i], "-add") == 0){
param->addImageName=argv[++i];
flag->addImageFlag=1;
}
else if(strcmp(argv[i], "-sub") == 0){
param->subImageName=argv[++i];
flag->subImageFlag=1;
}
else if(strcmp(argv[i], "-mul") == 0){
param->mulImageName=argv[++i];
flag->mulImageFlag=1;
}
else if(strcmp(argv[i], "-div") == 0){
param->divImageName=argv[++i];
flag->divImageFlag=1;
}
else if(strcmp(argv[i], "-addV") == 0){
param->addValue=atof(argv[++i]);
flag->addValueFlag=1;
}
else if(strcmp(argv[i], "-subV") == 0){
param->subValue=atof(argv[++i]);
flag->subValueFlag=1;
}
else if(strcmp(argv[i], "-mulV") == 0){
param->mulValue=atof(argv[++i]);
flag->mulValueFlag=1;
}
else if(strcmp(argv[i], "-divV") == 0){
param->divValue=atof(argv[++i]);
flag->divValueFlag=1;
}
else if(strcmp(argv[i], "-rms") == 0){
param->rmsImageName=argv[++i];
flag->rmsImageFlag=1;
......@@ -162,34 +214,66 @@ int main(int argc, char **argv)
nifti_image_free(smoothImg);
}
if(flag->addImageFlag){
nifti_image *imageToAdd = nifti_image_read(param->addImageName,true);
if(imageToAdd == NULL){
fprintf(stderr,"** ERROR Error when reading the image to add: %s\n",param->addImageName);
if(flag->addImageFlag || flag->subImageFlag || flag->mulImageFlag || flag->divImageFlag){
nifti_image *image2;
if(flag->addImageFlag) image2 = nifti_image_read(param->addImageName,true);
if(flag->subImageFlag) image2 = nifti_image_read(param->subImageName,true);
if(flag->mulImageFlag) image2 = nifti_image_read(param->mulImageName,true);
if(flag->divImageFlag) image2 = nifti_image_read(param->divImageName,true);
if(image2 == NULL){
if(flag->addImageFlag)
fprintf(stderr,"** ERROR Error when reading the image to add: %s\n",param->addImageName);
if(flag->subImageFlag)
fprintf(stderr,"** ERROR Error when reading the image to sub: %s\n",param->subImageName);
if(flag->mulImageFlag)
fprintf(stderr,"** ERROR Error when reading the image to mul: %s\n",param->mulImageName);
if(flag->divImageFlag)
fprintf(stderr,"** ERROR Error when reading the image to div: %s\n",param->divImageName);
return 1;
}
// Check image dimension
if(image->dim[0]!=imageToAdd->dim[0] ||
image->dim[1]!=imageToAdd->dim[1] ||
image->dim[2]!=imageToAdd->dim[2] ||
image->dim[3]!=imageToAdd->dim[3] ||
image->dim[4]!=imageToAdd->dim[4] ||
image->dim[5]!=imageToAdd->dim[5] ||
image->dim[6]!=imageToAdd->dim[6] ||
image->dim[7]!=imageToAdd->dim[7]){
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;
}
nifti_image *sumImage = nifti_copy_nim_info(image);
sumImage->data = (void *)malloc(sumImage->nvox * sumImage->nbyper);
nifti_image *resultImage = nifti_copy_nim_info(image);
resultImage->data = (void *)malloc(resultImage->nvox * resultImage->nbyper);
if(flag->outputImageFlag)
nifti_set_filenames(sumImage, param->outputImageName, 0, 0);
else nifti_set_filenames(sumImage, "output.nii", 0, 0);
nifti_set_filenames(resultImage, param->outputImageName, 0, 0);
else nifti_set_filenames(resultImage, "output.nii", 0, 0);
if(flag->addImageFlag) reg_tools_addSubMulDivImages(image, image2, resultImage, 0);
if(flag->subImageFlag) reg_tools_addSubMulDivImages(image, image2, resultImage, 1);
if(flag->mulImageFlag) reg_tools_addSubMulDivImages(image, image2, resultImage, 2);
if(flag->divImageFlag) reg_tools_addSubMulDivImages(image, image2, resultImage, 3);
nifti_image_write(resultImage);
nifti_image_free(resultImage);
nifti_image_free(image2);
}
if(flag->addValueFlag || flag->subValueFlag || flag->mulValueFlag || flag->divValueFlag){
nifti_image *resultImage = nifti_copy_nim_info(image);
resultImage->data = (void *)malloc(resultImage->nvox * resultImage->nbyper);
if(flag->outputImageFlag)
nifti_set_filenames(resultImage, param->outputImageName, 0, 0);
else nifti_set_filenames(resultImage, "output.nii", 0, 0);
if(flag->addValueFlag) reg_tools_addSubMulDivValue(image, resultImage, param->addValue, 0);
if(flag->subValueFlag) reg_tools_addSubMulDivValue(image, resultImage, param->subValue, 1);
if(flag->mulValueFlag) reg_tools_addSubMulDivValue(image, resultImage, param->mulValue, 2);
if(flag->divValueFlag) reg_tools_addSubMulDivValue(image, resultImage, param->divValue, 3);
reg_tools_addImages(image, imageToAdd, sumImage);
nifti_image_write(sumImage);
nifti_image_free(sumImage);
nifti_image_free(imageToAdd);
nifti_image_write(resultImage);
nifti_image_free(resultImage);
}
if(flag->rmsImageFlag){
......
......@@ -656,104 +656,203 @@ double reg_tool_GetIntensityValue(nifti_image *image,
/* *************************************************************** */
/* *************************************************************** */
template <class TYPE1, class TYPE2>
void reg_tools_addImages2( nifti_image *img1,
nifti_image *img2,
nifti_image *res)
void reg_tools_addSubMulDivImages2( nifti_image *img1,
nifti_image *img2,
nifti_image *res,
int type)
{
TYPE1 *img1Ptr = static_cast<TYPE1 *>(img1->data);
TYPE2 *img2Ptr = static_cast<TYPE2 *>(img2->data);
TYPE1 *resPtr = static_cast<TYPE1 *>(res->data);
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ + *img2Ptr++);
TYPE1 *img1Ptr = static_cast<TYPE1 *>(img1->data);
TYPE2 *img2Ptr = static_cast<TYPE2 *>(img2->data);
TYPE1 *resPtr = static_cast<TYPE1 *>(res->data);
switch(type){
case 0:
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ + *img2Ptr++);
break;
case 1:
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ - *img2Ptr++);
break;
case 2:
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ * *img2Ptr++);
break;
case 3:
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ / *img2Ptr++);
break;
}
}
/* *************************************************************** */
template <class TYPE1>
void reg_tools_addImages1( nifti_image *img1,
nifti_image *img2,
nifti_image *res)
void reg_tools_addSubMulDivImages1( nifti_image *img1,
nifti_image *img2,
nifti_image *res,
int type)
{
switch(img1->datatype){
case NIFTI_TYPE_UINT8:
reg_tools_addImages2<TYPE1,unsigned char>(img1, img2, res);
break;
case NIFTI_TYPE_INT8:
reg_tools_addImages2<TYPE1,char>(img1, img2, res);
break;
case NIFTI_TYPE_UINT16:
reg_tools_addImages2<TYPE1,unsigned short>(img1, img2, res);
break;
case NIFTI_TYPE_INT16:
reg_tools_addImages2<TYPE1,short>(img1, img2, res);
break;
case NIFTI_TYPE_UINT32:
reg_tools_addImages2<TYPE1,unsigned int>(img1, img2, res);
break;
case NIFTI_TYPE_INT32:
reg_tools_addImages2<TYPE1,int>(img1, img2, res);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_addImages2<TYPE1,float>(img1, img2, res);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_addImages2<TYPE1,double>(img1, img2, res);
break;
default:
fprintf(stderr,"err\treg_tools_addImage\tSecond image data type is not supported\n");
return;
}
switch(img1->datatype){
case NIFTI_TYPE_UINT8:
reg_tools_addSubMulDivImages2<TYPE1,unsigned char>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT8:
reg_tools_addSubMulDivImages2<TYPE1,char>(img1, img2, res, type);
break;
case NIFTI_TYPE_UINT16:
reg_tools_addSubMulDivImages2<TYPE1,unsigned short>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT16:
reg_tools_addSubMulDivImages2<TYPE1,short>(img1, img2, res, type);
break;
case NIFTI_TYPE_UINT32:
reg_tools_addSubMulDivImages2<TYPE1,unsigned int>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT32:
reg_tools_addSubMulDivImages2<TYPE1,int>(img1, img2, res, type);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_addSubMulDivImages2<TYPE1,float>(img1, img2, res, type);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_addSubMulDivImages2<TYPE1,double>(img1, img2, res, type);
break;
default:
fprintf(stderr,"err\treg_tools_addSubMulDivImages1\tSecond image data type is not supported\n");
return;
}
}
/* *************************************************************** */
void reg_tools_addImages( nifti_image *img1,
nifti_image *img2,
nifti_image *res)
void reg_tools_addSubMulDivImages( nifti_image *img1,
nifti_image *img2,
nifti_image *res,
int type)
{
if(img1->dim[0]!=img2->dim[0] ||
img1->dim[1]!=img2->dim[1] ||
img1->dim[2]!=img2->dim[2] ||
img1->dim[3]!=img2->dim[3] ||
img1->dim[4]!=img2->dim[4] ||
img1->dim[5]!=img2->dim[5] ||
img1->dim[6]!=img2->dim[6] ||
img1->dim[7]!=img2->dim[7]){
fprintf(stderr,"err\treg_tools_addImage\tBoth images do not have the same dimension\n");
return;
}
if(img1->dim[0]!=img2->dim[0] ||
img1->dim[1]!=img2->dim[1] ||
img1->dim[2]!=img2->dim[2] ||
img1->dim[3]!=img2->dim[3] ||
img1->dim[4]!=img2->dim[4] ||
img1->dim[5]!=img2->dim[5] ||
img1->dim[6]!=img2->dim[6] ||
img1->dim[7]!=img2->dim[7]){
fprintf(stderr,"err\treg_tools_addSubMulDivImages\tBoth images do not have the same dimension\n");
return;
}
if(img1->datatype != img2->datatype){
fprintf(stderr,"err\treg_tools_addImage\tFirst and result image do not have the same data type\n");
return;
}
switch(img1->datatype){
case NIFTI_TYPE_UINT8:
reg_tools_addImages1<unsigned char>(img1, img2, res);
break;
case NIFTI_TYPE_INT8:
reg_tools_addImages1<char>(img1, img1, res);
break;
case NIFTI_TYPE_UINT16:
reg_tools_addImages1<unsigned short>(img1, img2, res);
break;
case NIFTI_TYPE_INT16:
reg_tools_addImages1<short>(img1, img2, res);
break;
case NIFTI_TYPE_UINT32:
reg_tools_addImages1<unsigned int>(img1, img2, res);
break;
case NIFTI_TYPE_INT32:
reg_tools_addImages1<int>(img1, img2, res);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_addImages1<float>(img1, img2, res);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_addImages1<double>(img1, img2, res);
break;
default:
fprintf(stderr,"err\treg_tools_addImage\tFirst image data type is not supported\n");
return;
}
if(img1->datatype != res->datatype){
fprintf(stderr,"err\treg_tools_addSubMulDivImages\tFirst and result image do not have the same data type\n");
return;
}
switch(img1->datatype){
case NIFTI_TYPE_UINT8:
reg_tools_addSubMulDivImages1<unsigned char>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT8:
reg_tools_addSubMulDivImages1<char>(img1, img1, res, type);
break;
case NIFTI_TYPE_UINT16:
reg_tools_addSubMulDivImages1<unsigned short>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT16:
reg_tools_addSubMulDivImages1<short>(img1, img2, res, type);
break;
case NIFTI_TYPE_UINT32:
reg_tools_addSubMulDivImages1<unsigned int>(img1, img2, res, type);
break;
case NIFTI_TYPE_INT32:
reg_tools_addSubMulDivImages1<int>(img1, img2, res, type);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_addSubMulDivImages1<float>(img1, img2, res, type);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_addSubMulDivImages1<double>(img1, img2, res, type);
break;
default:
fprintf(stderr,"err\treg_tools_addSubMulDivImages1\tFirst image data type is not supported\n");
return;
}
}
/* *************************************************************** */
/* *************************************************************** */
template <class TYPE1>
void reg_tools_addSubMulDivValue1( nifti_image *img1,
nifti_image *res,
float val,
int type)
{
TYPE1 *img1Ptr = static_cast<TYPE1 *>(img1->data);
TYPE1 *resPtr = static_cast<TYPE1 *>(res->data);
switch(type){
case 0:
printf("+ %g\n",val);
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ + val);
break;
case 1:
printf("- %g\n",val);
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ - val);
break;
case 2:
printf("* %g\n",val);
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ * val);
break;
case 3:
printf("/ %g\n",val);
for(unsigned int i=0; i<res->nvox; i++)
*resPtr++ = (TYPE1)(*img1Ptr++ / val);
break;
}
}
/* *************************************************************** */
void reg_tools_addSubMulDivValue( nifti_image *img1,
nifti_image *res,
float val,
int type)
{
if(img1->datatype != res->datatype){
fprintf(stderr,"err\treg_tools_addSubMulDivValue\tInput and result image do not have the same data type\n");
return;
}
switch(img1->datatype){
case NIFTI_TYPE_UINT8:
reg_tools_addSubMulDivValue1<unsigned char>
(img1, res, val, type);
break;
case NIFTI_TYPE_INT8:
reg_tools_addSubMulDivValue1<char>
(img1, res, val, type);
break;
case NIFTI_TYPE_UINT16:
reg_tools_addSubMulDivValue1<unsigned short>
(img1, res, val, type);
break;
case NIFTI_TYPE_INT16:
reg_tools_addSubMulDivValue1<short>
(img1, res, val, type);
break;
case NIFTI_TYPE_UINT32:
reg_tools_addSubMulDivValue1<unsigned int>
(img1, res, val, type);
break;
case NIFTI_TYPE_INT32:
reg_tools_addSubMulDivValue1<int>
(img1, res, val, type);
break;
case NIFTI_TYPE_FLOAT32:
reg_tools_addSubMulDivValue1<float>
(img1, res, val, type);
break;
case NIFTI_TYPE_FLOAT64:
reg_tools_addSubMulDivValue1<double>
(img1, res, val, type);
break;
default:
fprintf(stderr,"err\treg_tools_addSubMulDivImages1\tFirst image data type is not supported\n");
return;
}
}
/* *************************************************************** */
/* *************************************************************** */
......
......@@ -50,9 +50,15 @@ double reg_tool_GetIntensityValue(nifti_image *,
int *);
extern "C++"
void reg_tools_addImages( nifti_image *,
nifti_image *,
nifti_image *);
void reg_tools_addSubMulDivImages( nifti_image *,
nifti_image *,
nifti_image *,
int);
extern "C++"
void reg_tools_addSubMulDivValue( nifti_image *,
nifti_image *,
float,
int);
extern "C++"
void reg_tool_binarise_image(nifti_image *);
......
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