TPCCLIB
Loading...
Searching...
No Matches
imgfrdif.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 "Compute the pixel value difference between consecutive frames to get a rough",
25 "estimate on variance on each pixel.",
26 "Program writes an image containing sum of |frame[i+1]-frame[i]|.",
27 " ",
28 "Usage: @P [Options] imgfile difimgfile",
29 " ",
30 "Options:",
31 " -norm[alize]",
32 " Differences are divided by the average of frames,",
33 " |frame[i+1]+frame[i]|/2.",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "See also: imgpeak, imgmask, imgthrs, imgfrsmo, imgdelfr, imgledif, img2tif",
37 " ",
38 "Keywords: image, dynamics, noise, time, mask",
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 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
61 int dif_normalized=0;
62 int ret;
63 char *cptr;
64
65
66 /*
67 * Get arguments
68 */
69 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
70 imgfile[0]=outfile[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
73 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
74 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
75 if(strncasecmp(cptr, "NORMALIZED", 4)==0) {
76 dif_normalized=1; continue;
77 }
78 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
79 return(1);
80 } else break;
81
82 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 /* Process other arguments, starting from the first non-option */
88 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
89 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
90 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
91
92 /* Is something missing or wrong? */
93 if(!outfile[0]) {
94 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
95 return(1);
96 }
97
98 /* In verbose mode print arguments and options */
99 if(verbose>1) {
100 printf("imgfile := %s\n", imgfile);
101 printf("outfile := %s\n", outfile);
102 printf("dif_normalized := %d\n", dif_normalized);
103 fflush(stdout);
104 }
105
106
107 /*
108 * Read dynamic image
109 */
110 if(verbose>0) {printf("reading dynamic image %s\n", imgfile); fflush(stdout);}
111 IMG img; imgInit(&img);
112 ret=imgRead(imgfile, &img);
113 if(ret) {
114 fprintf(stderr, "Error: %s\n", img.statmsg);
115 if(verbose>1) printf("ret := %d\n", ret);
116 return(2);
117 }
118 if(img.dimt<2) {
119 fprintf(stderr, "Error: %s contains only 1 time frame.\n", imgfile);
120 imgEmpty(&img); return(2);
121 }
122 if(verbose>0) {
123 fprintf(stdout, " image contains %d frames and %d planes.\n", img.dimt, img.dimz);
124 fflush(stdout);
125 }
126 if(imgNaNs(&img, 1)>0)
127 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
128
129
130 /*
131 * Calculate the difference image
132 */
133 if(verbose>0) {printf("computing frame differences\n"); fflush(stdout);}
134 IMG dimg; imgInit(&dimg);
135 IMG mimg; imgInit(&mimg);
136 if(dif_normalized==0) ret=imgGetFrameDiff(&img, &dimg, NULL, verbose-1);
137 else ret=imgGetFrameDiff(&img, &dimg, &mimg, verbose-1);
138 imgEmpty(&img);
139 if(ret) {
140 fprintf(stderr, "Error: cannot compute frame differences.\n");
141 imgEmpty(&dimg); imgEmpty(&mimg);
142 return(4);
143 }
144 /* Normalize */
145 if(dif_normalized) {
146 if(verbose>1) {printf("normalizing difference image\n"); fflush(stdout);}
147 int zi, yi, xi;
148 for(zi=0; zi<dimg.dimz; zi++)
149 for(yi=0; yi<dimg.dimy; yi++)
150 for(xi=0; xi<dimg.dimx; xi++) {
151 if(mimg.m[zi][yi][xi][0]>1.0E-10)
152 dimg.m[zi][yi][xi][0]/=mimg.m[zi][yi][xi][0];
153 else
154 dimg.m[zi][yi][xi][0]=0.0;
155 }
156 }
157
158 /*
159 * Save result image
160 */
161 if(verbose>1) {printf("writing result image\n"); fflush(stdout);}
162 ret=imgWrite(outfile, &dimg);
163 if(ret) {
164 fprintf(stderr, "Error: %s\n", mimg.statmsg);
165 imgEmpty(&dimg); imgEmpty(&mimg);
166 return(11);
167 }
168 if(verbose>0) fprintf(stdout, "Image %s saved.\n", outfile);
169
170 imgEmpty(&dimg); imgEmpty(&mimg);
171 return(0);
172}
173/*****************************************************************************/
174
175/*****************************************************************************/
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 imgGetFrameDiff(IMG *img, IMG *dimg, IMG *mimg, int verbose)
Definition imgframe.c:180
Header file for libtpcimgio.
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
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
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg