TPCCLIB
Loading...
Searching...
No Matches
maskeros.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 "Erode 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 erosion; 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: maskdila, imgmask, imgthrs, imgmax, imgbox",
37 " ",
38 "Keywords: image, mask, erosion, 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(verbose>1) {
139 printf("initial_nr_of_positive_voxels := %u\n", mn);
140 fflush(stdout);
141 }
142
143
144 /*
145 * Make the structuring element (dimensions must be odd)
146 */
147 IMG selem; imgInit(&selem);
148 ret=imgStructuringElement(&selem, structuring_element, verbose-1);
149 if(ret!=0) {
150 fprintf(stderr, "Error: cannot make structuring element.\n");
151 imgEmpty(&mask); imgEmpty(&selem);
152 return(5);
153 }
154
155
156 /*
157 * Erode the mask
158 */
159 if(verbose>0) {printf("eroding\n"); fflush(stdout);}
160 ret=imgMaskErode(&mask, &selem);
161 if(ret<0) {
162 fprintf(stderr, "Error: cannot erode the mask.\n");
163 if(verbose>1) printf("ret := %d\n", -ret);
164 imgEmpty(&mask); imgEmpty(&selem);
165 return(8);
166 }
167 if(ret==0) {
168 fprintf(stdout, "No voxels eroded.\n");
169 imgEmpty(&mask); imgEmpty(&selem);
170 return(0);
171 }
172 if(verbose>0) {
173 fprintf(stdout, "%d mask voxel(s) eroded.\n", ret);
174 fflush(stdout);
175 }
176 /* Check whether mask has any voxels left */
177 mn=imgMaskCount(&mask);
178 if(mn==0) {
179 fprintf(stderr, "Warning: all voxels eroded; empty mask not saved.\n");
180 imgEmpty(&mask); imgEmpty(&selem);
181 return(0);
182 }
183 if(verbose>1) {
184 printf("nr_of_positive_voxels := %u\n", mn);
185 }
186 imgEmpty(&selem);
187
188
189 /*
190 * Save the modified mask
191 */
192 if(verbose>2) printf("writing mask\n");
193 ret=imgWrite(outfile, &mask);
194 if(ret) {
195 fprintf(stderr, "Error: %s\n", mask.statmsg);
196 imgEmpty(&mask); return(11);
197 }
198 imgEmpty(&mask);
199 if(verbose>0) printf("eroded mask written in %s.\n\n", outfile);
200
201 return(0);
202}
203/*****************************************************************************/
204
205/*****************************************************************************/
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 imgMaskErode(IMG *img, IMG *se)
Definition mask.c:34
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 dimt
const char * statmsg