TPCCLIB
Loading...
Searching...
No Matches
imgdysmo.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 "Smoothing of dynamic PET image in ECAT 6.3 and 7, Analyze 7.5, and NIfTI",
27 "format.",
28 "Replaces pixel TACs with an average TAC of up to 5x5x5 neighbouring pixels",
29 "which are selected based on the MRL (maximum run length) and AUC difference.",
30 "Note that before application in data analysis, the quantitativity of",
31 "the regional results must be validated.",
32 " ",
33 "Usage: @P [Options] dynamic_image smoothed_image",
34 " ",
35 "Options:",
36 " -m=<3|5>",
37 " Set filter mask to 3x3x3 or 5x5x5 pixels (default).",
38 " -s=<1|2|3|4|5>",
39 " Set smoothing strength: 1=min, 2=default, 5=max.",
40 " -a=<nr>",
41 " Set the nr of pixels to average inside smoothing mask;",
42 " by default this is set automatically based on the mask size.",
43 " -stdoptions", // List standard options like --help, -v, etc
44 " ",
45 "Example 1.",
46 " @P b123dy1.v b123dy1smoothed.v",
47 " ",
48 "See also: imgthrs, imgbkgrm, imgfsegm, imgfiltg, fvar4img",
49 " ",
50 "Keywords: image, smoothing, modelling",
51 0};
52/*****************************************************************************/
53
54/*****************************************************************************/
55/* Turn on the globbing of the command line, since it is disabled by default in
56 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
57 In Unix&Linux wildcard command line processing is enabled by default. */
58/*
59#undef _CRT_glob
60#define _CRT_glob -1
61*/
62int _dowildcard = -1;
63/*****************************************************************************/
64
65/*****************************************************************************/
69int main(int argc, char **argv)
70{
71 int ai, help=0, version=0, verbose=1;
72 int ret=0, maskSize=5, smoothing=2, avgNr=0;
73 char imgfile[FILENAME_MAX], smofile[FILENAME_MAX], tmp[FILENAME_MAX];
74 char *cptr;
75 IMG img, smoimg;
76
77
78 /*
79 * Get arguments
80 */
81 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
82 imgfile[0]=smofile[0]=(char)0;
83 imgInit(&img); imgInit(&smoimg);
84 /* Options */
85 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
86 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
87 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
88 cptr=argv[ai]+1;
89 if(strncasecmp(cptr, "M=", 2)==0) {
90 cptr+=2; maskSize=atoi(cptr); if(maskSize==3 || maskSize==5) continue;
91 } else if(strncasecmp(cptr, "S=", 2)==0) {
92 cptr+=2; smoothing=atoi(cptr); if(smoothing>0 && smoothing<6) continue;
93 } else if(strncasecmp(cptr, "A=", 2)==0) {
94 cptr+=2; avgNr=atoi(cptr); if(avgNr>1) continue;
95 }
96 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
97 return(1);
98 } else break;
99
100 /* Print help or version? */
101 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
102 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
103 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
104
105 /* Process other arguments, starting from the first non-option */
106 for(; ai<argc; ai++) {
107 if(!imgfile[0]) {
108 strcpy(imgfile, argv[ai]); continue;
109 } else if(!smofile[0]) {
110 strcpy(smofile, argv[ai]); continue;
111 }
112 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
113 return(1);
114 }
115
116 /* Is something missing? */
117 if(!smofile[0]) {
118 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
119 argv[0]);
120 return(1);
121 }
122
123 /* In verbose mode print arguments and options */
124 if(verbose>1) {
125 printf("imgfile := %s\n", imgfile);
126 printf("smofile := %s\n", smofile);
127 printf("maskSize := %d\n", maskSize);
128 printf("smoothing=%d\n", smoothing);
129 printf("avgNr := %d\n", avgNr);
130 }
131
132
133 /*
134 * Read dynamic image
135 */
136 if(verbose>=0) fprintf(stdout, "reading dynamic image %s\n", imgfile);
137 if(imgRead(imgfile, &img)) {
138 fprintf(stderr, "Error: %s\n", img.statmsg);
139 if(verbose>1) imgInfo(&img);
140 imgEmpty(&img);
141 return(2);
142 }
143 if(verbose>10) imgInfo(&img);
144 /* Check that image is image */
145 if(img.type!=IMG_TYPE_IMAGE) {
146 fprintf(stderr, "Error: raw PET data cannot be smoothed with this program.\n");
147 imgEmpty(&img); return(1);
148 }
149 if(imgNaNs(&img, 1)>0)
150 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
151 /* Check that MRL and frame nr are ok */
152 if(img.dimt<2) {
153 fprintf(stderr, "Error: image has only one time frame.\n");
154 imgEmpty(&img); return(1);
155 }
156 /* Check that plane nr is ok */
157 if(img.dimz<3) {
158 fprintf(stderr, "Warning: smoothing would work better for images with more planes.\n");
159 }
160
161
162 /*
163 * Calculate the smoothed image
164 */
165 /* Set avgNr if necessary */
166 if(avgNr<2) switch(maskSize) {
167 case 3:
168 switch(smoothing) {
169 case 1: avgNr=3; break;
170 case 2: avgNr=5; break;
171 case 3: avgNr=7; break;
172 case 4: avgNr=9; break;
173 case 5: avgNr=11; break;
174 default: avgNr=5; break;
175 }
176 break;
177 case 5:
178 default:
179 switch(smoothing) {
180 case 1: avgNr=5; break;
181 case 2: avgNr=9; break;
182 case 3: avgNr=15; break;
183 case 4: avgNr=25; break;
184 case 5: avgNr=35; break;
185 default: avgNr=9; break;
186 }
187 break;
188 } else { /* Check that avgNr is not set to stupid values by the user */
189 ret=maskSize*maskSize*maskSize/2; if(avgNr>ret) avgNr=ret;
190 }
191 if(verbose>1) {
192 printf("Average of %d best pixels inside %dx%dx%d matrix.\n",
193 avgNr, maskSize, maskSize, maskSize);
194 }
195 if(verbose>=0) printf("Calculating smoothed image\n");
196 ret=imgsegmSimilar(&img, maskSize, avgNr, &smoimg, verbose-1);
197 if(ret) {
198 fprintf(stderr, "Error (%d): cannot smooth.\n", ret);
199 imgEmpty(&img); imgEmpty(&smoimg); return(4);
200 }
201
202 /* Save the smoothed image */
203 if(verbose>=0) printf("writing smoothed image %s\n", smofile);
204 backupExistingFile(smofile, NULL, tmp);
205 if(imgWrite(smofile, &smoimg)) {
206 fprintf(stderr, "Error: %s\n", smoimg.statmsg);
207 imgEmpty(&img); imgEmpty(&smoimg);
208 return(11);
209 }
210
211 /*
212 * Free up memory
213 */
214 imgEmpty(&img); imgEmpty(&smoimg);
215
216 if(verbose>=0) fprintf(stdout, "done.\n");
217 return(0);
218}
219/*****************************************************************************/
220
221/*****************************************************************************/
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 imgsegmSimilar(IMG *input, int smoothDim, int smoothNr, IMG *output, int verbose)
Definition imgsegm.c:478
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
unsigned short int dimz
const char * statmsg