8#include "tpcclibConfig.h"
25static char *info[] = {
26 "Program for adding Gaussian noise to dynamic PET image",
27 "using equations (1, 2):",
28 " SD(t) = TAC(t) * sqrt(Pc/(TAC(t)*exp(-lambda*t)*deltat)",
29 " TAC_noisy(t) = TAC(t) + SD(t)*G(0,1) ,",
30 "where Pc is the proportionality constant that determines the level of noise,",
31 "TAC(t) is the mean activity concentration in the image frame,",
32 "Deltat is the scan frame length, and G(0,1) is a pseudo random number from",
33 "a Gaussian distribution with zero mean and SD of one.",
35 "Image must be in ECAT (6.3 or 7) format, or, Analyze 7.5 or NIfTI-1 format",
37 "Image is assumed to be decay corrected to zero time.",
39 "Usage: @P [Options] image Pc outputimage ",
42 " -seed=<seed for random number generator>",
43 " Computer clock is used by default.",
44 " -i=<O-15|N-13|C-11|F-18|Ge-68|Ga-68|Br-76|Rb-82|Cu-62>",
45 " Specifies the isotope.",
47 " Set a minimum SD to add a minimum level of noise also to the image",
48 " frames with no activity.",
49 " -efile=<TAC filename>",
50 " Save image mean TAC and SD that were used in noise simulation.",
54 " @P -i=C-11 simulated.v 5.0 noisy.v",
57 "1. Chen K, Huang SC, Yu DC. Phys Med Biol 1991;36:1183-1200.",
58 "2. Varga J, Szabo Z. J Cereb Blood Flow Metab 2002;22:240-244.",
60 "See also: dft2img, flat2img, eframe, imgdecay, fvar4tac, img2tif",
62 "Keywords: image, simulation, noise",
81int main(
int argc,
char **argv)
83 int ai, help=0, version=0, verbose=1;
84 int ret=0, fi, pi, ri, ci;
85 unsigned long int seed=0L;
86 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX], efile[FILENAME_MAX];
87 char *cptr, tmp[FILENAME_MAX];
88 double pc=-1.0, minsd=-1.0, halflife=0.0;
96 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
97 imgfile[0]=outfile[0]=efile[0]=(char)0;
100 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
101 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
103 if(strncasecmp(cptr,
"S=", 2)==0) {
104 seed=atol(cptr+2);
if(seed>0L)
continue;
105 }
else if(strncasecmp(cptr,
"SEED=", 5)==0) {
106 seed=atol(cptr+5);
if(seed>0L)
continue;
107 }
else if(strncasecmp(cptr,
"I=", 2)==0) {
109 halflife=60.0*
hlFromIsotope(cptr);
if(halflife>0.0)
continue;}
110 fprintf(stderr,
"Error: invalid isotope with option -i\n");
return(1);
111 }
else if(strncasecmp(cptr,
"MINSD=", 6)==0) {
112 minsd=
atof_dpi(cptr+6);
if(minsd>0.0)
continue;
113 }
else if(strncasecmp(cptr,
"EFILE=", 6)==0) {
114 strcpy(efile, cptr+6);
if(strlen(efile)>1)
continue;
116 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
121 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
126 for(; ai<argc; ai++) {
128 strcpy(imgfile, argv[ai]);
continue;
130 pc=
atof_dpi(argv[ai]);
if(pc>0.0)
continue;
131 }
else if(!outfile[0]) {
132 strcpy(outfile, argv[ai]);
continue;
135 fprintf(stderr,
"Error: too many arguments: '%s'.\n", argv[ai]);
141 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n",
145 if(strcasecmp(imgfile, outfile)==0) {
146 fprintf(stderr,
"Error: must not overwrite input image.\n");
return(1);
151 printf(
"imgfile := %s\n", imgfile);
152 printf(
"outfile := %s\n", outfile);
153 printf(
"proportionality_constant := %g\n", pc);
154 if(halflife>0.0) printf(
"halflife := %g\n", halflife);
155 if(minsd>0.0) printf(
"minsd := %g\n", minsd);
156 printf(
"efile := %s\n", efile);
163 if(verbose>1) printf(
"reading image %s\n", imgfile);
165 fprintf(stderr,
"Error: %s\n", img.
statmsg);
172 if(verbose>0) fprintf(stderr,
"Warning: missing pixel values.\n");
175 fprintf(stderr,
"Error: %s is not an image.\n", imgfile);
180 fprintf(stderr,
"Error: image does not contain frame times.\n");
185 fprintf(stderr,
"Error: set isotope with option -i\n");
193 fprintf(stderr,
"Error %d in setting decay correction factors.\n", ret);
201 fprintf(stderr,
"Error: %s\n", tmp);
209 if(verbose>1) printf(
"calculating average TAC of all image pixels\n");
213 fprintf(stderr,
"Error: cannot allocate memory.\n");
223 for(fi=0; fi<img.
dimt; fi++) {
225 tac.
x[fi]=img.
mid[fi];
231 fprintf(stderr,
"Error: cannot calculate average TAC (%d).\n", ret);
235 for(fi=0; fi<img.
dimt; fi++) tac.
voi[0].
y[fi]=img.
sd[fi];
242 if(verbose>1) printf(
"calculating SD for each frame\n");
246 "Error: cannot calculate SD for noise simulation (%d).\n", ret);
247 if(verbose>1) printf(
" %s\n", tmp);
253 for(fi=0; fi<img.
dimt; fi++)
254 if(minsd>tac.
voi[1].
y[fi]) tac.
voi[1].
y[fi]=minsd;
256 printf(
"Frame\t\t\tMean\tSD\n");
257 for(fi=0; fi<img.
dimt; fi++)
258 printf(
"%d\t%g\t%g\t%g\t%g\n",
259 1+fi, tac.
x1[fi], tac.
x2[fi], tac.
voi[0].
y[fi], tac.
voi[1].
y[fi]);
263 if(verbose>1) printf(
"writing %s\n", efile);
266 fprintf(stderr,
"Error in writing '%s': %s\n", efile,
dfterrmsg);
269 if(verbose>0) printf(
"Mean TAC and noise SD written in %s\n", efile);
276 if(verbose>1) printf(
"adding noise to image\n");
279 for(fi=0; fi<img.
dimt; fi++) {
280 if(verbose>0 && img.
dimt>1) {fprintf(stdout,
"."); fflush(stdout);}
281 for(pi=0; pi<img.
dimz; pi++)
282 for(ri=0; ri<img.
dimy; ri++)
283 for(ci=0; ci<img.
dimx; ci++)
286 if(verbose>0 && img.
dimt>1) {fprintf(stdout,
"\n"); fflush(stdout);}
292 if(verbose>1) printf(
"writing noisy image %s\n", outfile);
294 fprintf(stderr,
"Error: %s\n", img.
statmsg);
305 if(verbose>0) printf(
"done.\n");
int backupExistingFile(char *filename, char *backup_ext, char *status)
double atof_dpi(char *str)
int dftSetmem(DFT *data, int frameNr, int voiNr)
int dftWrite(DFT *data, char *filename)
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
char * hlCorrectIsotopeCode(char *isocode)
double hlFromIsotope(char *isocode)
int imgExistentTimes(IMG *img)
unsigned long long imgNaNs(IMG *img, int fix)
void imgEmpty(IMG *image)
char * imgIsotope(IMG *img)
int imgSetDecayCorrFactors(IMG *image, int mode)
int imgAverageTAC(IMG *img, float *tac)
int imgRead(const char *fname, IMG *img)
int imgWrite(const char *fname, IMG *img)
char * imgUnit(int dunit)
Header file for libtpccurveio.
#define DFT_DECAY_CORRECTED
#define DFT_TIME_STARTEND
Header file for libtpcimgio.
Header file for libtpcimgp.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Header file for libtpcmodel.
Header file for libtpcmodext.
int noiseSD4SimulationFromDFT(DFT *dft, int index, double pc, double *sd, char *status, int verbose)
char unit[MAX_UNITS_LEN+1]
char voiname[MAX_REGIONSUBNAME_LEN+1]
char name[MAX_REGIONNAME_LEN+1]