TPCCLIB
Loading...
Searching...
No Matches
imgfrsmo.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 <time.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpcmodel.h"
18#include "libtpccurveio.h"
19#include "libtpcimgio.h"
20#include "libtpcimgp.h"
21#include "libtpcmodext.h"
22/*****************************************************************************/
23
24/*****************************************************************************/
25static char *info[] = {
26 "Simple smoothing over time frames for dynamic PET image in ECAT 6.3 and 7,",
27 "Analyze 7.5, and NIfTI format.",
28 "Replaces pixel TACs with a frame duration weighted average TAC.",
29 "Note that this program can only be used for visualization purposes.",
30 " ",
31 "Usage: @P [Options] dynamic_image smoothed_image",
32 " ",
33 "Options:",
34 " -n=<nr of frames to average>",
35 " Enter an odd number; by default 3.",
36 " -stdoptions", // List standard options like --help, -v, etc
37 " ",
38 "Example 1.",
39 " @P -n=5 b123dy1.v b123dy1smoothed.v",
40 " ",
41 "See also: imgdysmo, imgthrs, imgbkgrm, imgfsegm, imgpeak, fvar4img",
42 " ",
43 "Keywords: image, smoothing, modelling",
44 0};
45/*****************************************************************************/
46
47/*****************************************************************************/
48/* Turn on the globbing of the command line, since it is disabled by default in
49 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
50 In Unix&Linux wildcard command line processing is enabled by default. */
51/*
52#undef _CRT_glob
53#define _CRT_glob -1
54*/
55int _dowildcard = -1;
56/*****************************************************************************/
57
58/*****************************************************************************/
62int main(int argc, char **argv)
63{
64 int ai, help=0, version=0, verbose=1;
65 int ret=0, avgNr=3;
66 char imgfile[FILENAME_MAX], smofile[FILENAME_MAX], tmp[FILENAME_MAX];
67 char *cptr;
68
69
70 /*
71 * Get arguments
72 */
73 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
74 imgfile[0]=smofile[0]=(char)0;
75 /* Options */
76 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
77 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
78 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
79 cptr=argv[ai]+1;
80 if(strncasecmp(cptr, "N=", 2)==0) {
81 cptr+=2; avgNr=atoi(cptr); if(avgNr>2) continue;
82 }
83 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
84 return(1);
85 } else break;
86
87 /* Print help or version? */
88 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
89 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
90 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
91
92 /* Process other arguments, starting from the first non-option */
93 for(; ai<argc; ai++) {
94 if(!imgfile[0]) {
95 strcpy(imgfile, argv[ai]); continue;
96 } else if(!smofile[0]) {
97 strcpy(smofile, argv[ai]); continue;
98 }
99 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
100 return(1);
101 }
102
103 /* Is something missing? */
104 if(!smofile[0]) {
105 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
106 return(1);
107 }
108 /* Check that frame number is odd */
109 if((avgNr%2)==0) {
110 fprintf(stderr, "Error: even number given with option -n.\n");
111 return(1);
112 }
113
114 /* In verbose mode print arguments and options */
115 if(verbose>1) {
116 printf("imgfile := %s\n", imgfile);
117 printf("smofile := %s\n", smofile);
118 printf("avgNr := %d\n", avgNr);
119 }
120
121
122 /*
123 * Read dynamic image
124 */
125 if(verbose>=0) fprintf(stdout, "reading dynamic image %s\n", imgfile);
126 IMG img;
127 imgInit(&img);
128 if(imgRead(imgfile, &img)) {
129 fprintf(stderr, "Error: %s\n", img.statmsg);
130 if(verbose>1) imgInfo(&img);
131 imgEmpty(&img);
132 return(2);
133 }
134 if(verbose>10) imgInfo(&img);
135 /* Check that image is image */
136 if(img.type!=IMG_TYPE_IMAGE) {
137 fprintf(stderr, "Error: raw PET data cannot be smoothed with this program.\n");
138 imgEmpty(&img); return(1);
139 }
140 /* Check that frame nr is ok */
141 if(img.dimt<avgNr) {
142 fprintf(stderr, "Error: too few image frames for smoothing.\n");
143 imgEmpty(&img); return(1);
144 }
145 if(imgNaNs(&img, 1)>0)
146 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
147
148
149 /*
150 * Calculate the smoothed image
151 */
152 if(verbose>=0) printf("Calculating smoothed image\n");
153 ret=imgSmoothOverFrames(&img, avgNr);
154 if(ret) {
155 fprintf(stderr, "Error: cannot smooth.\n");
156 imgEmpty(&img); return(4);
157 }
158
159 /* Save the smoothed image */
160 if(verbose>=0) printf("writing smoothed image %s\n", smofile);
161 backupExistingFile(smofile, NULL, tmp);
162 if(imgWrite(smofile, &img)) {
163 fprintf(stderr, "Error: %s\n", img.statmsg);
164 imgEmpty(&img);
165 return(11);
166 }
167
168 /*
169 * Free up memory
170 */
171 imgEmpty(&img);
172
173 if(verbose>=0) fprintf(stdout, "done.\n");
174 return(0);
175}
176/*****************************************************************************/
177
178/*****************************************************************************/
int backupExistingFile(char *filename, char *backup_ext, char *status)
Definition backup.c:14
void imgInfo(IMG *image)
Definition img.c:359
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 imgSmoothOverFrames(IMG *img, int n)
Definition imgframe.c:136
Header file for libtpccurveio.
Header file for libtpcimgio.
#define IMG_TYPE_IMAGE
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
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
Header file for libtpcmodel.
Header file for libtpcmodext.
char type
unsigned short int dimt
const char * statmsg