TPCCLIB
Loading...
Searching...
No Matches
imgsd.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 <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpccsv.h"
18#include "tpctac.h"
19#include "tpcimage.h"
20#include "tpcstatist.h"
21/*****************************************************************************/
22
23/*****************************************************************************/
24static char *info[] = {
25 "Make a map of standard deviation across image frames.",
26 " ",
27 "Usage: @P [options] imgfile sdmap",
28 " ",
29 "Options:",
30 " -SNR",
31 " Calculate mean/SD map instead of SD map.",
32 " -dif",
33 " Calculate mean-SD map instead of SD map.",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "See also: fvar4img, imginteg, imgfiltg, imgdysmo",
37 " ",
38 "Keywords: image, noise",
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 imgfile[FILENAME_MAX], mapfile[FILENAME_MAX];
62 int mode=0; // 0=SD, 1=Mean/SD, 2=Mean-SD
63
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 imgfile[0]=mapfile[0]=(char)0;
70 /* Options */
71 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
72 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
73 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
74 if(strcasecmp(cptr, "SNR")==0) {
75 mode=1; continue;
76 } else if(strcasecmp(cptr, "SD")==0 || strcasecmp(cptr, "STD")==0) {
77 mode=0; continue;
78 } else if(strcasecmp(cptr, "DIF")==0) {
79 mode=2; continue;
80 }
81 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
82 return(1);
83 } else break;
84
85 /* Print help or version? */
86 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
87 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
88 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
89
90 /* Process other arguments, starting from the first non-option */
91 if(ai<argc) strlcpy(imgfile, argv[ai++], FILENAME_MAX);
92 if(ai<argc) strlcpy(mapfile, argv[ai++], FILENAME_MAX);
93 if(ai<argc) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
94 /* Is something missing? */
95 if(!mapfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
96
97 /* In verbose mode print arguments and options */
98 if(verbose>1) {
99 printf("imgfile := %s\n", imgfile);
100 printf("mapfile := %s\n", mapfile);
101 printf("mode := %d\n", mode);
102 fflush(stdout);
103 }
104
105 TPCSTATUS status; statusInit(&status);
106 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
107 status.verbose=verbose-1;
108
109
110
111 /*
112 * Read image data
113 */
114 if(verbose>1) {printf("reading %s\n", imgfile); fflush(stdout);}
115 IMG img; imgInit(&img);
116 ret=imgRead(&img, imgfile, &status);
117 if(ret!=TPCERROR_OK) { // error
118 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), imgfile); fflush(stderr);
119 return(2);
120 }
121 if(verbose>2) imgContents(&img, stdout);
122 if(img.dimt<2) {
123 fprintf(stderr, "Error: method is not applicable to static image.\n"); fflush(stderr);
124 return(2);
125 }
126 if(imgNaNs(&img, 1)>0)
127 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
128
129
130 /*
131 * Allocate memory for sd map
132 */
133 if(verbose>1) {printf("preparing SD map\n"); fflush(stdout);}
134 IMG map; imgInit(&map);
135 if(imgAllocate(&map, img.dimz, img.dimy, img.dimx, 1, &status)!=TPCERROR_OK) {
136 fprintf(stderr, "Error: invalid sample times in %s\n", imgfile); fflush(stderr);
137 imgFree(&img); return(3);
138 }
139 imgCopyHeader(&img, &map);
140
141
142 /*
143 * Pixel-by-pixel
144 */
145 long long pxlNr=img.dimx*img.dimy*img.dimz;
146 if(verbose>0) {printf("processing %llu image pixels\n", pxlNr); fflush(stdout);}
147 for(int zi=0; zi<img.dimz; zi++) {
148 if(img.dimz>2 && verbose>0) {fprintf(stdout, "."); fflush(stdout);}
149 for(int yi=0; yi<img.dimy; yi++) for(int xi=0; xi<img.dimx; xi++) {
150 map.m[zi][yi][xi][0]=0.0;
151 float mean, sd;
152 if(fstatMeanSD(img.m[zi][yi][xi], img.dimt, &mean, &sd, NULL)!=0) continue;
153 if(mode==0) {
154 map.m[zi][yi][xi][0]=sd;
155 } else if(mode==1) {
156 float snr=mean/sd;
157 if(isnormal(snr)) map.m[zi][yi][xi][0]=snr;
158 } else {
159 map.m[zi][yi][xi][0]=mean-sd;
160 }
161 }
162 }
163 if(img.dimz>2 && verbose>0) {fprintf(stdout, "\n"); fflush(stdout);}
164
165 /* Dynamic data not needed after this */
166 imgFree(&img);
167
168
169 /*
170 * Write the map
171 */
172 if(verbose>1) {printf("writing %s\n", mapfile); fflush(stdout);}
173 ret=imgWrite(&map, mapfile, &status);
174 if(ret!=TPCERROR_OK) {
175 fprintf(stderr, "Error: %s\n", errorMsg(status.error)); fflush(stderr);
176 imgFree(&map);
177 return(11);
178 }
179 if(verbose>0) {printf(" %s written.\n", mapfile); fflush(stdout);}
180
181 imgFree(&map);
182
183 return(0);
184}
185/*****************************************************************************/
186
187/*****************************************************************************/
void imgContents(IMG *img, FILE *fp)
Definition image.c:293
void imgFree(IMG *img)
Definition image.c:107
unsigned long long imgNaNs(IMG *img, int fix)
Definition image.c:373
void imgInit(IMG *img)
Definition image.c:64
int imgAllocate(IMG *img, const unsigned int dimz, const unsigned int dimy, const unsigned int dimx, const unsigned int dimt, TPCSTATUS *status)
Definition image.c:126
int imgCopyHeader(IMG *img1, IMG *img2)
Definition imageheader.c:18
int imgRead(IMG *img, const char *fname, TPCSTATUS *status)
Definition imageio.c:82
int imgWrite(IMG *img, const char *fname, TPCSTATUS *status)
Definition imageio.c:214
int fstatMeanSD(float *data, unsigned int n, float *mean, float *sd, unsigned int *vn)
Definition mean.c:73
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
Definition tpcimage.h:82
unsigned short int dimx
Definition tpcimage.h:112
float **** m
Definition tpcimage.h:161
unsigned short int dimt
Definition tpcimage.h:110
unsigned short int dimz
Definition tpcimage.h:116
unsigned short int dimy
Definition tpcimage.h:114
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpccsv.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for libtpcimage.
Header file for libtpcstatist.
Header file for library libtpctac.