00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045
00046
00047
00048 #include "libdenoising.h"
00049 #include "io_png.h"
00050
00073
00074
00075 int main(int argc, char **argv) {
00076
00077
00078 if (argc < 5) {
00079 printf("usage: nlmeans_ipol image sigma noisy denoised \n");
00080 exit(-1);
00081 }
00082
00083
00084
00085 size_t nx,ny,nc;
00086 float *d_v = NULL;
00087 d_v = io_png_read_f32(argv[1], &nx, &ny, &nc);
00088 if (!d_v) {
00089 printf("error :: %s not found or not a correct png image \n", argv[1]);
00090 exit(-1);
00091 }
00092
00093
00094
00095
00096
00097 int d_w = (int) nx;
00098 int d_h = (int) ny;
00099 int d_c = (int) nc;
00100 if (d_c == 2) {
00101 d_c = 1;
00102 }
00103 if (d_c > 3) {
00104 d_c = 3;
00105 }
00106
00107 int d_wh = d_w * d_h;
00108 int d_whc = d_c * d_w * d_h;
00109
00110
00111
00112
00113 if (d_c > 1) {
00114
00115
00116 int i=0;
00117 while (i < d_wh && d_v[i] == d_v[d_wh + i] && d_v[i] == d_v[2 * d_wh + i ]) {
00118 i++;
00119 }
00120
00121 if (i == d_wh) d_c = 1;
00122
00123 }
00124
00125
00126
00127
00128 float fSigma = atof(argv[2]);
00129 float *noisy = new float[d_whc];
00130
00131 for (int i=0; i < d_c; i++) {
00132 fiAddNoise(&d_v[i * d_wh], &noisy[i * d_wh], fSigma, i, d_wh);
00133 }
00134
00135
00136
00137
00138
00139
00140 float **fpI = new float*[d_c];
00141 float **fpO = new float*[d_c];
00142 float *denoised = new float[d_whc];
00143
00144 for (int ii=0; ii < d_c; ii++) {
00145
00146 fpI[ii] = &noisy[ii * d_wh];
00147 fpO[ii] = &denoised[ii * d_wh];
00148
00149 }
00150
00151
00152
00153
00154 int bloc, win;
00155 float fFiltPar;
00156
00157 if (d_c == 1) {
00158
00159 if (fSigma > 0.0f && fSigma <= 15.0f) {
00160 win = 1;
00161 bloc = 10;
00162 fFiltPar = 0.4f;
00163
00164 } else if ( fSigma > 15.0f && fSigma <= 30.0f) {
00165 win = 2;
00166 bloc = 10;
00167 fFiltPar = 0.4f;
00168
00169 } else if ( fSigma > 30.0f && fSigma <= 45.0f) {
00170 win = 3;
00171 bloc = 17;
00172 fFiltPar = 0.35f;
00173
00174 } else if ( fSigma > 45.0f && fSigma <= 75.0f) {
00175 win = 4;
00176 bloc = 17;
00177 fFiltPar = 0.35f;
00178
00179 } else if (fSigma <= 100.0f) {
00180
00181 win = 5;
00182 bloc = 17;
00183 fFiltPar = 0.30f;
00184
00185 } else {
00186 printf("error :: algorithm parametrized only for values of sigma less than 100.0\n");
00187 exit(-1);
00188 }
00189
00190
00191
00192
00193
00194 } else {
00195
00196
00197 if (fSigma > 0.0f && fSigma <= 25.0f) {
00198 win = 1;
00199 bloc = 10;
00200 fFiltPar = 0.55f;
00201
00202 } else if (fSigma > 25.0f && fSigma <= 55.0f) {
00203 win = 2;
00204 bloc = 17;
00205 fFiltPar = 0.4f;
00206
00207 } else if (fSigma <= 100.0f) {
00208 win = 3;
00209 bloc = 17;
00210 fFiltPar = 0.35f;
00211
00212 } else {
00213 printf("error :: algorithm parametrized only for values of sigma less than 100.0\n");
00214 exit(-1);
00215 }
00216
00217
00218
00219 }
00220
00221
00222
00223 nlmeans_ipol(win, bloc, fSigma, fFiltPar, fpI, fpO, d_c, d_w, d_h);
00224
00225
00226
00227 if (io_png_write_f32(argv[3], noisy, (size_t) d_w, (size_t) d_h, (size_t) d_c) != 0) {
00228 printf("... failed to save png image %s", argv[3]);
00229 }
00230
00231 if (io_png_write_f32(argv[4], denoised, (size_t) d_w, (size_t) d_h, (size_t) d_c) != 0) {
00232 printf("... failed to save png image %s", argv[4]);
00233 }
00234
00235
00236
00237
00238 }
00239
00240
00241