-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_multi.cpp
1140 lines (963 loc) · 30 KB
/
algo_multi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* This file is part of the Ponomarenko Noise Estimation algorithm.
*
* Copyright(c) 2011 Miguel Colom.
*
* This file may be licensed under the terms of of the
* GNU General Public License Version 2 (the ``GPL'').
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the GPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the GPL along with this
* program. If not, go to http://www.gnu.org/licenses/gpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <vector>
#include <fftw3.h>
#include <iostream>
#include <assert.h>
#include <string>
#include <dirent.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include "algo.h"
#include "curve_filter.h"
//
#include "framework/CFramework.h"
#include "framework/CImage.h"
#include "framework/libparser.h"
#include "framework/operations.cpp"
#include "framework/CHistogram.cpp"
using namespace std;
//! A data structure to store the global id of a block within a sequence
// of images
struct BlockId
{
int image;
int block_origin;
int bin;
};
template <typename T>
void print_arr(const T* data, int len)
{
for (size_t i = 0; i < len; ++i) {
cout << data[i] << ", ";
}
cout << endl;
}
//! Computes the delta matrix and returns the normalization factor theta
/*!
\param *delta Delta matrix (mask for the low/high freqs in the block)
\param w Block side
\param T Number of low-freq coefficients, excluding DC
\return theta Normalization factor for the matrix delta
*/
int compute_delta(float *delta, int w, int T)
{
int theta = 0;
for (int j = 0; j < w; j++)
for (int i = 0; i < w; i++)
{
int value = (i + j < T && i + j != 0 ? 1 : 0);
delta[j * w + i] = value;
theta += value;
}
return theta;
}
//! Computes the set of variances computed form the low-frequency coefficients of the given blocks
/*!
\param *VL Output set of variances
\param M number of blocks taken into account
\param w Block side
\param *delta Delta matrix (mask for the low/high freqs in the block)s
\param **blocks_ptr List of pointers to the blocks
\param theta Normalization factor for the matrix delta
*/
void compute_VL(float *VL, int M, int w, float *delta, float **blocks_ptr,
int theta)
{
for (int m = 0; m < M; m++)
{
float *block = blocks_ptr[m];
VL[m] = 0;
for (int j = 0; j < w; j++)
{
for (int i = 0; i < w; i++)
if (delta[j * w + i] != 0)
VL[m] += pow(block[j * w + i], 2);
}
VL[m] /= theta;
}
}
//! Computes the set of variances computed from the high-frequency coefficients of the given blocks
/*!
\param *VH Output set of variances
\param **blocks_ptr List of pointers to the blocks
\param *indices_VL Sorting indices for the blocks_ptr list (by low-freqs)
\param w Block side
\param T Number of low-freq coefficients, excluding DC
\param K Number of blocks that should be used
\return Length of the returned variances list
*/
int compute_VH(float *VH, float **blocks_ptr, int *indices_VL, int w,
int T, int K)
{
int VH_count = 0;
//#pragma omp parallel for
for (int q = 0; q < w * w; q++)
{
int j = q / w;
int i = q - j * w;
if (i + j >= T)
{
float s = 0.0;
for (int k = 0; k < K; k++)
{
float *block = blocks_ptr[indices_VL[k]];
s += pow(block[q], 2); // q == j*w+i
}
VH[VH_count++] = s / K;
}
}
return VH_count;
}
//! Computes the optimal K parameter using Ponomarenko's original article loop
/*!
\param M Number of variance values in VL to use
\param *VL List of variances obtained for low-freq coefficients
\return The optimal K
*/
int get_optimal_K_ponom_orig(int M, float *VL)
{
int K = sqrt(M);
//
for (int i = 0; i < 7; i++)
{
float U = 1.3 * VL[K / 2];
int m_min = arg_find<float>(U, VL, M);
int K1 = m_min;
if (K1 > 0)
K = K1;
}
// Set K = K / 5 to provide robustness
int K1 = int(K / 5.0);
if (K1 > 0)
K = K1;
return K;
}
//! Return the optimal T parameter according to the given block side
/*!
\param w Block side
\return The optimal T parameter
*/
int get_T(int w)
{
switch (w)
{
case 3:
return 3;
case 4:
return 3;
case 5:
return 5;
case 7:
return 8;
case 8:
return 9;
case 11:
return 13;
case 15:
return 17;
case 21:
return 24;
default:
PRINT_ERROR("Unknown block side: %d\n", w);
exit(-1);
}
}
//! Build an index map from valid blocks to raw blocks of an image,
//! so that the origin of the i-th block in a valid block list corresponds to the valid_coords[i]-th
//! pixel in the raw image
/*!
\param *mask Input valid mask of an image, 0 for valid, 1 for invalid
\param **valid_coords pointer to output index map
\param Nx Length of a row in the image
\param Ny Length of a column in the image
\param w Block side
\param num_blocks Number of valid blocks
*/
void make_valid_coords(const int* mask, unsigned* valid_coords,
int Nx, int Ny) {
int count_coords = 0;
//
for (int i = 0; i < Nx*Ny; i++) {
if (mask[i] == 0) {
valid_coords[count_coords++] = i;
}
}
}
//! Reads all valid blocks (all neighbor pixels are different when the mask
//! is active) in the image
/*!
\param *D Output list of blocks
\param *u Input image
\param Nx Length of a row in the image
\param Ny Length of a column in the image
\param w Block side
\param num_blocks Number of blocks
\param *mask Mask to determine if a pixel is valid or not
\return Number of valid block copied into the output list
*/
void read_all_valid_blocks(float *D,
float *u,
int Nx, int Ny,
int w, unsigned num_blocks, int *mask) {
if (mask == NULL) {
const int w2 = w * w;
int q = 0;
for (int y = 0; y < Ny - w + 1; ++y) {
for (int x = 0; x < Nx - w + 1; ++x) {
for (int j = 0; j < w; ++j) {
for (int i = 0; i < w; ++i) {
D[q*w2+j*w+i] = u[(j+y)*Nx+i+x];
}
}
++q;
}
}
}
else {
unsigned* valid_coords = new unsigned[num_blocks];
make_valid_coords(mask, valid_coords, Nx, Ny);
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (unsigned q = 0; q < num_blocks; q++) {
int addr = valid_coords[q];
for (int j = 0; j < w; j++) {
for (int i = 0; i < w; i++) {
D[q*w*w+j*w+i] = u[j*Nx+i+addr];
}
}
}
delete[] valid_coords;
}
}
//! Computes the mean of all given blocks
/*!
\param *means Output list of means of blocks
\param *blocks Input list of blocks to compute their means
\param w Block side
\param num_blocks Number of block in the input list
*/
void compute_means(float *means, float *blocks, int w, int num_blocks)
{
const float ONE_DIV_w2 = 1.0 / (w * w);
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int b = 0; b < num_blocks; b++)
{
float mean = 0.0;
for (int p = 0; p < w * w; p++)
{
mean += blocks[b * w * w + p];
}
mean *= ONE_DIV_w2;
means[b] = mean;
}
}
int get_max(int *data, int N)
{
int max = data[0];
for (int i = 1; i < N; i++)
if (data[i] > max)
max = data[i];
return max;
}
void copy(float *dest, float *orig, int N)
{
for (int i = 0; i < N; i++)
dest[i] = orig[i];
}
//! Determines if the given string corresponds to the custom percentile code
/*!
\param *num_str Input string
\return true if the input string corresponds to the custom percentile code or false if not.
*/
bool is_custom_percentile(const char *num_str)
{
char buffer[1024];
float value = atof(num_str);
sprintf(buffer, "%.4f", value);
return strcmp(buffer, "0.0000") != 0;
}
//! Returns the mean of the data associated to the top K indices
/*!
\param mean_method Method to compute the mean (1: mean of means, 2: median of means)
\param K Number of selected elements for computing the mean
\param indices List of indices associated to the data
\param data raw data
\return The mean of the bin
*/
float get_top_K_mean(int mean_method, int K, int *indices,
float *data)
{
float mean;
float *values = new float[K + 1];
for (int i = 0; i <= K; i++)
values[i] = data[indices[i]];
switch (mean_method)
{
case 1:
{ // mean of means
mean = 0.0;
for (int i = 0; i <= K; i++)
{
mean += values[i];
}
mean /= (K + 1);
break;
}
case 2:
{ // median of means
mean = median<float>(values, K + 1);
break;
}
default:
{
PRINT_ERROR("Unknown mean method: %d\n", mean_method);
exit(-1);
}
}
delete[] values;
return mean;
}
//! In-place Normalization of the FFTW output in order to get a orthonormal 2D DCT-II
/*!
\param *blocks Input/output list of transformed blocks
\param w Block side
\param num_blocks Number of blocks in the list
*/
void normalize_FFTW(float *blocks, int w, int num_blocks)
{
const float ONE_DIV_2w = 1.0 / (2.0 * w);
const float ONE_DIV_SQRT_2 = 1.0 / sqrtf(2);
// Divide all coefficients by 2*w
//#pragma omp parallel for shared(blocks)
for (int i = 0; i < num_blocks * w * w; i++)
blocks[i] *= ONE_DIV_2w;
#ifdef _OPENMP
#pragma omp parallel for shared(blocks) schedule(static)
#endif
for (int b = 0; b < num_blocks; b++)
{
// {(i, j)} with i == 0 or j == 0
for (int j = 0; j < w; j++)
{
int i = 0;
blocks[b * w * w + j * w + i] *= ONE_DIV_SQRT_2;
}
for (int i = 0; i < w; i++)
{
int j = 0;
blocks[b * w * w + j * w + i] *= ONE_DIV_SQRT_2;
}
}
}
/**
* @brief Build a mask for invalid pixel. If mask(i, j) = true, the pixels will not be used.
*
* @param i_im : noisy image;
* @param o_mask : will contain the mask for all pixel in the image size;
* @param p_imSize : size of the image;
* @param p_sizePatch : size of a patch.
*
* @return number of valid blocks.
*
**/
unsigned buildMask(CImage &i_im, int *o_mask,
unsigned Nx, unsigned Ny, unsigned w,
unsigned num_channels)
{
unsigned count = 0;
for (unsigned ij = 0; ij < Nx * Ny; ij++)
{
const unsigned j = ij / Nx;
const unsigned i = ij - j * Nx;
//! Look if the pixel is not to close to the boundaries of the image
if (i < Nx - w + 1 && j < Ny - w + 1)
{
for (unsigned c = 0; c < num_channels; c++)
{
float *u = i_im.get_channel(c);
//! Look if the square 2x2 of pixels is constant
int invalid_pixel = (c == 0 ? 1 : o_mask[ij]);
// Try to validate pixel
if (fabs(u[ij] - u[ij + 1]) > 0.001f)
{
invalid_pixel = 0;
}
else if (fabs(u[ij + 1] - u[ij + Nx]) > 0.001f)
{
invalid_pixel = 0;
}
else if (fabs(u[ij + Nx] - u[ij + Nx + 1]) > 0.001f)
{
invalid_pixel = 0;
}
o_mask[ij] = invalid_pixel;
}
}
else
{
o_mask[ij] = 1; // Not valid
}
if (o_mask[ij] == 0)
count++;
}
return count;
}
/**
* @brief Build a mask for saturated (invalid) pixel. If mask(i, j) = true, the pixels will not be used.
*
* @param i_im : noisy image;
* @param o_mask : will contain the mask for all pixel in the image size;
*
* @return number of valid blocks.
*
**/
unsigned buildSaturatedMask(CImage &i_im, int *o_mask,
unsigned Nx, unsigned Ny, unsigned w,
unsigned num_channels) {
unsigned count = 0;
//! Get the maximum value of the channel
// float max_val = 0;
std::vector<float> max_vals(num_channels);
for (unsigned c = 0; c < num_channels; c++) {
for (unsigned ij = 0; ij < Nx*Ny; ij++) {
float *u = i_im.get_channel(c);
max_vals[c] = max_vals[c] < u[ij] ? u[ij] : max_vals[c];
}
}
memset(o_mask, 0, Nx*Ny*sizeof(*o_mask));
for (unsigned j = 0; j < Ny; ++j) {
for (unsigned i = 0; i < Nx; ++i) {
const unsigned ij = j * Nx + i;
for (unsigned c = 0; c < num_channels; c++) {
float *u = i_im.get_channel(c);
int invalid_pixel = (c == 0 ? 0 : o_mask[ij]);
//! Look if the pixel is saturated
o_mask[ij] = u[ij] >= max_vals[c] ? 1 : invalid_pixel;
}
}
}
for (unsigned j = 0; j < Ny; ++j) {
for (unsigned i = 0; i < Nx; ++i) {
const unsigned ij = j * Nx + i;
if (i < Nx - w + 1 && j < Ny - w + 1) {
unsigned accum = 0;
for (size_t ii = 0; ii < w; ++ii) {
for (size_t jj = 0; jj < w; ++jj) {
accum += o_mask[ij + ii*Nx + jj];
}
}
if (accum > 0) {
o_mask[ij] = 1;
} else {
++count;
}
} else {
o_mask[ij] = 1;
}
}
}
// for (unsigned ij = 0; ij < Nx*Ny; ij++) {
// const unsigned j = ij / Nx;
// const unsigned i = ij - j * Nx;
// if (i < Nx - w + 1 && j < Ny - w + 1) {
// unsigned accum = 0;
// for (int ii = 0; ii < w; ++ii) {
// for (int jj = 0; jj < w; ++jj) {
// accum += o_mask[ij + ii*Nx + jj];
// }
// }
// if (accum > 0) {
// o_mask[ij] = 1;
// } else {
// ++count;
// }
// } else {
// o_mask[ij] = 1;
// }
// }
return count;
}
/**
* @brief Build a mask of all pixels excluding right and bottom borders. If mask(i, j) = true, the pixels will not be used.
*
* @param o_mask : will contain the mask for all pixel in the image size;
*
* @return number of valid blocks.
*
**/
unsigned buildFullMask(int *o_mask,
unsigned Nx, unsigned Ny, unsigned w)
{
for (size_t ij = 0; ij < Nx*Ny; ++ij) {
const size_t j = ij / Nx;
const size_t i = ij - j * Nx;
o_mask[ij] = (i < Nx - w + 1 && j < Ny - w + 1) ? 0 : 1;
}
return (Nx - w + 1) * (Ny - w + 1);
}
/**
* @brief Get the names of all .png or .bmp files in the given directory
*
* @param path : directory path
*
* @return an array of file names
*
**/
std::vector<std::string> get_filenames(const std::string &path)
{
std::vector<std::string> files;
struct dirent *entry;
DIR *dir = opendir(path.c_str());
if (dir == NULL)
{
return files;
}
while ((entry = readdir(dir)) != NULL)
{
std::string fname(entry->d_name);
if (fname.size() > 4)
{
if (fname.substr(fname.size() - 4, fname.size()).compare(".png") == 0 ||
fname.substr(fname.size() - 4, fname.size()).compare(".bmp") == 0)
{
files.push_back(path + "/" + fname);
}
}
}
closedir(dir);
return files;
}
//! Computes the set of variances of all bins, computed from the high-frequency coefficients of the blocks
// from multiple images.
/*!
\param *VH Output sets of variances of all bins, of size num_bins*w*w
\param block_ids_selected List of pointers to the blocks
\param inputs List of pointers to the blocks
\param w Block side
\param T Number of low-freq coefficients, excluding DC
\param ch Channel of the computed variances
\return Length of the returned variances list
*/
int compute_VH(double *VH, const std::vector<BlockId> &block_ids_selected,
const std::vector<std::string> &inputs, int w, int T, int ch)
{
std::vector<std::vector<BlockId>> block_ids_by_image(inputs.size());
for (const BlockId block_id : block_ids_selected)
{
block_ids_by_image[block_id.image].push_back(block_id);
}
int VH_count = 0;
for (uint32_t image_idx = 0; image_idx < inputs.size(); ++image_idx)
{
PRINT_VERBOSE("Loading again %s for computing VH\n", inputs[image_idx].c_str());
// load image
CImage input;
input.load(inputs[image_idx].c_str());
int Nx = input.get_width();
// int Ny = input.get_height();
// Initialize blocks
const std::vector<BlockId> &block_ids = block_ids_by_image[image_idx];
uint32_t num_blocks = block_ids.size();
PRINT_VERBOSE("num_blocks: %d\n", num_blocks);
float *blocks = new float[num_blocks * w * w];
int nbTable[2] = {w, w};
int nembed[2] = {w, w};
#ifdef _OPENMP
fftwf_plan_with_nthreads(omp_get_num_procs());
#endif
fftwf_r2r_kind kindTable[2] = {FFTW_REDFT10, FFTW_REDFT10};
fftwf_plan fft_plan = fftwf_plan_many_r2r(2, nbTable, num_blocks, blocks,
nembed, 1, w * w, blocks, nembed,
1, w * w, kindTable, FFTW_ESTIMATE);
const uint32_t w2 = w * w;
float *image = input.get_channel(ch);
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (uint32_t q = 0; q < num_blocks; ++q)
{
for (int j = 0; j < w; ++j)
{
for (int i = 0; i < w; ++i)
{
int block_origin = block_ids[q].block_origin;
blocks[q * w2 + j * w + i] = image[block_origin + j * Nx + i];
}
}
}
// Compute 2D-DCT of all the blocks
//
// Transform blocks with FFTW
fftwf_execute_r2r(fft_plan, blocks, blocks);
// Normalize FFTW output
normalize_FFTW(blocks, w, num_blocks);
// store VH for each bin
VH_count = 0;
for (int q = 0; q < w * w; ++q)
{
int j = q / w;
int i = q - j * w;
if (i + j >= T)
{
for (uint32_t k = 0; k < num_blocks; ++k)
{
const BlockId &block_id = block_ids[k];
VH[block_id.bin * w2 + VH_count] += pow(blocks[k * w2 + q], 2);
}
++VH_count;
}
}
delete[] blocks;
fftwf_destroy_plan(fft_plan);
}
return VH_count;
}
//! Ponomarenko et al. AVIRIS noise estimation algorithm.
/*!
\param argc Number of arguments of the program
\param **argv Arguments of the program
*/
void algorithm(int argc, char **argv)
{
vector<OptStruct *> options;
vector<ParStruct *> parameters;
//
OptStruct owin = {"w:", 8, "8", NULL, "Block side"};
options.push_back(&owin);
OptStruct opercentile = {"p:", 1, "0.005", NULL, "Percentile"};
options.push_back(&opercentile);
// OptStruct ore = {"r", 0, NULL, NULL, "Flag to remove equal pixels"};
// options.push_back(&ore);
OptStruct obins = {"b:", 0, "0", NULL, "Number of bins"};
options.push_back(&obins);
OptStruct oD = {"D:", 7, "7", NULL, "Filtering distance"};
options.push_back(&oD);
OptStruct ofiltercurve = {"g:", 5, "5", NULL, "Filter curve iterations"};
options.push_back(&ofiltercurve);
OptStruct omeanMethod = {"m:", 2, "2", NULL, "Mean computation method"};
options.push_back(&omeanMethod);
OptStruct oremoveSaturate = {"s", 0, NULL, NULL, "Flag to remove saturated pixels"};
options.push_back(&oremoveSaturate);
OptStruct ochannel = {"c:", 3, "3", NULL, "Number of channels"};
options.push_back(&ochannel);
ParStruct pinput = {"input", NULL, "input image directory"};
parameters.push_back(&pinput);
//
if (!parsecmdline("ponomarenko", "Ponomarenko SD noise estimation algorithm",
argc, argv, options, parameters))
{
printf("\n");
printf("(c) 2012 Miguel Colom. Under license GNU GPL.\n");
printf("http://mcolom.perso.math.cnrs.fr/\n");
printf("\n");
exit(-1);
}
// Read parameters
int w = atoi(owin.value);
int T = get_T(w);
float p = atof(opercentile.value);
int num_bins = atoi(obins.value);
int D = atoi(oD.value);
int curve_filter_iterations = atoi(ofiltercurve.value);
int mean_method = atoi(omeanMethod.value);
// bool remove_equal_pixels_blocks = ore.flag;
bool remove_saturated_pixels_blocks = oremoveSaturate.flag;
int num_channels = atoi(ochannel.value);
// Parallelization config
#ifdef _OPENMP
omp_set_num_threads(omp_get_num_procs());
#endif
CFramework::set_verbose(false);
// Custom percentile or given by the user?
// bool custom_percentile = is_custom_percentile(opercentile.value);
// Load input image
std::string path(pinput.value);
std::vector<std::string> inputs = get_filenames(path);
// TODO: prepare statistics variable
std::vector<std::vector<float>> means_all(num_channels);
std::vector<std::vector<float>> VL_all(num_channels);
std::vector<std::vector<BlockId>> block_ids_all(num_channels);
// float *vmeans = NULL;
// float *vstds = NULL;
std::vector<float> vmeans;
std::vector<float> vstds;
for (uint32_t input_idx = 0; input_idx < inputs.size(); ++input_idx)
{
// TODO: input multiple images
PRINT_VERBOSE("Processing %s \n", inputs[input_idx].c_str());
CImage input;
input.load(inputs[input_idx].c_str());
// Get image properties
int Nx = input.get_width();
int Ny = input.get_height();
int input_channels = input.get_num_channels();
if (input_channels != num_channels)
{
printf("%s has a channel number (%d) incompatible with the required channel number (%d). \n"
"Skipped.\n",
inputs[input_idx].c_str(),
input_channels, num_channels);
continue;
}
int total_blocks = (Nx - w + 1) * (Ny - w + 1); // Number of overlapping blocks
// Create equal pixels mask
int *mask_all = NULL;
int num_blocks;
// int *mask_saturated;
mask_all = new int[Nx * Ny];
if (remove_saturated_pixels_blocks) {
num_blocks = buildSaturatedMask(input, mask_all, Nx, Ny, w, num_channels);
}
// else if (remove_equal_pixels_blocks)
// {
// mask_all = new int[Nx * Ny];
// num_blocks = buildMask(input, mask_all, Nx, Ny, w, num_channels);
// }
else
{
num_blocks = buildFullMask(mask_all, Nx, Ny, w);
}
if (input_idx == 0)
{
// reserve space for global storage
for (int ch = 0; ch < num_channels; ++ch)
{
means_all[ch].reserve(inputs.size() * total_blocks);
VL_all[ch].reserve(inputs.size() * total_blocks);
block_ids_all[ch].reserve(inputs.size() * total_blocks);
}
// Set number of bins
if (num_bins <= 0)
num_bins = Nx * Ny / 42000;
if (num_bins <= 0)
num_bins = 1; // Force at least one bin
// Initialize the arrays for final means and noise estimations
// vmeans = new float[num_channels * num_bins];
// vstds = new float[num_channels * num_bins];
vmeans.resize(num_channels * num_bins);
vstds.resize(num_channels * num_bins);
}
// Compute delta and theta
CFramework *fw = CFramework::get_framework();
float *delta = fw->create_array(w * w);
int theta = compute_delta(delta, w, T);
std::vector<float> means(num_blocks);
std::vector<BlockId> block_ids(num_blocks);
float *blocks = new float[num_blocks * w * w];
// Init FFTW threads
fftwf_init_threads();
int nbTable[2] = {w, w};
int nembed[2] = {w, w};
#ifdef _OPENMP
fftwf_plan_with_nthreads(omp_get_num_procs());
#endif
fftwf_r2r_kind kindTable[2] = {FFTW_REDFT10, FFTW_REDFT10};
fftwf_plan fft_plan = fftwf_plan_many_r2r(2, nbTable, num_blocks, blocks,
nembed, 1, w * w, blocks, nembed,
1, w * w, kindTable, FFTW_ESTIMATE);
unsigned* valid_coords = new unsigned[num_blocks];
make_valid_coords(mask_all, valid_coords, Nx, Ny);
// Process each channel
for (int ch = 0; ch < num_channels; ch++)
{
float *u = input.get_channel(ch);
read_all_valid_blocks(blocks, u, Nx, Ny, w, num_blocks, mask_all);
// Compute means
compute_means(means.data(), blocks, w, num_blocks);
// Compute 2D-DCT of all the blocks
//
// Transform blocks with FFTW
fftwf_execute_r2r(fft_plan, blocks, blocks);
// Normalize FFTW output
normalize_FFTW(blocks, w, num_blocks);
// Create a list of pointers of the groups
float **blocks_ptr = new float *[num_blocks];
for (int i = 0; i < num_blocks; i++)
blocks_ptr[i] = &blocks[i * w * w];
// Create the global indices for blocks
for (int i = 0; i < num_blocks; ++i)
{
block_ids[i].image = input_idx;
// block_ids[i].block_origin = i;
block_ids[i].block_origin = valid_coords[i];
block_ids[i].bin = -1;
}
// cout << "valid_coords[" << i << "]" << endl;
// Compute VL
std::vector<float> VL(num_blocks);
compute_VL(VL.data(), num_blocks, w, delta, blocks_ptr, theta);
// store the means, VL and block indices
VL_all[ch].insert(VL_all[ch].end(), VL.begin(), VL.end());
means_all[ch].insert(means_all[ch].end(), means.begin(), means.end());
block_ids_all[ch].insert(block_ids_all[ch].end(), block_ids.begin(), block_ids.end());
/*
// Create histogram according to the means
CHistogram<float*> histo = CHistogram<float*>(num_bins,
blocks_ptr,
means.data(),
num_blocks);
// Process each bin
#ifdef _OPENMP
#pragma omp parallel for shared(vmeans, vstds, histo) schedule(static)
#endif
for (int bin = 0; bin < num_bins; bin++) {
int elems_bin = histo.get_num_elements_bin(bin);
float **block_ptr_bin = histo.get_data_bin(bin);
float *VL = new float[elems_bin];
// Compute VL
compute_VL(VL, elems_bin, w, delta, block_ptr_bin, theta);
}*/
delete[] blocks_ptr;
}
if (mask_all != NULL)
delete[] mask_all;
delete[] blocks;
delete[] valid_coords;
} // end for each input
for (int ch = 0; ch < num_channels; ++ch)
{
assert(VL_all[ch].size() == means_all[ch].size());
assert(block_ids_all[ch].size() == means_all[ch].size());
// Create histogram according to the means
CHistogram<BlockId> histo_block_id = CHistogram<BlockId>(num_bins,
block_ids_all[ch].data(),
means_all[ch].data(),
means_all[ch].size());
// Create another histogram according to the means to store VL