30 #define DISPLAY_SCALING 255
33 #define LAMBDA_TUNE_ITERATIONS 5
36 #define JPEGQUALITY 95
39 #define IMAGEIO_NUM (IMAGEIO_SINGLE)
41 #define IMAGEIO_NUM (IMAGEIO_DOUBLE)
81 "Total variation regularized denoising IPOL demo, P. Getreuer, 2012\n\n"
82 "Syntax: tvdenoise [options] <noisy> <denoised>\n");
83 puts(
"where <noisy> and <denoised> are "
86 "Either lambda (the fidelty strength) or sigma (the noise standard\n"
87 "deviation) should be specified. If sigma is specified, then lambda is\n"
88 "selected automatically using Chambolle's algorithm.\n");
90 puts(
" -n <model> Specify noise model, where <model> is\n"
91 " gaussian Additive white Gaussian noise\n"
92 " Y[n] ~ Normal(X[n], sigma^2)");
93 puts(
" laplace Laplace noise\n"
94 " Y[n] ~ Laplace(X[n], sigma/sqrt(2))\n"
95 " poisson Poisson noise\n"
96 " Y[n] ~ Poisson(X[n]/a) a\n"
97 " where a = 255 sigma^2 / (mean X)");
98 puts(
" -n <model>:<sigma> Specify sigma, the noise standard deviation");
99 puts(
" -l <number> Specify lambda, the fidelity stength\n");
101 puts(
" -q <number> Quality for saving JPEG images (0 to 100)\n");
104 " tvdenoise -n laplace:10 noisy.bmp denoised.bmp\n");
110 static int ParseParams(
programparams *Param,
int argc,
char *argv[]);
113 int main(
int argc,
char **argv)
116 image f = {NULL, 0, 0, 0}, u = {NULL, 0, 0, 0};
120 if(!ParseParams(&Param, argc, argv))
125 IMAGEIO_RGB | IMAGEIO_PLANAR | IMAGEIO_NUM)))
132 if(!(u.Data = (num *)
Malloc(
sizeof(num) * ((
size_t)f.
Width)
142 ((u.NumChannels == 1) ? IMAGEIO_GRAYSCALE : IMAGEIO_RGB)
178 if(!strcmp(Model,
"gaussian"))
179 Lambda = 0.7079 / Sigma + 0.002686 / (Sigma * Sigma);
180 else if(!strcmp(Model,
"laplace"))
181 Lambda = (-0.00416*Sigma + 0.001301)
182 / (((Sigma - 0.2042)*Sigma + 0.01635)*Sigma + 5.836e-4);
183 else if(!strcmp(Model,
"poisson"))
184 Lambda = 0.2839 / Sigma + 0.001502 / (Sigma * Sigma);
187 fprintf(stderr,
"Unrecognized noise model \"%s\"\n", Model);
196 printf(
"Tuning lambda...\n\n");
198 printf(
" --------------------\n");
199 printf(
" %-9.4f", Lambda);
209 fprintf(stderr,
"Error in computation.\n");
215 if(!strcmp(Model,
"laplace"))
216 Lambda *= sqrt(Rmse/Sigma);
218 Lambda *= Rmse/Sigma;
244 fprintf(stderr,
"Memory allocation failed\n");
249 fprintf(stderr,
"Unrecognized noise model \"%s\"\n", Model);
253 printf(
"TV regularized denoising with %c%s noise model\n",
254 toupper(*Model), Model + 1);
273 fprintf(stderr,
"Error in computation.\n");
293 for(n = 0; n < N; n++)
306 const long NumPixels = ((long)f.
Width) * ((long)f.
Height);
307 const num *Red = f.
Data;
308 const num *Green = f.
Data + NumPixels;
309 const num *Blue = f.
Data + 2*NumPixels;
312 for(n = 0; n < NumPixels; n++)
313 if(Red[n] != Green[n] || Red[n] != Blue[n])
320 static int ParseParams(
programparams *Param,
int argc,
char *argv[])
322 char *OptionString, *Sigma;
335 Param->
Model =
"gaussian";
340 for(i = 1; i < argc;)
342 if(argv[i] && argv[i][0] ==
'-')
344 if((OptionChar = argv[i][1]) == 0)
351 OptionString = &argv[i][2];
353 OptionString = argv[i];
363 Sigma = strchr(OptionString,
':');
364 Param->
Model = OptionString;
366 if((Sigma = strchr(OptionString,
':')))
369 Param->
Sigma = (num)(atof(Sigma + 1)
372 if(Param->
Sigma <= 0)
380 Param->
Lambda = (num)atof(OptionString);
388 #ifdef LIBJPEG_SUPPORT
394 ErrorMessage(
"JPEG quality must be between 0 and 100.\n");
403 if(isprint(OptionChar))
432 ErrorMessage(
"Either sigma or lambda must be specified.\n");