10#include "tpcclibConfig.h"
27static char *info[] = {
28 "Find peak value and time in dynamic PET image.",
29 "Static image containing the peak value of each pixel TAC is written.",
31 "Usage: @P [Options] imgfile [peakfile]",
35 " Save a template image containing the frame numbers (1..frame_nr)",
36 " where the maximum pixel values were found.",
38 " Save TAC file, containing the frame times (sec) on x-axis, and on",
39 " y-axis the means (and mean-SD and mean+SD) of the peak values of",
40 " each pixel that had its peak at this frame time.",
42 " Save the peak time (sec) of each pixel (middle time of peak frame).",
44 " Save the peak time (sec) of each pixel, calculated as weighted",
45 " average of 3 or 5 subsequent frames.",
47 " Save the value-weighted mean time (sec) of all frames.",
48 " Mean residence time (MRT) in PK for PTACs uses the same equation.",
50 " Use only pixels with peak value above (threshold/100 x max peak);",
53 " Maximum pixel values are divided by the frame average.",
56 "See also: tacpeak, imgmax, imgaumc, imgmask, imgthrs, imgfrsmo, imgdelfr",
58 "Keywords: image, peak, max, time, mask",
77int main(
int argc,
char **argv)
79 int ai, help=0, version=0, verbose=1;
80 char imgfile[FILENAME_MAX], peakfile[FILENAME_MAX], framefile[FILENAME_MAX],
81 mptfile[FILENAME_MAX], timefile[FILENAME_MAX], wtfile[FILENAME_MAX], ctfile[FILENAME_MAX];
83 float calcThreshold=0.05;
89 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
90 imgfile[0]=peakfile[0]=framefile[0]=mptfile[0]=timefile[0]=wtfile[0]=ctfile[0]=(char)0;
92 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
93 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
95 if(strncasecmp(cptr,
"FRAME=", 6)==0) {
96 if(
strlcpy(framefile, cptr+6, FILENAME_MAX)>1)
continue;
97 }
else if(strncasecmp(cptr,
"MPT=", 4)==0) {
98 if(
strlcpy(mptfile, cptr+4, FILENAME_MAX)>1)
continue;
99 }
else if(strncasecmp(cptr,
"TIME=", 5)==0) {
100 if(
strlcpy(timefile, cptr+5, FILENAME_MAX)>1)
continue;
101 }
else if(strncasecmp(cptr,
"WT=", 3)==0) {
102 if(
strlcpy(wtfile, cptr+3, FILENAME_MAX)>1)
continue;
103 }
else if(strncasecmp(cptr,
"CT=", 3)==0) {
104 if(
strlcpy(ctfile, cptr+3, FILENAME_MAX)>1)
continue;
105 }
else if(strncasecmp(cptr,
"NORMALIZED", 4)==0) {
106 max_normalized=1;
continue;
107 }
else if(strncasecmp(cptr,
"THR=", 4)==0) {
109 if(!
atof_with_check(cptr+4, &v) && v<100.0) {calcThreshold=(float)(0.01*v);
continue;}
111 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
116 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
121 if(ai<argc) {
strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
122 if(ai<argc) {
strlcpy(peakfile, argv[ai], FILENAME_MAX); ai++;}
123 if(ai<argc) {fprintf(stderr,
"Error: too many arguments.\n");
return(1);}
127 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
133 printf(
"imgfile := %s\n", imgfile);
134 if(peakfile[0]) printf(
"peakfile := %s\n", peakfile);
135 if(timefile[0]) printf(
"timefile := %s\n", timefile);
136 if(wtfile[0]) printf(
"wtfile := %s\n", wtfile);
137 if(ctfile[0]) printf(
"ctfile := %s\n", ctfile);
138 if(mptfile[0]) printf(
"mptfile := %s\n", mptfile);
139 if(framefile[0]) printf(
"framefile := %s\n", framefile);
140 printf(
"max_normalized := %d\n", max_normalized);
141 printf(
"threshold_ratio := %g\n", calcThreshold);
148 if(verbose>0) printf(
"reading dynamic image %s\n", imgfile);
151 fprintf(stderr,
"Error: %s\n", img.
statmsg);
155 printf(
"img_dimx := %d\n", img.
dimx);
156 printf(
"img_dimy := %d\n", img.
dimy);
157 printf(
"img_dimz := %d\n", img.
dimz);
158 printf(
"img_dimt := %d\n", img.
dimt);
161 fprintf(stderr,
"Error: %s contains only 1 time frame.\n", imgfile);
165 if(verbose>0) fprintf(stderr,
"Warning: missing pixel values.\n");
169 if(timefile[0] || wtfile[0] || ctfile[0]) timesNeeded=1;
171 fprintf(stderr,
"Error: image does not contain frame times.\n");
179 if(verbose>0) printf(
"searching for peak frames\n");
182 fprintf(stderr,
"Error: cannot search for peaks.\n");
190 if(verbose>0) printf(
"making peak value image\n");
192 if(
imgDup(&pfimg, &pvimg)) {
193 fprintf(stderr,
"Error: cannot allocate memory.\n");
197 for(
int zi=0; zi<img.
dimz; zi++) {
198 for(
int yi=0; yi<img.
dimy; yi++) {
199 for(
int xi=0; xi<img.
dimx; xi++) {
200 int fi=(int)roundf(pfimg.
m[zi][yi][xi][0]);
201 if(fi>0) pvimg.
m[zi][yi][xi][0]=img.
m[zi][yi][xi][fi-1];
202 else pvimg.
m[zi][yi][xi][0]=0.0;
207 if(verbose>1) printf(
"making thresholding mask\n");
209 if(
imgMax(&pvimg, &maxPeak)) {
210 fprintf(stderr,
"Error: cannot find maximum peak value.\n");
214 if(verbose>0) printf(
"max_peak_value := %g\n", maxPeak);
220 fprintf(stderr,
"Error: cannot create thresholding mask image.\n");
224 if(verbose>1) printf(
"%lld pixels are over threshold limit.\n", thrNr);
226 fprintf(stderr,
"Error: no pixels above threshold level.\n");
232 if(verbose>1) printf(
"thresholding\n");
241 if(verbose>1) printf(
"writing peak frames\n");
243 fprintf(stderr,
"Error: %s\n", pfimg.
statmsg);
247 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", framefile);
253 if(peakfile[0] && max_normalized) {
254 if(verbose>1) fprintf(stdout,
"computing mean of pixels\n");
256 fprintf(stderr,
"Error: cannot calculate average TAC.\n");
261 printf(
"Mean TAC:\n");
262 for(
int i=0; i<img.
dimt; i++)
263 printf(
"\t%g\t%g\n", img.
mid[i], img.
sd[i]);
265 if(verbose>1) printf(
"normalizing peak values\n");
266 for(
int zi=0; zi<img.
dimz; zi++) {
267 for(
int yi=0; yi<img.
dimy; yi++) {
268 for(
int xi=0; xi<img.
dimx; xi++) {
269 int fi=(int)roundf(pfimg.
m[zi][yi][xi][0]);
270 if(fi>0 && img.
sd[fi-1]>1.0E-010) pvimg.
m[zi][yi][xi][0]/=img.
sd[fi-1];
276 if(verbose>1) printf(
"writing peak values\n");
278 fprintf(stderr,
"Error: %s\n", pvimg.
statmsg);
282 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", peakfile);
292 if(verbose>0) printf(
"searching for peak times\n");
295 fprintf(stderr,
"Error: cannot search for peaks.\n");
299 if(verbose>1) printf(
"writing peak times\n");
301 fprintf(stderr,
"Error: %s\n", timg.
statmsg);
305 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", timefile);
314 if(verbose>0) printf(
"computing weighted time\n");
317 fprintf(stderr,
"Error: cannot search for peaks.\n");
321 if(verbose>1) printf(
"writing weighted times\n");
323 fprintf(stderr,
"Error: %s\n", timg.
statmsg);
327 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", ctfile);
336 if(verbose>0) printf(
"computing weighted time\n");
339 fprintf(stderr,
"Error: cannot search for peaks.\n");
343 if(verbose>1) printf(
"writing weighted peak times\n");
345 fprintf(stderr,
"Error: %s\n", timg.
statmsg);
349 if(verbose>0) fprintf(stdout,
"Image %s saved.\n", wtfile);
359 if(verbose>1) fprintf(stdout,
"calculating mean TAC of peak value versus peak time\n");
363 fprintf(stderr,
"Error: cannot allocate memory.\n");
378 double *peaks=(
double*)malloc(img.
dimz*img.
dimy*img.
dimx*
sizeof(
double));
380 fprintf(stderr,
"Error: out of memory.\n");
385 for(
int fi=0; fi<img.
dimt; fi++) {
386 if(verbose>3) printf(
"frame %d\n", 1+fi);
388 for(
int zi=0; zi<pfimg.
dimz; zi++) {
389 for(
int yi=0; yi<pfimg.
dimy; yi++) {
390 for(
int xi=0; xi<pfimg.
dimx; xi++) {
391 if((
int)roundf(pfimg.
m[zi][yi][xi][0])!=(fi+1))
continue;
393 peaks[pn++]=img.
m[zi][yi][xi][fi];
398 if(verbose>3) printf(
" %d peaks on this frame\n", pn);
401 double mmean=
dmean(peaks, pn, &msd);
402 if(verbose>3) printf(
" %g +- %g\n", mmean, msd);
413 fprintf(stderr,
"Error in writing '%s': %s\n", mptfile,
dfterrmsg);
417 if(verbose>0) fprintf(stdout,
"peak TAC written in %s\n", mptfile);
int atof_with_check(char *double_as_string, double *result_value)
int dftSetmem(DFT *data, int frameNr, int voiNr)
int dftWrite(DFT *data, char *filename)
int imgExistentTimes(IMG *img)
unsigned long long imgNaNs(IMG *img, int fix)
int imgDup(IMG *img1, IMG *img2)
void imgEmpty(IMG *image)
int imgAverageMaskTAC(IMG *img, IMG *timg, float *tac)
int imgRead(const char *fname, IMG *img)
int imgWrite(const char *fname, IMG *img)
int imgGetMaxTime(IMG *img, IMG *mimg, const int w, int verbose)
int imgGetMaxFrame(IMG *img, IMG *mimg, int verbose)
int imgMax(IMG *img, float *maxvalue)
int imgThresholdMaskCount(IMG *img, float minValue, float maxValue, IMG *timg, long long *count)
int imgThresholdByMask(IMG *img, IMG *templt, float thrValue)
char * imgUnit(int dunit)
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.
double dmean(double *data, int n, double *sd)
char unit[MAX_UNITS_LEN+1]
char voiname[MAX_REGIONSUBNAME_LEN+1]
char name[MAX_REGIONNAME_LEN+1]