TPCCLIB
Loading...
Searching...
No Matches
maskdila.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 "Dilate mask image in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
25 "Original mask image file is overwritten if name for new file is not given.",
26 " ",
27 "Usage: @P [Options] maskfile [newmaskfile]",
28 " ",
29 "Options:",
30 " -struct=<<cube>|<rcube>|<star>>",
31 " Select the structuring element for dilation; either 3x3x3 cube (default),",
32 " rounded cube (3x3x3 cube without corners), or star (cube on its corner,",
33 " consisting of 7 voxels).",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "See also: maskeros, imgmask, masksize, imgthrs, imgmax, imgbox",
37 " ",
38 "Keywords: image, mask, dilation, threshold",
39 0};
40/*****************************************************************************/
41
42/*****************************************************************************/
43/* Turn on the globbing of the command line, since it is disabled by default in
44 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
45 In Unix&Linux wildcard command line processing is enabled by default. */
46/*
47#undef _CRT_glob
48#define _CRT_glob -1
49*/
50int _dowildcard = -1;
51/*****************************************************************************/
52
53/*****************************************************************************/
57int main(int argc, char **argv)
58{
59 int ai, help=0, version=0, verbose=1;
60 int ret;
61 char maskfile[FILENAME_MAX], outfile[FILENAME_MAX];
62 int structuring_element=1; // 1=cube, 2=rcube, 3=star
63 char *cptr=NULL;
64 unsigned int mn;
65
66
67 /*
68 * Get arguments
69 */
70 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
71 maskfile[0]=outfile[0]=(char)0;
72 /* Options */
73 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
74 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 if(strncasecmp(cptr, "STRUCT=", 7)==0) {
77 cptr+=7;
78 if(strncasecmp(cptr, "CUBE", 1)==0) {structuring_element=1; continue;}
79 if(strncasecmp(cptr, "RCUBE", 1)==0) {structuring_element=2; continue;}
80 if(strncasecmp(cptr, "STAR", 1)==0) {structuring_element=3; continue;}
81 }
82 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
83 return(1);
84 } else break;
85
86 /* Print help or version? */
87 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
88 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
89 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
90
91 /* Process other arguments, starting from the first non-option */
92 if(ai<argc) {strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
93 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
94 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
95
96 /* Did we get all the information that we need? */
97 if(!maskfile[0]) {
98 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
99 return(1);
100 }
101
102
103 /* In verbose mode print options */
104 if(verbose>1) {
105 printf("maskfile := %s\n", maskfile);
106 if(outfile[0]) printf("outfile := %s\n", outfile);
107 printf("structuring_element := %d\n", structuring_element);
108 fflush(stdout);
109 }
110
111 /* If file name for output was not given, the overwrite input */
112 if(!outfile[0]) strcpy(outfile, maskfile);
113
114
115 /*
116 * Read mask
117 */
118 IMG mask; imgInit(&mask);
119
120 if(verbose>0) {printf("reading %s\n", maskfile); fflush(stdout);}
121 ret=imgRead(maskfile, &mask);
122 if(ret) {
123 fprintf(stderr, "Error: %s\n", mask.statmsg);
124 if(verbose>1) printf("ret := %d\n", ret);
125 return(2);
126 }
127 if(mask.dimt>1) {
128 fprintf(stderr, "Error: mask cannot be dynamic image.\n");
129 imgEmpty(&mask); return(2);
130 }
131 /* Check whether mask has any voxels to begin with */
132 mn=imgMaskCount(&mask);
133 if(mn==0) {
134 fprintf(stderr, "Warning: initial mask contains no positive voxels.\n");
135 imgEmpty(&mask);
136 return(0);
137 }
138 if(mn==(unsigned int)(mask.dimz*mask.dimy*mask.dimx)) {
139 fprintf(stderr, "Warning: initial mask contains all positive voxels.\n");
140 imgEmpty(&mask);
141 return(0);
142 }
143 if(verbose>1) {
144 printf("initial_nr_of_positive_voxels := %ud\n", mn);
145 }
146
147
148 /*
149 * Make the structuring element (dimensions must be odd)
150 */
151 IMG selem; imgInit(&selem);
152 ret=imgStructuringElement(&selem, structuring_element, verbose-1);
153 if(ret!=0) {
154 fprintf(stderr, "Error: cannot make structuring element.\n");
155 imgEmpty(&mask); imgEmpty(&selem);
156 return(5);
157 }
158
159
160 /*
161 * Dilate the mask
162 */
163 if(verbose>0) {printf("dilating\n"); fflush(stdout);}
164 ret=imgMaskDilate(&mask, &selem);
165 if(ret<0) {
166 fprintf(stderr, "Error: cannot dilate the mask.\n");
167 if(verbose>1) printf("ret := %d\n", -ret);
168 imgEmpty(&mask); imgEmpty(&selem);
169 return(8);
170 }
171 if(ret==0) {
172 fprintf(stdout, "No voxels dilated.\n");
173 imgEmpty(&mask); imgEmpty(&selem);
174 return(0);
175 }
176 if(verbose>0) {
177 fprintf(stdout, "%d mask voxel(s) dilated.\n", ret);
178 fflush(stdout);
179 }
180 /* Check the number of masked voxels */
181 mn=imgMaskCount(&mask);
182 if(mn==0) {
183 fprintf(stderr, "Warning: empty mask, not saved.\n");
184 imgEmpty(&mask); imgEmpty(&selem);
185 return(0);
186 }
187 if(verbose>1) {
188 printf("nr_of_positive_voxels := %u\n", mn);
189 }
190 if(mn==(unsigned int)(mask.dimz*mask.dimy*mask.dimx)) {
191 fprintf(stderr, "Warning: dilated mask contains all positive voxels.\n");
192 }
193 imgEmpty(&selem);
194
195 /*
196 * Save the modified mask
197 */
198 if(verbose>2) printf("writing mask\n");
199 ret=imgWrite(outfile, &mask);
200 if(ret) {
201 fprintf(stderr, "Error: %s\n", mask.statmsg);
202 imgEmpty(&mask); return(11);
203 }
204 imgEmpty(&mask);
205 if(verbose>0) printf("dilated mask written in %s.\n\n", outfile);
206
207 return(0);
208}
209/*****************************************************************************/
210
211/*****************************************************************************/
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
Header file for libtpcimgio.
Header file for libtpcimgp.
long long imgMaskCount(IMG *img)
Definition mask.c:15
int imgStructuringElement(IMG *img, const int structuring_element, int verbose)
Definition mask.c:126
int imgMaskDilate(IMG *img, IMG *se)
Definition mask.c:80
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
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg