8#include "tpcclibConfig.h"
24static char *info[] = {
25 "Cut off PET image (in ECAT, NIfTI, or Analyze format) pixel values that are",
26 "outside given range of values.",
27 "Original image file is modified, if file name for output is not given.",
29 "Usage: @P [Options] imgfile lowerlimit upperlimit [outputfile]",
33 " Set pixel value to zero if original value is outside of the range;",
34 " by default value is set to the limit.",
36 " Set pixel value to one or zero, depending whether original value is",
37 " inside or outside of the range.",
39 " Optional output file is written even in the case that no pixel values",
40 " fell outside of limits; by default image is saved only if contents were",
44 "Lower and upper limit can be given directly as values on the command line,",
45 "or as names of files containing only the value, or value with key 'lower' or",
46 "'upper', respectively.",
48 "See also: imgthrs, imgmask, imgqntls, imginteg, imgunit",
50 "Keywords: image, threshold, mask",
69int main(
int argc,
char **argv)
71 int ai, help=0, version=0, verbose=1;
72 char petfile[FILENAME_MAX], outfile[FILENAME_MAX];
73 float lowerThreshold=+1.0, upperThreshold=-1.0;
84 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
85 petfile[0]=outfile[0]=(char)0;
87 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
89 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
90 if(strncasecmp(cptr,
"ZERO", 1)==0) {
92 }
else if(strncasecmp(cptr,
"MASK", 1)==0) {
94 }
else if(strcasecmp(cptr,
"F")==0 || strcasecmp(cptr,
"FORCE")==0) {
95 forceMode=1;
continue;
97 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
102 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
107 if(ai<argc) {
strlcpy(petfile, argv[ai], FILENAME_MAX); ai++;}
114 fprintf(stderr,
"Error: invalid lower limit '%s'.\n", argv[ai]);
120 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n", argv[0]);
129 fprintf(stderr,
"Error: invalid lower limit '%s'.\n", argv[ai]);
135 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n", argv[0]);
138 if(ai<argc) {
strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
139 if(ai<argc) {fprintf(stderr,
"Error: too many arguments.\n");
return(1);}
144 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n", argv[0]);
147 if(lowerThreshold>upperThreshold) {
148 fprintf(stderr,
"Error: invalid value range.\n");
151 if(lowerThreshold==upperThreshold) {
154 f=0.001*lowerThreshold; lowerThreshold-=f; upperThreshold+=f;
155 if(verbose>1) printf(
"Threshold range: %g - %g\n", lowerThreshold, upperThreshold);
158 if(!outfile[0]) strcpy(outfile, petfile);
160 if(maskMode) zeroMode=0;
165 printf(
"petfile := %s\n", petfile);
166 if(outfile[0]) printf(
"outfile := %s\n", outfile);
167 printf(
"upper_limit := %g\n", upperThreshold);
168 printf(
"lower_limit := %g\n", lowerThreshold);
169 printf(
"zeroMode := %d\n", zeroMode);
170 printf(
"maskMode := %d\n", maskMode);
178 if(verbose>0) printf(
"reading %s\n", petfile);
181 fprintf(stderr,
"Error: %s\n", img.
statmsg);
182 if(verbose>1) printf(
"ret=%d\n", ret);
190 if(verbose>0) fprintf(stdout,
"verifying pixel values...\n");
194 long long nr, total_nr=0;
195 if(verbose>1) printf(
"Nr of cut-off pixels:\n");
196 for(fi=0; fi<img.
dimt; fi++) {
197 for(zi=0; zi<img.
dimz; zi++) {
199 for(yi=0; yi<img.
dimy; yi++) {
200 for(xi=0; xi<img.
dimx; xi++) {
201 if(!isfinite(img.
m[zi][yi][xi][fi]) || img.
m[zi][yi][xi][fi]<lowerThreshold) {
202 if(zeroMode==0) img.
m[zi][yi][xi][fi]=lowerThreshold;
else img.
m[zi][yi][xi][fi]=0.0;
205 if(!isfinite(img.
m[zi][yi][xi][fi]) || img.
m[zi][yi][xi][fi]>upperThreshold) {
206 if(zeroMode==0) img.
m[zi][yi][xi][fi]=upperThreshold;
else img.
m[zi][yi][xi][fi]=0.0;
211 if(verbose>1 && img.
dimz>1 && img.
dimt>1)
212 printf(
" frame %d plane %d: %lld\n", fi+1, zi+1, nr);
216 if(verbose>0) printf(
"cutoff_pixels := %lld\n", total_nr);
221 fprintf(stderr,
"Warning: no pixels thresholded.\n");
223 fprintf(stderr,
"Note: no pixels thresholded, image not saved.\n");
231 for(
int fi=0; fi<img.
dimt; fi++) {
232 for(
int zi=0; zi<img.
dimz; zi++) {
233 for(
int yi=0; yi<img.
dimy; yi++) {
234 for(
int xi=0; xi<img.
dimx; xi++) {
235 if(!isfinite(img.
m[zi][yi][xi][fi]) || img.
m[zi][yi][xi][fi]<lowerThreshold) {
236 img.
m[zi][yi][xi][fi]=0.0;
continue;}
237 if(!isfinite(img.
m[zi][yi][xi][fi]) || img.
m[zi][yi][xi][fi]>upperThreshold) {
238 img.
m[zi][yi][xi][fi]=0.0;
continue;}
239 img.
m[zi][yi][xi][fi]=1.0; nr++;
244 if(verbose>0) printf(
"retained_pixels := %lld\n", nr);
249 fprintf(stderr,
"Warning: no pixels retained.\n");
251 fprintf(stderr,
"Note: no pixels retained, image not saved.\n");
263 if(verbose>1) fprintf(stdout,
"writing %s\n", outfile);
265 fprintf(stderr,
"Error: %s\n", img.
statmsg);
266 if(verbose>1) printf(
"ret=%d\n", ret);
270 if(verbose>0) printf(
"Written %s\n", outfile);
int atof_with_check(char *double_as_string, double *result_value)
char * iftReadValue(char *filename, char *keystr, int verbose)
void imgEmpty(IMG *image)
int imgRead(const char *fname, IMG *img)
int imgWrite(const char *fname, IMG *img)
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 libtpcmodext.