TPCCLIB
Loading...
Searching...
No Matches
masklbl.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 "Region labelling (1) for mask image in ECAT 6.3 or 7.x, NIfTI-1, or",
25 "Analyze 7.5 format.",
26 "All pixels with value < 0.1 are considered as background pixels; other",
27 "pixels are considered as foreground pixels, and tagged with values 2, 3, ...",
28 "Notice that mask image file will be overwritten by labelled mask file.",
29 " ",
30 "Usage: @P [Options] maskfile",
31 " ",
32 "Options:",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "References:",
36 "1. Burger W and Burge MJ: Principles of Digital Image Processing - Core",
37 " Algorithms, Springer, 2009, DOI 10.1007/978-1-84800-195-4.",
38 " ",
39 "See also: masksize, imgthrs, maskinv, maskconj, imgmask",
40 " ",
41 "Keywords: image, mask, ROI",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 int ret, n;
64 char maskfile[FILENAME_MAX];
65//char *cptr=NULL;
66
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 maskfile[0]=(char)0;
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 //cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
77 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
78 return(1);
79 } else break;
80
81 /* Print help or version? */
82 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
83 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
84 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
85
86 /* Process other arguments, starting from the first non-option */
87 if(ai<argc) {strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
88 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
89
90 /* Did we get all the information that we need? */
91 if(!maskfile[0]) {
92 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
93 return(1);
94 }
95
96
97 /* In verbose mode print options */
98 if(verbose>1) {
99 printf("maskfile := %s\n", maskfile);
100 fflush(stdout);
101 }
102
103
104 /*
105 * Read mask
106 */
107 IMG mask; imgInit(&mask);
108
109 if(verbose>0) {printf("reading %s\n", maskfile); fflush(stdout);}
110 ret=imgRead(maskfile, &mask);
111 if(ret) {
112 fprintf(stderr, "Error: %s\n", mask.statmsg);
113 if(verbose>1) printf("ret := %d\n", ret);
114 return(2);
115 }
116 if(mask.dimt>1) {
117 fprintf(stderr, "Error: mask cannot be dynamic image.\n");
118 imgEmpty(&mask); return(2);
119 }
120
121
122 /*
123 * Region labelling
124 */
125 if(verbose>0) {printf("labelling regions...\n"); fflush(stdout);}
126 IMG mask2; imgInit(&mask2);
127 ret=imgMaskRegionLabeling(&mask, &mask2, &n, verbose-2);
128 imgEmpty(&mask);
129 if(ret!=0) {
130 fprintf(stderr, "Error: cannot label regions.\n");
131 if(verbose>1) printf("ret := %d\n", ret);
132 imgEmpty(&mask2);
133 return(6);
134 }
135 if(verbose>0) printf("region_nr := %d\n", n);
136 if(n==0) {
137 fprintf(stdout, "Warning: mask file was empty.\n");
138 imgEmpty(&mask2);
139 return(0);
140 }
141
142
143 /*
144 * Save the modified mask
145 */
146 if(verbose>2) printf("writing labelled mask...\n");
147 ret=imgWrite(maskfile, &mask2);
148 if(ret) {
149 fprintf(stderr, "Error: %s\n", mask2.statmsg);
150 imgEmpty(&mask2); return(11);
151 }
152 imgEmpty(&mask2);
153 if(verbose>0) printf("labelled mask written.\n\n");
154
155 return(0);
156}
157/*****************************************************************************/
158
159/*****************************************************************************/
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.
int imgMaskRegionLabeling(IMG *mask1, IMG *mask2, int *n, int verbose)
Definition mask.c:279
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