-
Notifications
You must be signed in to change notification settings - Fork 3
/
inormalize.cc
1068 lines (900 loc) · 28.5 KB
/
inormalize.cc
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
/*--------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1996, Alex P. Zijdenbos,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
----------------------------------------------------------------------------
$RCSfile: inormalize.cc,v $
$Revision: 1.4 $
$Author: claude $
$Date: 2007-10-09 21:56:35 $
$State: Exp $
--------------------------------------------------------------------------*/
/* ----------------------------- MNI Header -----------------------------------
@NAME : inormalize
@INPUT : argc, argv - command line arguments
@OUTPUT : (none)
@RETURNS : error status
@DESCRIPTION: Program for the intensity normalization of a minc volume,
following a (registered) model volume.
@METHOD : Minimization of the MSE voxel intensity difference by exhaustive
search
@GLOBALS :
@CALLS :
@CREATED : April 11, 1995 (Alex Zijdenbos)
@MODIFIED : $Log: inormalize.cc,v $
@MODIFIED : Revision 1.4 2007-10-09 21:56:35 claude
@MODIFIED : fixed inormalize for float data type
@MODIFIED :
@MODIFIED : Revision 1.3 2005/08/17 23:14:26 bert
@MODIFIED : Minor changes for some C++ warnings and issues
@MODIFIED :
@MODIFIED : Revision 1.2 2002/03/28 23:53:19 jason
@MODIFIED : switched to SimpleArray from CachedArray as there appear to be IO problems in the cached array implementation
@MODIFIED :
@MODIFIED : Revision 1.1.1.1 2002/03/27 18:36:59 jason
@MODIFIED : First import of inormalize sources. Does not work perfectly yet,
@MODIFIED : especially the median estimation appears to fail.
@MODIFIED :
@MODIFIED :
@MODIFIED : Revision 1.6 1998/04/01 18:29:49 alex
@MODIFIED : Made OpTimers quiet if -quiet was specified
@MODIFIED :
@MODIFIED : Revision 1.5 1998/02/28 20:04:34 alex
@MODIFIED : Added -range and -const options
@MODIFIED :
@MODIFIED : Revision 1.4 1997/09/11 14:26:31 alex
@MODIFIED : Various changes directed at g++ compatibility
@MODIFIED :
@MODIFIED : Revision 1.3 1996/12/19 20:47:16 alex
@MODIFIED : Various changes directed at g++ compatibility
@MODIFIED :
* Revision 1.2 1996/09/04 20:56:35 alex
* Checked Inormalize into main branch
*
* Revision 1.1.1.1 1996/08/29 19:05:04 alex
* Source for inormalize
*
@COPYRIGHT :
---------------------------------------------------------------------------- */
#include "inormalize.h"
#include <iostream>
using namespace std;
#include <time.h>
// #include "EBTKS/CachedArray.h"
#include "EBTKS/Dictionary.h"
#include "EBTKS/FileIO.h"
#include "EBTKS/OpTimer.h"
#include "EBTKS/amoeba.h"
#include "EBTKS/trivials.h"
class OptData {
public:
const FloatArray& modelArray;
const FloatArray& dataArray;
unsigned n;
Boolean verbose;
OptData(const FloatArray& ma, const FloatArray& da, unsigned nEl,
Boolean vb = FALSE)
: modelArray(ma),
dataArray(da)
{ n = nEl; verbose = vb; }
};
//
// Main program
//
int
main(int argc, char *argv[])
{
// Set timers
OpTimer usr(OpTimer::USR, "inormalize");
OpTimer sys(OpTimer::SYS, "inormalize");
OpTimer cpu(OpTimer::CPU, "inormalize");
// Get argument information
InormalizeArgs args(argc, argv);
if (!args.verbose) {
usr.verbose(FALSE);
sys.verbose(FALSE);
cpu.verbose(FALSE);
}
// Determine valid voxels from the mask and threshold settings
SimpleArray<Boolean> validVoxels;
unsigned nVoxels = getValidVoxels(args, validVoxels);
if (args.verbose)
cout << "Considering " << nVoxels << " voxels ("
<< 100.0*nVoxels/validVoxels.size() << "% of total)" << endl;
if (!nVoxels) {
cerr << "Error! voxel selection criterai removed all voxels. Not normalizing volume" << endl;
exit(EXIT_FAILURE);
}
if (args.verbose)
cout << "Selected normalization: " << args.methods[args.method] << endl;
// Can delete mask volume here
if (args.mask) {
delete_volume(args.mask);
args.mask = 0;
}
// Create history to append to MINC file
time_t timeVal = time(0);
MString history(ctime(&timeVal));
history.chop();
history += ">>> ";
history += args.command;
history += "; ";
// Self-normalize the input volume
if (args.zNormalize)
selfNormalizeMain(MIzspace, validVoxels, args, history);
if (args.yNormalize)
selfNormalizeMain(MIyspace, validVoxels, args, history);
if (args.xNormalize)
selfNormalizeMain(MIxspace, validVoxels, args, history);
if (args.oneConst) {
SimpleArray<float> volumeArray;
floatArrayFromVolume(volumeArray, args.volume, validVoxels, &nVoxels, args.verbose);
LinearMap iMap;
if (args.method == InormalizeArgs::RATIO_OF_MEANS)
iMap.factor(args.constants[0] / mean(volumeArray));
else if (args.method == InormalizeArgs::RATIO_OF_MEDIANS) {
iMap.factor(args.constants[0] / median(volumeArray));
cout << median(volumeArray) << endl;
cout << mean(volumeArray) << endl;
}
else {
cerr << "Invalid method " << args.methods[args.method] << endl;
exit(EXIT_FAILURE);
}
MString mapString;
appendToString(mapString, iMap);
if (args.normalize) {
history += mapString;
reMapVolume(args.volume, iMap, args.verbose);
}
}
else if (args.twoConst) {
SimpleArray<float> volumeArray;
floatArrayFromVolume(volumeArray, args.volume, validVoxels, &nVoxels, args.verbose);
FloatArray extrema(pctExtrema(volumeArray, args.rangePct, args.verbose));
LinearMap iMap(extrema[(unsigned int)0],
extrema[(unsigned int)1],
args.constants[(unsigned int)0],
args.constants[(unsigned int)1]);
MString mapString;
appendToString(mapString, iMap);
if (args.normalize) {
history += mapString;
reMapVolume(args.volume, iMap, args.verbose);
}
}
else if (args.model) {
// Make sure volume and model have compatible sizes.
int volume_sizes[3], model_sizes[3];
get_volume_sizes(args.volume, volume_sizes);
get_volume_sizes(args.model, model_sizes);
if( ( model_sizes[0] != volume_sizes[0] ) ||
( model_sizes[1] != volume_sizes[1] ) ||
( model_sizes[2] != volume_sizes[2] ) ) {
cerr << "Volume and model dimensions do not match!" << endl;
exit(EXIT_FAILURE);
}
// Convert entire volume to float arrays (cached to conserve memory)
SimpleArray<float> volumeArray;
SimpleArray<float> modelArray;
floatArrayFromVolume(volumeArray, args.volume, validVoxels, &nVoxels, args.verbose);
floatArrayFromVolume(modelArray, args.model, validVoxels, &nVoxels, args.verbose);
// Can delete model volume here
delete_volume(args.model);
args.model = 0;
#ifdef HAVE_MATLAB
if (args.matlabOutputString) {
SimpleArray<float> samples = SimpleArray<float>(volumeArray.sample(10000));
samples.saveMatlab(args.matlabOutputString, "volume");
samples = SimpleArray<float>(modelArray.sample(10000));
samples.saveMatlab(args.matlabOutputString, "model");
}
#endif
history += "global:";
for (unsigned method = 0; method < args.nMethods; method++)
if ((method == args.method) || args.printValues) {
if (args.printValues)
cout << " " << args.methods[method] << ":" << flush;
LinearMap iMap = determineMap(modelArray, volumeArray, args, method);
MString mapString;
appendToString(mapString, iMap);
if (args.printValues)
cout << mapString << endl;
#ifdef HAVE_MATLAB
if (args.matlabOutputString) {
SimpleArray<float> mapArray(2);
LinearMap tempMap(iMap);
tempMap.inv();
mapArray[0] = tempMap.factor();
mapArray[1] = tempMap.offset();
mapArray.saveMatlab(args.matlabOutputString, args.methods[method]);
}
#endif
if (args.normalize && (args.method == method)) {
history += mapString;
reMapVolume(args.volume, iMap, args.verbose);
}
}
}
if (args.normalize) {
history += "\n";
if (!saveVolume(args.volume, args.outputPath, args.inputPath, history,
args.compress, args.verbose))
exit(EXIT_FAILURE);
}
return(EXIT_SUCCESS);
}
//
//
//
void
scanVoxelRange(const Volume volume, double *voxelMin, double *voxelMax)
{
int sizes[3];
get_volume_sizes(volume, sizes);
unsigned D1 = sizes[0];
unsigned D2 = sizes[1];
unsigned D3 = sizes[2];
Real minVal = get_volume_voxel_value(volume, 0, 0, 0, 0, 0);
Real maxVal = minVal;
for (unsigned d1 = 0; d1 < D1; d1++)
for (unsigned d2 = 0; d2 < D2; d2++)
for (unsigned d3 = 0; d3 < D3; d3++) {
Real value = get_volume_voxel_value(volume, d1, d2, d3, 0, 0);
if (value < minVal)
minVal = value;
else if (value > maxVal)
maxVal = value;
}
*voxelMin = minVal;
*voxelMax = maxVal;
}
//
//
//
unsigned
getValidVoxels(const InormalizeArgs& args, BoolArray& validVoxels)
{
int sizes[3];
get_volume_sizes(args.volume, sizes);
unsigned D1 = sizes[0];
unsigned D2 = sizes[1];
unsigned D3 = sizes[2];
unsigned nVoxels = D1*D2*D3;
validVoxels.newSize(nVoxels);
double minT = args.thresholds[0];
double maxT = args.thresholds[1];
Boolean useThreshold =
((maxT >= minT) && ((minT > -MAXDOUBLE) || (maxT < MAXDOUBLE)));
if (args.mask || useThreshold) {
validVoxels.clear(FALSE);
unsigned maskD1, maskD2, maskD3;
if (args.mask) {
if (args.verbose)
cout << "Scanning mask" << flush;
int maskSizes[3];
get_volume_sizes(args.mask, maskSizes);
maskD1 = maskSizes[0];
maskD2 = maskSizes[1];
maskD3 = maskSizes[2];
if (!args.useWorldCoord && ((maskD1 != D1) || (maskD2 != D2) || (maskD3 != D3))) {
cerr << "Volume and mask dimensions do not match!" << endl;
exit(EXIT_FAILURE);
}
}
nVoxels = 0;
unsigned i = 0;
for (unsigned d1 = 0; d1 < D1; d1++) {
for (unsigned d2 = 0; d2 < D2; d2++) {
for (unsigned d3 = 0; d3 < D3; d3++) {
Boolean valid = TRUE;
if (useThreshold) {
Real value = get_volume_real_value(args.volume, d1, d2, d3, 0, 0);
if ((value < minT) || (value > maxT))
valid = FALSE;
}
if (valid && args.mask) {
Real maskValue = 0;
if (args.useWorldCoord) {
Real xWorld, yWorld, zWorld;
convert_3D_voxel_to_world(args.volume, d1, d2, d3,
&xWorld, &yWorld, &zWorld);
Real voxel1, voxel2, voxel3;
convert_3D_world_to_voxel(args.mask, xWorld, yWorld, zWorld,
&voxel1, &voxel2, &voxel3);
int v1 = ROUND(voxel1);
int v2 = ROUND(voxel2);
int v3 = ROUND(voxel3);
if ((v1 >= 0) && (v2 >= 0) && (v3 >= 0) &&
(v1 < maskD1) && (v2 < maskD2) && (v3 < maskD3))
maskValue = get_volume_real_value(args.mask, v1, v2, v3, 0, 0);
}
else
maskValue = get_volume_real_value(args.mask, d1, d2, d3, 0, 0);
if (!maskValue)
valid = FALSE;
}
if (validVoxels[i++] = valid)
nVoxels++;
}
}
if (args.verbose)
cout << "." << flush;
}
if (args.verbose)
cout << "Done" << endl;
}
else
validVoxels.clear(TRUE);
return nVoxels;
}
//
//
//
void
floatArrayFromVolume(FloatArray& array, const Volume volume,
const BoolArray& validVoxels, unsigned *N,
int verbose)
{
unsigned nVoxels = (N && *N) ? *N : validVoxels.occurrencesOf(TRUE);
if (N && !*N)
*N = nVoxels;
if (verbose)
cout << "Converting volume" << flush;
array.newSize(nVoxels);
int sizes[3];
get_volume_sizes(volume, sizes);
unsigned D1 = sizes[0];
unsigned D2 = sizes[1];
unsigned D3 = sizes[2];
unsigned voxelCtr = 0;
unsigned valueCtr = 0;
for (unsigned d1 = 0; d1 < D1; d1++) {
for (unsigned d2 = 0; d2 < D2; d2++) {
for (unsigned d3 = 0; d3 < D3; d3++)
if (validVoxels[voxelCtr++])
array[valueCtr++] = (float) get_volume_real_value(volume, d1, d2, d3, 0, 0);
}
if (verbose)
cout << "." << flush;
}
if (verbose)
cout << "Done" << endl;
}
//
//
//
void
floatArraysFromSlices(const Volume volume, const BoolArray& validVoxels,
unsigned axis, unsigned slice1, unsigned slice2,
FloatArray& array1, FloatArray& array2)
{
int sizes[3];
get_volume_sizes(volume, sizes);
unsigned D1 = sizes[0];
unsigned D2 = sizes[1];
unsigned D3 = sizes[2];
switch(axis) {
case 0: {
unsigned N = D2*D3;
array1.newSize(N);
array1.newSize(0); // Allocated but empty
array2.newSize(N);
array2.newSize(0); // Allocated but empty
unsigned valid1 = slice1*N;
unsigned valid2 = slice2*N;
// const Boolean *valid1 = validVoxels.contents() + slice1*N;
// const Boolean *valid2 = validVoxels.contents() + slice2*N;
for (unsigned d2 = 0; d2 < D2; d2++)
for (unsigned d3 = 0; d3 < D3; d3++, valid1++, valid2++)
if (validVoxels[valid1] && validVoxels[valid2]) {
Real value1 = get_volume_real_value(volume, slice1, d2, d3, 0, 0);
Real value2 = get_volume_real_value(volume, slice2, d2, d3, 0, 0);
if (value1 && value2) {
array1.append(value1);
array2.append(value2);
}
}
break;
}
case 1: {
unsigned N = D1*D3;
array1.newSize(N);
array1.newSize(0); // Allocated but empty
array2.newSize(N);
array2.newSize(0); // Allocated but empty
// const Boolean *validBase = validVoxels.contents();
for (unsigned d1 = 0; d1 < D1; d1++) {
unsigned valid1 = (d1*D2 + slice1)*D3;
unsigned valid2 = (d1*D2 + slice2)*D3;
// const Boolean *valid1 = validBase + (d1*D2 + slice1)*D3;
// const Boolean *valid2 = validBase + (d1*D2 + slice2)*D3;
for (unsigned d3 = 0; d3 < D3; d3++, valid1++, valid2++)
if (validVoxels[valid1] && validVoxels[valid2]) {
Real value1 = get_volume_real_value(volume, d1, slice1, d3, 0, 0);
Real value2 = get_volume_real_value(volume, d1, slice2, d3, 0, 0);
if (value1 && value2) {
array1.append(value1);
array2.append(value2);
}
}
}
break;
}
case 2: {
unsigned N = D1*D2;
array1.newSize(N);
array1.newSize(0); // Allocated but empty
array2.newSize(N);
array2.newSize(0); // Allocated but empty
// const Boolean *validBase = validVoxels.contents();
for (unsigned d1 = 0; d1 < D1; d1++) {
// const Boolean *valid1 = validBase + d1*D2*D3 + slice1;
// const Boolean *valid2 = validBase + d1*D2*D3 + slice2;
unsigned valid1 = d1*D2*D3 + slice1;
unsigned valid2 = d1*D2*D3 + slice2;
for (unsigned d2 = 0; d2 < D2; d2++) {
if (validVoxels[valid1] && validVoxels[valid2]) {
Real value1 = get_volume_real_value(volume, d1, d2, slice1, 0, 0);
Real value2 = get_volume_real_value(volume, d1, d2, slice2, 0, 0);
if (value1 && value2) {
array1.append(value1);
array2.append(value2);
}
}
valid1 += D3;
valid2 += D3;
}
}
break;
}
}
}
//
//
//
void
selfNormalizeMain(char *dimension, const BoolArray& validVoxels,
const InormalizeArgs& args, MString& history)
{
for (unsigned method = 0; method < args.nMethods; method++)
if ((method == args.method) || args.printValues) {
Array<LinearMap> iMaps(selfNormalize(args.volume, validVoxels, dimension,
args, method));
MString mapString(dimension);
mapString += ":";
appendToString(mapString, iMaps);
if (args.method == method)
history += mapString;
if (args.printValues)
cout << " " << args.methods[method] << ":" << mapString << endl;
}
}
//
//
//
Array<LinearMap>
selfNormalize(Volume volume, const BoolArray& validVoxels, char *dimension,
const InormalizeArgs& args, int method)
{
if (method < -1)
method = args.method;
int sizes[3];
get_volume_sizes(volume, sizes);
int axis;
if (!convert_dim_name_to_spatial_axis(dimension, &axis)) {
cerr << "inormalize: couldn't convert dim_name " << dimension
<< " to axis" << endl;
exit(EXIT_FAILURE);
}
axis = 2 - axis; // ??? Why is this necessary?
unsigned sliceDim = sizes[axis];
Array<LinearMap> iMaps(sliceDim);
if (sliceDim < 2)
return iMaps;
unsigned centerSlice = sliceDim/2;
unsigned int slice;
FloatArray refArray, array;
for (slice = centerSlice; slice < sliceDim - 1; slice++) {
floatArraysFromSlices(volume, validVoxels, axis, slice, slice+1, refArray, array);
LinearMap sliceMap(determineMap(refArray, array, args, method));
iMaps[slice + 1] = iMaps[slice];
iMaps[slice + 1].concat(sliceMap);
}
for (slice = centerSlice; slice > 0; slice--) {
floatArraysFromSlices(volume, validVoxels, axis, slice, slice-1, refArray, array);
LinearMap sliceMap(determineMap(refArray, array, args, method));
iMaps[slice - 1] = iMaps[slice];
iMaps[slice - 1].concat(sliceMap);
}
for (slice = 0; slice < sliceDim; slice++)
cout << slice << ": " << iMaps[slice].factor() << endl;
reMapVolume(volume, axis, iMaps, args.verbose);
return iMaps;
}
//
//
//
void
reMapVolume(Volume volume, const LinearMap& iMap, int verbose)
{
// Scan voxel range, just in case the volume attributes are messed up
double voxelMin, voxelMax;
scanVoxelRange(volume, &voxelMin, &voxelMax);
// Get real extrema
double realMin = CONVERT_VOXEL_TO_VALUE(volume, voxelMin);
double realMax = CONVERT_VOXEL_TO_VALUE(volume, voxelMax);
// cout << "(" << voxelMin << ", " << voxelMax << ") -> ("
// << realMin << ", " << realMax << ")" << endl;
if (verbose)
cout << "Normalizing (" << iMap.factor() << ", " << iMap.offset() << ")" << endl;
// Concatenate original and derived mapping
realMin = iMap(realMin);
realMax = iMap(realMax);
//LinearMap fullMap(voxelMin, voxelMax, realMin, realMax);
//cout << "FullMap: " << fullMap << endl;
// cout << "(" << voxelMin << ", " << voxelMax << ") -> ("
// << realMin << ", " << realMax << ")" << endl;
// Scale the voxel values if not working with ranges (for FLOAT and DOUBLE).
if( !volume->real_range_set ) {
cout << "Fixing voxel values for real data type..." << endl;
int sizes[3];
get_volume_sizes(volume, sizes);
for (unsigned d1 = 0; d1 < sizes[0]; d1++) {
for (unsigned d2 = 0; d2 < sizes[1]; d2++) {
for (unsigned d3 = 0; d3 < sizes[2]; d3++) {
Real value = get_volume_voxel_value(volume, d1, d2, d3, 0, 0);
set_volume_voxel_value(volume, d1, d2, d3, 0, 0,
clamp(iMap(value), realMin, realMax));
}
}
}
} else {
// Reset the voxel range and set the desired real range
set_volume_voxel_range(volume, voxelMin, voxelMax);
}
set_volume_real_range(volume, realMin, realMax);
}
//
//
//
void
reMapVolume(Volume volume, int axis, const Array<LinearMap>& iMaps, int verbose)
{
int sizes[3];
get_volume_sizes(volume, sizes);
unsigned D1 = sizes[0];
unsigned D2 = sizes[1];
unsigned D3 = sizes[2];
// Scan voxel range, just in case the volume attributes are messed up
double voxelMin = MAXDOUBLE;
double voxelMax = -MAXDOUBLE;
double mappedRealMin = MAXDOUBLE;
double mappedRealMax = -MAXDOUBLE;
// Traverse volume to find new global min and max
LinearMap iMap;
unsigned d1;
for (d1 = 0; d1 < D1; d1++) {
if (!axis)
iMap = iMaps[d1];
for (unsigned d2 = 0; d2 < D2; d2++) {
if (axis == 1)
iMap = iMaps[d2];
for (unsigned d3 = 0; d3 < D3; d3++) {
if (axis == 2)
iMap = iMaps[d3];
Real value = get_volume_voxel_value(volume, d1, d2, d3, 0, 0);
if (value < voxelMin)
voxelMin = value;
if (value > voxelMax)
voxelMax = value;
value = iMap(CONVERT_VOXEL_TO_VALUE(volume, value));
if (value < mappedRealMin)
mappedRealMin = value;
if (value > mappedRealMax)
mappedRealMax = value;
}
}
}
// Get real extrema
double realMin = CONVERT_VOXEL_TO_VALUE(volume, voxelMin);
double realMax = CONVERT_VOXEL_TO_VALUE(volume, voxelMax);
// Retrieve original voxel->real mapping
LinearMap voxelToRealMap(voxelMin, voxelMax, CONVERT_VOXEL_TO_VALUE(volume, voxelMin),
CONVERT_VOXEL_TO_VALUE(volume, voxelMax));
// Reset the voxel range to the full range of the data type
// And set the desired real range
set_volume_voxel_range(volume, voxelMin, voxelMax);
set_volume_real_range(volume, mappedRealMin, mappedRealMax);
// Retrieve range of data type
// get_volume_voxel_range(volume, &voxelMin, &voxelMax);
// Determine final real->voxel map
LinearMap realToVoxelMap(mappedRealMin, mappedRealMax, voxelMin, voxelMax);
/*
cout << "Axis: " << axis << endl
<< "voxelMin: " << voxelMin << " voxelMax: " << voxelMax << endl
<< "Voxel->real map: " << voxelToRealMap << endl
<< "Real->voxel map: " << realToVoxelMap << endl;
*/
// Remap volume
if (verbose)
cout << "Remapping slices" << flush;
for (d1 = 0; d1 < D1; d1++) {
if (!axis) {
iMap = voxelToRealMap;
iMap.concat(iMaps[d1]);
iMap.concat(realToVoxelMap);
// iMap = realToVoxelMap(iMaps[d1](voxelToRealMap));
}
for (unsigned d2 = 0; d2 < D2; d2++) {
if (axis == 1) {
iMap = realToVoxelMap;
iMap.concat(iMaps[d2]);
iMap.concat(voxelToRealMap);
// iMap = realToVoxelMap(iMaps[d2](voxelToRealMap));
}
for (unsigned d3 = 0; d3 < D3; d3++) {
if (axis == 2) {
iMap = realToVoxelMap;
iMap.concat(iMaps[d3]);
iMap.concat(voxelToRealMap);
// iMap = realToVoxelMap(iMaps[d3](voxelToRealMap));
}
Real value = get_volume_voxel_value(volume, d1, d2, d3, 0, 0);
set_volume_voxel_value(volume, d1, d2, d3, 0, 0,
clamp(iMap(value), voxelMin, voxelMax));
}
}
if (verbose)
cout << "." << flush;
}
if (verbose)
cout << "Done" << endl;
}
//
//
//
LinearMap
determineMap(const FloatArray& modelArray, const FloatArray& dataArray,
const InormalizeArgs& args, int method)
{
unsigned nVoxels = size(modelArray);
if (size(dataArray) != nVoxels) {
cerr << "determineMap: sizes of data- and modelarray are not equal!" << endl;
exit(EXIT_FAILURE);
}
if (method < 0)
method = args.method;
LinearMap map;
if (nVoxels < args.minVoxels)
return map;
if (method == InormalizeArgs::RMS) {
OptData optData(modelArray, dataArray, nVoxels, args.verbose);
FloatArray temp(modelArray);
double f = temp.medianVolatile();
temp = dataArray;
f /= temp.medianVolatile();
double fStep = 0.1;
amoeba_struct amoeba;
initialize_amoeba(&amoeba, 1, &f, &fStep, evaluateRMS, (void *) &optData, 1e-6);
unsigned iter = 0;
while ((iter < 100) && perform_amoeba(&amoeba))
iter++;
double minRMS = get_amoeba_parameters(&amoeba, &f);
terminate_amoeba(&amoeba);
map.factor() = f;
}
else if (method == InormalizeArgs::VR) {
OptData optData(modelArray, dataArray, nVoxels, args.verbose);
FloatArray temp(modelArray);
double f = temp.medianVolatile();
temp = dataArray;
f /= temp.medianVolatile();
double fStep = 0.1;
amoeba_struct amoeba;
initialize_amoeba(&amoeba, 1, &f, &fStep, evaluateVR, (void *)&optData, 1e-6);
unsigned iter = 0;
while ((iter < 100) && perform_amoeba(&amoeba))
iter++;
double minVR = get_amoeba_parameters(&amoeba, &f);
terminate_amoeba(&amoeba);
map.factor() = f;
}
else if (method == InormalizeArgs::RATIO_OF_MEANS)
map.factor() = mean(modelArray)/mean(dataArray);
else if (method == InormalizeArgs::RATIO_OF_MEDIANS) {
FloatArray temp(modelArray);
map.factor() = temp.medianVolatile();
temp = dataArray;
map.factor() /= temp.medianVolatile();
}
else if (method == InormalizeArgs::MEAN_OF_RATIOS) {
FloatArray temp(modelArray);
temp /= dataArray;
prune(temp);
if (args.constrained) {
float ceil = pow(10, 0.1);
float floor = pow(10, -0.1);
temp.removeAllNotIn(floor, ceil);
}
if (size(temp) >= args.minVoxels) {
map.factor() = mean(temp);
if (args.constrained && args.verbose)
cout << "+";
}
}
else if (method == InormalizeArgs::MEAN_OF_LOG_RATIOS) {
FloatArray temp(modelArray);
temp /= dataArray;
temp = log(temp);
prune(temp);
if (args.constrained) {
float ceil = 0.1;
float floor = -0.1;
temp.removeAllNotIn(floor, ceil);
}
if (size(temp) >= args.minVoxels) {
map.factor() = pow(10, mean(temp));
if (args.constrained && args.verbose)
cout << "+";
}
}
else if (method == InormalizeArgs::MEDIAN_OF_RATIOS) {
FloatArray temp(modelArray);
temp /= dataArray;
prune(temp);
if (args.constrained) {
float ceil = pow(10, 0.1);
float floor = pow(10, -0.1);
temp.removeAllNotIn(floor, ceil);
}
if (size(temp) >= args.minVoxels) {
map.factor() = temp.medianVolatile();
if (args.constrained && args.verbose)
cout << "+";
}
}
else if (method == InormalizeArgs::RANGE) {
FloatArray dataExtrema(pctExtrema(dataArray, args.rangePct, args.verbose));
FloatArray modelExtrema(pctExtrema(modelArray, args.rangePct, args.verbose));
map(dataExtrema[(unsigned int)0],
dataExtrema[(unsigned int)1],
modelExtrema[(unsigned int)0],
modelExtrema[(unsigned int)1]);
}
else {
cerr << "Unknown method " << method << " encountered" << endl;
exit(EXIT_FAILURE);
}
return map;
}
//
//
//
Boolean
saveVolume(const Volume volume, const Path& path, const Path& mincModel,
const MString& history, int compress, int verbose)
{
char *mincModelString = 0;
if (!mincModel.isEmpty())
mincModelString = (char *) mincModel.string();
char *historyString = 0;
if (!history.isEmpty())
historyString = (char *) history.string();
Path outputPath(path.expanded());
outputPath.removeCompressedExtension();
if (output_modified_volume(outputPath, NC_UNSPECIFIED, FALSE, 0.0, 0.0,
volume, mincModelString, historyString,
(minc_output_options *) NULL) != OK) {
cerr << "Couldn't save " << outputPath << endl;
return FALSE;
}
if (compress) {
if (verbose)
cout << "Compressing " << outputPath << "..." << flush;
MString zipCommand("gzip -f " + outputPath);
system(zipCommand);
if (verbose)
cout << "Done" << endl;
}
return TRUE;
}
//
//
//
MString&
appendToString(MString& string, const LinearMap& iMap)
{
string += " (";
string += iMap.factor();
string += ", ";
string += iMap.offset();
string += ")";
return string;
}
//
//
//
MString&
appendToString(MString& string, const Array<LinearMap>& iMaps)
{
unsigned n = iMaps.size();
if (n)
for (unsigned i = 0; i < n; i++) {
string += " (";
string += iMaps[i].factor();
string += ", ";
string += iMaps[i].offset();
string += ")";
}
return string;
}
double
evaluateRMS(void *data, float *f)
{
OptData& optData = *(OptData *) data;
double RMS = 0;
optData.modelArray.resetIterator();
optData.dataArray.resetIterator();
for (unsigned i = optData.n; i; i--) {
double value = optData.modelArray++ - (optData.dataArray++) * (*f);
RMS += value*value;