TPCCLIB
Loading...
Searching...
No Matches
imgfrdyn.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <string.h>
14#include <unistd.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcimgio.h"
19#include "libtpcimgp.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Estimate the dynamics in PET image by counting the numbers of frames where",
25 "pixel values are increasing or decreasing as compared to the previous frame.",
26 "An image containing number of increases minus number of decreases is saved.",
27 " ",
28 "Usage: @P [Options] imgfile dynimgfile",
29 " ",
30 "Options:",
31 " -dmi | -decr | -incr",
32 " Instead of the default operation, calculate the number of decreases",
33 " minus increases (-dmi), number of increases (-incr), or number of",
34 " decreases (-decr).",
35 " -noneg",
36 " Negative numbers are set to zero.",
37 " -stdoptions", // List standard options like --help, -v, etc
38 " ",
39 "See also: imgfrdif, imgledif, imgpeak, imgmask, imgthrs, img2tif",
40 " ",
41 "Keywords: image, dynamics, time, mask",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
64 int mode=0; // 0=incr-decr, 1=decr-incr, 2=incr, 3=decr
65 int workNegatives=0; // 0=leave, 1=set to zero
66 int ret;
67
68
69 /*
70 * Get arguments
71 */
72 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
73 imgfile[0]=outfile[0]=(char)0;
74 /* Options */
75 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
76 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
77 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
78 if(strcasecmp(cptr, "IMD")==0) {
79 mode=0; continue;
80 } else if(strcasecmp(cptr, "DMI")==0) {
81 mode=1; continue;
82 } else if(strcasecmp(cptr, "INCR")==0) {
83 mode=2; continue;
84 } else if(strcasecmp(cptr, "DECR")==0) {
85 mode=3; continue;
86 } else if(strncasecmp(cptr, "NONEGATIVES", 5)==0) {
87 workNegatives=1; continue;
88 }
89 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
90 return(1);
91 } else break;
92
93 /* Print help or version? */
94 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
95 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
96 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
97
98 /* Process other arguments, starting from the first non-option */
99 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
100 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
101 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
102
103 /* Is something missing or wrong? */
104 if(!outfile[0]) {
105 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
106 return(1);
107 }
108
109 /* In verbose mode print arguments and options */
110 if(verbose>1) {
111 printf("imgfile := %s\n", imgfile);
112 printf("outfile := %s\n", outfile);
113 printf("mode := %d\n", mode);
114 printf("workNegatives := %d\n", workNegatives);
115 fflush(stdout);
116 }
117
118
119 /*
120 * Read dynamic image
121 */
122 if(verbose>0) {printf("reading dynamic image %s\n", imgfile); fflush(stdout);}
123 IMG img; imgInit(&img);
124 ret=imgRead(imgfile, &img);
125 if(ret) {
126 fprintf(stderr, "Error: %s\n", img.statmsg);
127 if(verbose>1) printf("ret := %d\n", ret);
128 return(2);
129 }
130 if(img.dimt<2) {
131 fprintf(stderr, "Error: %s contains only 1 time frame.\n", imgfile);
132 imgEmpty(&img); return(2);
133 }
134 if(verbose>0) {
135 fprintf(stdout, " image contains %d frames and %d planes.\n", img.dimt, img.dimz);
136 fflush(stdout);
137 }
138 if(imgNaNs(&img, 1)>0)
139 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
140
141
142 /*
143 * Calculate the numbers of increasing and decreasing frames
144 */
145 if(verbose>0) {printf("computing frame differences\n"); fflush(stdout);}
146 IMG iimg; imgInit(&iimg);
147 IMG dimg; imgInit(&dimg);
148 ret=imgGetFrameDyn(&img, &iimg, &dimg, verbose-1);
149 imgEmpty(&img);
150 if(ret) {
151 fprintf(stderr, "Error: cannot compute frame differences.\n");
152 imgEmpty(&dimg); imgEmpty(&iimg);
153 return(4);
154 }
155 /* Based on the mode, calculate final number */
156 if(mode==0) {
157 if(verbose>1) {printf("computing increases minus decreases\n"); fflush(stdout);}
158 for(int zi=0; zi<dimg.dimz; zi++)
159 for(int yi=0; yi<dimg.dimy; yi++)
160 for(int xi=0; xi<dimg.dimx; xi++)
161 iimg.m[zi][yi][xi][0]-=dimg.m[zi][yi][xi][0];
162 } else if(mode==1) {
163 if(verbose>1) {printf("computing decreases minus increases\n"); fflush(stdout);}
164 for(int zi=0; zi<dimg.dimz; zi++)
165 for(int yi=0; yi<dimg.dimy; yi++)
166 for(int xi=0; xi<dimg.dimx; xi++)
167 iimg.m[zi][yi][xi][0]=dimg.m[zi][yi][xi][0]-iimg.m[zi][yi][xi][0];
168 } else if(mode==3) {
169 for(int zi=0; zi<dimg.dimz; zi++)
170 for(int yi=0; yi<dimg.dimy; yi++)
171 for(int xi=0; xi<dimg.dimx; xi++)
172 iimg.m[zi][yi][xi][0]=dimg.m[zi][yi][xi][0];
173 }
174 imgEmpty(&dimg);
175
176 /* If requested, set negative difference to zero */
177 if(workNegatives==1) imgCutoff(&iimg, 0.0, 1);
178
179 /*
180 * Save result image
181 */
182 if(verbose>1) {printf("writing result image\n"); fflush(stdout);}
183 ret=imgWrite(outfile, &iimg);
184 if(ret) {
185 fprintf(stderr, "Error: %s\n", iimg.statmsg);
186 imgEmpty(&iimg);
187 return(11);
188 }
189 if(verbose>0) fprintf(stdout, "Image %s saved.\n", outfile);
190
191 imgEmpty(&iimg);
192 return(0);
193}
194/*****************************************************************************/
195
196/*****************************************************************************/
unsigned long long imgNaNs(IMG *img, int fix)
Definition img.c:658
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
int imgGetFrameDyn(IMG *img, IMG *iimg, IMG *dimg, int verbose)
Definition imgframe.c:249
void imgCutoff(IMG *image, float cutoff, int mode)
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)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
unsigned short int dimx
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg