8#include "tpcclibConfig.h"
25static char *info[] = {
26 "Calculate the slope of pixel TACs in dynamic PET image.",
27 "Slope is calculated between given start and end times (min).",
28 "Static image containing the slope value of each pixel TAC is written.",
30 "Usage: @P [Options] imgfile starttime endtime slopefile",
34 " Instead of LSQ regression line, the slope is determined as median",
35 " of slopes between any two data points.",
37 " Change the sign of slopes.",
39 " Pixels with negative slope (optionally after sign change) are set",
43 "See also: imgcalc, imgpeak, imgaumc, imgledif, imgthrs",
45 "Keywords: image, dynamics, input, mask",
64int main(
int argc,
char **argv)
66 int ai, help=0, version=0, verbose=1;
67 char imgfile[FILENAME_MAX], slopefile[FILENAME_MAX];
68 double v, tstart=0.0, tstop=0.0;
77 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
78 imgfile[0]=slopefile[0]=(char)0;
80 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
81 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
83 if(strcasecmp(cptr,
"MEDIAN")==0) {
85 }
else if(strcasecmp(cptr,
"SIGN")==0) {
87 }
else if(strncasecmp(cptr,
"NONEGATIVES", 5)==0) {
90 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
95 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
100 if(ai<argc) {
strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
103 if(ai<argc) {
strlcpy(slopefile, argv[ai], FILENAME_MAX); ai++;}
104 if(ai<argc) {fprintf(stderr,
"Error: too many arguments.\n");
return(1);}
108 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
114 printf(
"imgfile := %s\n", imgfile);
115 printf(
"tstart := %g\n", tstart);
116 printf(
"tstop := %g\n", tstop);
117 printf(
"slopefile := %s\n", slopefile);
118 printf(
"mode := %d\n", mode);
119 printf(
"noneg := %d\n", noneg);
127 if(verbose>0) printf(
"reading dynamic image %s\n", imgfile);
130 fprintf(stderr,
"Error: %s\n", img.
statmsg);
134 printf(
"img_dimx := %d\n", img.
dimx);
135 printf(
"img_dimy := %d\n", img.
dimy);
136 printf(
"img_dimz := %d\n", img.
dimz);
137 printf(
"img_dimt := %d\n", img.
dimt);
141 fprintf(stderr,
"Error: %s contains only 1 time frame.\n", imgfile);
145 if(verbose>0) fprintf(stderr,
"Warning: missing pixel values.\n");
149 fprintf(stderr,
"Error: image does not contain frame times.\n");
155 tstop=img.
end[img.
dimt-1]/60.0;
156 if(verbose>0) {printf(
"tstop set to %g min\n", tstop); fflush(stdout);}
158 tstart*=60.0; tstop*=60.0;
160 int fstart=img.
dimt-1;
162 for(
int i=img.
dimt-1; i>=0; i--)
if(img.
mid[i]>=tstart) fstart=i;
else break;
163 for(
int i=0; i<img.
dimt; i++)
if(img.
mid[i]<=tstop) fstop=i;
else break;
165 printf(
"fstart := %d\n", fstart);
166 printf(
"fstop := %d\n", fstop);
168 int sampleNr=1+fstop-fstart;
170 fprintf(stderr,
"Error: invalid time range.\n");
178 if(verbose>1) printf(
"allocating memory for parametric image\n");
181 fprintf(stderr,
"Error: cannot allocate IMG data.\n");
187 sout.
unit=CUNIT_UNITLESS;
194 double x[sampleNr], y[sampleNr], a, b;
195 for(
int i=0; i<sampleNr; i++) x[i]=img.
mid[i+fstart];
196 for(
int zi=0; zi<img.
dimz; zi++) {
197 for(
int yi=0; yi<img.
dimy; yi++)
for(
int xi=0; xi<img.
dimx; xi++) {
199 sout.
m[zi][yi][xi][0]=0.0;
201 for(
int i=0; i<sampleNr; i++) y[i]=img.
m[zi][yi][xi][i+fstart];
209 if(noneg && !(a>=0.0)) a=0.0;
210 if(ret==0) sout.
m[zi][yi][xi][0]=60.0*(float)a;
220 if(verbose>1) printf(
"writing slopes\n");
222 fprintf(stderr,
"Error: %s\n", sout.
statmsg);
226 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", slopefile);
int atof_with_check(char *double_as_string, double *result_value)
int imgExistentTimes(IMG *img)
unsigned long long imgNaNs(IMG *img, int fix)
int imgAllocateWithHeader(IMG *image, int planes, int rows, int columns, int frames, IMG *image_from)
void imgEmpty(IMG *image)
int imgRead(const char *fname, IMG *img)
int imgWrite(const char *fname, IMG *img)
Header file for libtpccurveio.
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)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
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.
int medianline(double *x, double *y, int nr, double *slope, double *ic)
int regr_line(double *x, double *y, int n, double *m, double *c)