forked from freesurfer/freesurfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmri_coreg.cpp
2144 lines (1946 loc) · 67.7 KB
/
mri_coreg.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
/**
* @brief Computes registration between two volumes
*
*/
/*
* Original Author: Douglas N. Greve
*
* Copyright © 2021 The General Hospital Corporation (Boston, MA) "MGH"
*
* Terms and conditions for use, reproduction, distribution and contribution
* are found in the 'FreeSurfer Software License Agreement' contained
* in the file 'LICENSE' found in the FreeSurfer distribution, and here:
*
* https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
*
* Reporting: [email protected]
*
*/
/*!
\file dummy.c
\brief Example c file that can be used as a template.
\author Douglas Greve
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include "macros.h"
#include "utils.h"
#include "fio.h"
#include "version.h"
#include "cmdargs.h"
#include "error.h"
#include "diag.h"
#include "mri.h"
#include "mri2.h"
#include "mrisurf.h"
#include <sys/time.h>
#include <sys/resource.h>
#include "romp_support.h"
#include "timer.h"
#include "mrimorph.h"
#include "fmriutils.h"
#include "fsenv.h"
#include "matfile.h"
#include "icosahedron.h"
#include "cpputils.h"
#include "numerics.h"
#include "randomfields.h"
#include "mri_conform.h"
double round(double x);
static int parse_commandline(int argc, char **argv);
static void check_options(void);
static void print_usage(void) ;
static void usage_exit(void);
static void print_help(void) ;
static void print_version(void) ;
static void dump_options(FILE *fp);
int main(int argc, char *argv[]) ;
static char vcid[] = "$Id: mri_coreg.c,v 1.27 2016/04/30 15:11:49 greve Exp $";
const char *Progname = NULL;
char *cmdline, cwd[2000];
int debug=0;
int checkoptsonly=0;
struct utsname uts;
typedef struct {
char *mov;
const char *ref;
const char *refmask;
char *movmask;
char *outreg;
char *regdat;
char *subject;
int DoCoordDither;
int DoIntensityDither;
char *moviditherfile=NULL;
int dof;
double params[12];
int nsep, seplist[10];
char *DoInitCostOnly=NULL;
int DoSmoothing;
int cras0;
int AlignCentroids=0;
double ftol,linmintol;
int nitersmax;
int refconf;
char *logcost;
int DoBF;
double BFLim;
int BFNSamp;
char *outparamfile;
char *outcostfile;
double fwhmc, fwhmr, fwhms;
int SmoothRef;
double SatPct;
int MovOOBFlag;
const char *rusagefile;
int optschema;
int seed=53;
char *movoutfile=NULL;
char *InitRegSave=NULL;
int InitRegSaveOnly = 0;
int ltatype = LINEAR_VOX_TO_VOX;
} CMDARGS;
CMDARGS *cmdargs;
MRI *MRIrescaleToUChar(MRI *mri, MRI *ucmri, double sat);
unsigned char *MRItoUCharVect(MRI *mri, RFS *rfs, MRI *dither);
MATRIX *MRIgetVoxelToVoxelXformBase(MRI *mri_src, MRI *mri_dst, MATRIX *SrcRAS2DstRAS, MATRIX *SrcVox2DstVox, int base);
double **conv1dmat(double **M, int rows, int cols, double *v, int nv, int dim,
double **C, int *pcrows, int *pcols);
int conv1dmatTest(void);
double *conv1d(double *v1, int n1, double *v2, int n2, double *vc);
double **AllocDoubleMatrix(int rows, int cols);
int FreeDoubleMatrix(double **M, int rows, int cols);
int WriteDoubleMatrix(const char *fname, const char *fmt, double **M, int rows, int cols);
int PrintDoubleMatrix(FILE *fp, const char *fmt, double **M, int rows, int cols);
double *SumVectorDoubleMatrix(double **M, int rows, int cols, int dim, double *sumvect, int *nv);
typedef struct {
MRI *ref, *mov, *refmask, *movmask;
int seplist[10],nsep,sep,sepmin;
double SatPct,refsat, movsat;
unsigned char *g,*f;
MATRIX *M,*V2V;
double reffwhm[3],refgstd[3];
double movfwhm[3],movgstd[3];
double histfwhm[2];
double params[12]; // params to be optimized, may be < 12
int nparams; // number of params to be optimized, may be < 12
double mparams[12]; // params to create the matrix, always=12
double H01d[256*256];
double **H0;
double cost;
int nCostEvaluations;
double tLastEval;
double ftol,linmintol;
float fret;
int nitersmax,niters;
int startmin;
int nhits,nvoxref;
double pcthits;
int DoCoordDither;
RFS *crfs;
MRI *cdither;
int DoIntensityDither;
RFS *refirfs,*movirfs;
MRI *movidither=NULL;
int DoSmoothing;
FILE *fplogcost;
int MovOOBFlag;
int optschema;
int debug;
int seed;
} COREG;
double COREGcost(COREG *coreg);
float COREGcostPowell(float *pPowel) ;
int COREGMinPowell();
float MRIgetPercentile(MRI *mri, double Pct, int frame);
int COREGfwhm(MRI *mri, double sep, double fwhm[3]);
int COREGpreproc(COREG *coreg);
LTA *LTAcreate(MRI *src, MRI *dst, MATRIX *T, int type);
int COREGhist(COREG *coreg);
long COREGvolIndex(int ncols, int nrows, int nslices, int c, int r, int s);
double COREGsamp(unsigned char *f, const double c, const double r, const double s,
const int ncols, const int nrows, const int nslices);
double NMICost(double **H, int cols, int rows);
int COREGprint(FILE *fp, COREG *coreg);
MRI *MRIconformNoScale(MRI *mri, MRI *mric);
int COREGoptBruteForce(COREG *coreg, double lim0, int niters, int n1d);
double *COREGoptSchema2MatrixPar(COREG *coreg, double *par);
int COREGmatrixPar2OptSchema(COREG *coreg, double *par);
COREG *coreg;
FSENV *fsenv;
/*---------------------------------------------------------------*/
int main(int argc, char *argv[]) {
int nargs,err,n;
LTA *lta;
Timer timer;
timer.reset();
cmdargs = (CMDARGS *)calloc(sizeof(CMDARGS),1);
cmdargs->mov = NULL;
cmdargs->ref = NULL;
cmdargs->outreg = NULL;
cmdargs->regdat = NULL;
cmdargs->subject = NULL;
cmdargs->DoCoordDither = 1;
cmdargs->DoIntensityDither = 1;
cmdargs->dof = 6;
for(n=0; n < 12; n++) cmdargs->params[n] = 0;
for(n=6; n < 9; n++) cmdargs->params[n] = 1;
cmdargs->nsep = 0;
cmdargs->DoInitCostOnly = 0;
cmdargs->DoSmoothing = 1;
cmdargs->cras0 = 1;
cmdargs->nitersmax = 4;
cmdargs->ftol = 10e-8;
cmdargs->linmintol = .001;
cmdargs->refconf = 0;
cmdargs->DoBF = 1;
cmdargs->BFLim = 30;
cmdargs->BFNSamp = 30;
cmdargs->SmoothRef = 0;
cmdargs->SatPct = 99.99;
cmdargs->MovOOBFlag = 0;
cmdargs->optschema = 1;
cmdargs->seed = 53;
cmdargs->rusagefile = "";
nargs = handleVersionOption(argc, argv, "mri_coreg");
if (nargs && argc - nargs == 1) exit (0);
argc -= nargs;
cmdline = argv2cmdline(argc,argv);
uname(&uts);
getcwd(cwd,2000);
Progname = argv[0] ;
argc --;
argv++;
ErrorInit(NULL, NULL, NULL) ;
DiagInit(NULL, NULL, NULL) ;
if (argc == 0) usage_exit();
parse_commandline(argc, argv);
fsenv = FSENVgetenv();
check_options();
if(checkoptsonly) return(0);
dump_options(stdout);
coreg = (COREG *) calloc(sizeof(COREG),1);
coreg->seed = cmdargs->seed;
printf("Seed %d\n",coreg->seed);
printf("Reading in mov %s\n",cmdargs->mov);
coreg->mov = MRIread(cmdargs->mov);
if(!coreg->mov) exit(1);
printf("Reading in ref %s\n",cmdargs->ref);
coreg->ref = MRIread(cmdargs->ref);
if(!coreg->ref) exit(1);
if(cmdargs->refconf){
MRI *mritmp;
printf("Conforming ref (no scaling or rehistogramming)\n");
mritmp = MRIconformNoScale(coreg->ref,NULL);
MRIfree(&coreg->ref);
coreg->ref = mritmp;
}
coreg->nvoxref = coreg->ref->width * coreg->ref->height * coreg->ref->depth;
if(cmdargs->moviditherfile){
printf("Reading mov intensity dither file %s\n",cmdargs->moviditherfile);
coreg->movidither = MRIread(cmdargs->moviditherfile);
if(!coreg->movidither) exit(1);
}
if(cmdargs->refmask){
printf("Reading in and applying refmask %s\n",cmdargs->refmask);
coreg->refmask = MRIread(cmdargs->refmask);
if(!coreg->refmask) exit(1);
int c,r,s,m;
for(c=0; c < coreg->ref->width; c++){
for(r=0; r < coreg->ref->width; r++){
for(s=0; s < coreg->ref->width; s++){
m = MRIgetVoxVal(coreg->refmask,c,r,s,0);
if(m < 0.5) MRIsetVoxVal(coreg->ref,c,r,s,0,0.0);
}
}
}
}
if(cmdargs->SmoothRef){
double stdc,stdr,stds;
stdc = cmdargs->fwhmc/sqrt(log(256.0));
stdr = cmdargs->fwhmr/sqrt(log(256.0));
stds = cmdargs->fwhms/sqrt(log(256.0));
printf("Smoothing ref by FWHM %5.2lf %5.2lf %5.2lf\n",cmdargs->fwhmc,cmdargs->fwhmr,cmdargs->fwhms);
MRIgaussianSmoothNI(coreg->ref, stdc, stdr, stds, coreg->ref);
}
if(cmdargs->movmask){
printf("Reading in movmask %s\n",cmdargs->movmask);
coreg->movmask = MRIread(cmdargs->movmask);
if(!coreg->movmask) exit(1);
int c,r,s,m;
for(c=0; c < coreg->mov->width; c++){
for(r=0; r < coreg->mov->width; r++){
for(s=0; s < coreg->mov->width; s++){
m = MRIgetVoxVal(coreg->movmask,c,r,s,0);
if(m < 0.5) MRIsetVoxVal(coreg->mov,c,r,s,0,0.0);
}
}
}
}
if(cmdargs->cras0 || cmdargs->AlignCentroids){
MATRIX *Vref, *Vmov, *Imidref, *Imidmov, *Pmidref, *Pmidmov;
double xc; double yc; double zc;
// Compute the location of the voxel in ref
if(cmdargs->AlignCentroids){
printf("Setting cras translation parameters to align centroids\n");
MRIcomputeCentroid(coreg->ref,&xc,&yc,&zc);
printf("Ref CRS centroid %g %g %g\n",xc,yc,zc);
}
else{
printf("Setting cras translation parameters to align volume centers\n");
xc = (coreg->ref->width-1)/2.0;
yc = (coreg->ref->height-1)/2.0;
zc = (coreg->ref->depth-1)/2.0;
}
Vref = MRIxfmCRS2XYZ(coreg->ref, 0);
Imidref = MatrixAlloc(4,1,MATRIX_REAL);
Imidref->rptr[1][1] = xc;
Imidref->rptr[2][1] = yc;
Imidref->rptr[3][1] = zc;
Imidref->rptr[4][1] = 1;
Pmidref = MatrixMultiply(Vref,Imidref,NULL);
// Compute the location of the voxel in mov
if(cmdargs->AlignCentroids){
MRIcomputeCentroid(coreg->mov,&xc,&yc,&zc);
printf("Mov CRS centroid %g %g %g\n",xc,yc,zc);
}
else{
xc = (coreg->mov->width-1)/2.0;
yc = (coreg->mov->height-1)/2.0;
zc = (coreg->mov->depth-1)/2.0;
}
Vmov = MRIxfmCRS2XYZ(coreg->mov, 0);
Imidmov = MatrixAlloc(4,1,MATRIX_REAL);
Imidmov->rptr[1][1] = xc;
Imidmov->rptr[2][1] = yc;
Imidmov->rptr[3][1] = zc;
Imidmov->rptr[4][1] = 1;
Pmidmov = MatrixMultiply(Vmov,Imidmov,NULL);
// Set translation to make them equal
cmdargs->params[0] = Pmidmov->rptr[1][1] - Pmidref->rptr[1][1];
cmdargs->params[1] = Pmidmov->rptr[2][1] - Pmidref->rptr[2][1];
cmdargs->params[2] = Pmidmov->rptr[3][1] - Pmidref->rptr[3][1];
MatrixFree(&Vref);
MatrixFree(&Vmov);
MatrixFree(&Imidref);
MatrixFree(&Imidmov);
MatrixFree(&Pmidref);
MatrixFree(&Pmidmov);
}
coreg->histfwhm[0] = 7;
coreg->histfwhm[1] = 7;
coreg->nparams = cmdargs->dof;
coreg->nitersmax = cmdargs->nitersmax;
coreg->ftol = cmdargs->ftol;
coreg->linmintol = cmdargs->linmintol;
coreg->SatPct = cmdargs->SatPct;
coreg->DoCoordDither = cmdargs->DoCoordDither;
coreg->DoIntensityDither = cmdargs->DoIntensityDither;
coreg->refirfs = NULL;
coreg->movirfs = NULL;
coreg->DoSmoothing = cmdargs->DoSmoothing;
coreg->MovOOBFlag = cmdargs->MovOOBFlag;
coreg->optschema = cmdargs->optschema;
coreg->debug = debug;
// Initial parameters
printf("Init matrix params ");
for(n=0; n < 12; n++){
printf("%7.4lf ",cmdargs->params[n]);
coreg->mparams[n] = cmdargs->params[n];
}
printf("\n");
{
// Get the initial reg
MATRIX *T = TranformAffineParams2Matrix(cmdargs->params, NULL);
MatrixInverse(T,T);
LTA *lta = LTAcreate(coreg->mov, coreg->ref, T, LINEAR_RAS_TO_RAS);
if(cmdargs->subject) strncpy(lta->subject,cmdargs->subject,sizeof(lta->subject)-1);
else strncpy(lta->subject,"unknown", sizeof(lta->subject)-1);
printf("Initial Matrix (RAS2RAS)\n");
MatrixPrint(stdout,T);
if(cmdargs->InitRegSave){
int err = LTAwrite(lta,cmdargs->InitRegSave);
if(err) exit(err);
if(cmdargs->InitRegSaveOnly){
printf("--init-reg-save-only requested so exiting now\n");
exit(0);
}
}
}
COREGmatrixPar2OptSchema(coreg, cmdargs->params);
printf("Initial parameters to be opt ");
for(n=0; n < cmdargs->dof; n++) printf("%7.4lf ",coreg->params[n]);
printf("\n");
if(coreg->DoCoordDither){
// Creating a dither volume is needed for thread safety
printf("Creating random numbers for coordinate dithering\n");
coreg->crfs = RFspecInit(coreg->seed,NULL);
coreg->crfs->name = strcpyalloc("uniform");
coreg->crfs->params[0] = 0;
coreg->crfs->params[1] = 1;
coreg->cdither = MRIallocSequence(coreg->ref->width, coreg->ref->height, coreg->ref->depth, MRI_FLOAT,3);
RFsynth(coreg->cdither, coreg->crfs, NULL);
}
else printf("NOT Creating random numbers for coordinate dithering\n");
if(coreg->DoIntensityDither){
printf("Performing intensity dithering\n");
coreg->refirfs = RFspecInit(coreg->seed,NULL);
coreg->refirfs->name = strcpyalloc("uniform");
coreg->refirfs->params[0] = 0;
coreg->refirfs->params[1] = 1;
if(coreg->movidither){
printf("Performing intensity dithering on mov with input dither volume\n");
coreg->movirfs = NULL;
}
else {
printf("Performing intensity dithering on mov with computed dither\n");
coreg->movirfs = RFspecInit(coreg->seed,NULL);
coreg->movirfs->name = strcpyalloc("uniform");
}
}
else printf("NOT Performing intensity dithering\n");
fflush(stdout);
coreg->nsep = cmdargs->nsep;
coreg->sepmin = 100;
for(n=0; n < cmdargs->nsep; n++){
coreg->seplist[n] = cmdargs->seplist[n];
if(coreg->seplist[n] < coreg->sepmin) coreg->sepmin = coreg->seplist[n];
}
COREGprint(stdout, coreg);
COREGpreproc(coreg);
if(cmdargs->movoutfile){
printf("Saving mov to %s\n",cmdargs->movoutfile);
MRIwrite(coreg->mov,cmdargs->movoutfile);
}
if(cmdargs->logcost){
coreg->fplogcost = fopen(cmdargs->logcost,"w");
if(coreg->fplogcost == NULL){
printf("ERROR: could not open %s for writing\n",cmdargs->logcost);
exit(1);
}
}
printf("Testing if mov and target overlap\n");
coreg->sep = 4;
COREGcost(coreg);
printf("Init cost %15.10lf\n",coreg->cost);
printf("nhits = %d out of %d, Percent Overlap: %5.1f\n",coreg->nhits,coreg->nvoxref,coreg->pcthits);
if(coreg->nhits == 0){
printf("ERROR: mov and ref do not overlap\n");
exit(1);
}
printf("Initial RefRAS-to-MovRAS\n");
MatrixPrint(stdout,coreg->M);
printf("Initial RefVox-to-MovVox\n");
MatrixPrint(stdout,coreg->V2V);
if(cmdargs->DoInitCostOnly) {
FILE *fp = fopen(cmdargs->DoInitCostOnly,"w");
fprintf(fp,"%20.10f\n",coreg->cost);
fclose(fp);
exit(0);
}
for(n=0; n < coreg->nsep; n++){
coreg->sep = coreg->seplist[n];
printf("sep = %d -----------------------------------\n",coreg->sep);
if(n==0 && cmdargs->DoBF) COREGoptBruteForce(coreg, cmdargs->BFLim, 1, cmdargs->BFNSamp);
coreg->startmin = 1;
COREGMinPowell();
}
if(coreg->fplogcost) fclose(coreg->fplogcost);
MATRIX *invV2V;
invV2V = MatrixInverse(coreg->V2V,NULL);
lta = LTAcreate(coreg->mov, coreg->ref, invV2V,cmdargs->ltatype);
if(cmdargs->subject) strncpy(lta->subject,cmdargs->subject,sizeof(lta->subject)-1);
else strncpy(lta->subject,"unknown", sizeof(lta->subject)-1);
lta->subject[sizeof(lta->subject)-1] = 0;
err = LTAwrite(lta,cmdargs->outreg);
if(err) exit(1);
if(cmdargs->regdat){
LTAchangeType(lta,REGISTER_DAT);
err = LTAwrite(lta,cmdargs->regdat);
if(err) exit(1);
}
printf("Final RefRAS-to-MovRAS\n");
MatrixPrint(stdout,coreg->M);
printf("Final RefVox-to-MovVox\n");
MatrixPrint(stdout,coreg->V2V);
printf("Final matrix parameters ");
double *mpar = COREGoptSchema2MatrixPar(coreg, NULL);
for(n=0; n < 12; n++) printf("%7.4lf ",mpar[n]);
printf("\n");
printf("Final opt parameters ");
for(n=0; n < coreg->nparams; n++) printf("%7.4lf ",coreg->params[n]);
printf("\n");
if(cmdargs->outparamfile){
FILE *fp;
fp = fopen(cmdargs->outparamfile,"w");
// Not sure which set to write out here; keeping opt for now to maint conist
//for(n=0; n < 12; n++) fprintf(fp,"%15.8lf ",mpar[n]);
for(n=0; n < coreg->nparams; n++) fprintf(fp,"%7.4lf ",coreg->params[n]);
fprintf(fp,"\n");
fclose(fp);
}
if(cmdargs->outcostfile){
FILE *fp;
fp = fopen(cmdargs->outcostfile,"w");
fprintf(fp,"%20.13lf ",coreg->cost);
fprintf(fp,"\n");
fclose(fp);
}
printf("nhits = %d out of %d, Percent Overlap: %5.1f\n",coreg->nhits,coreg->nvoxref,coreg->pcthits);
printf("mri_coreg RunTimeSec %4.1f sec\n",timer.seconds());
if(! cmdargs->refconf){
printf("To check run:\n");
printf(" tkregisterfv --mov %s --targ %s --reg %s ",cmdargs->mov,cmdargs->ref,cmdargs->outreg);
if(cmdargs->subject) printf("--s %s --surfs ",cmdargs->subject);
}
printf("\n\n");
printf("mri_coreg done\n\n");
exit(0);
}
/* -------------------------------------------------------- */
static int parse_commandline(int argc, char **argv) {
int nargc , nargsused;
char **pargv, *option ;
if (argc < 1) usage_exit();
nargc = argc;
pargv = argv;
while (nargc > 0) {
option = pargv[0];
if (debug) printf("%d %s\n",nargc,option);
nargc -= 1;
pargv += 1;
nargsused = 0;
if (!strcasecmp(option, "--help")) print_help() ;
else if (!strcasecmp(option, "--version")) print_version() ;
else if (!strcasecmp(option, "--debug")) debug = 1;
else if (!strcasecmp(option, "--checkopts")) checkoptsonly = 1;
else if (!strcasecmp(option, "--nocheckopts")) checkoptsonly = 0;
else if (!strcasecmp(option, "--init-cost-only")){
if(nargc < 1) CMDargNErr(option,1);
cmdargs->DoInitCostOnly = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--no-smooth")) cmdargs->DoSmoothing = 0;
else if (!strcasecmp(option, "--cras0")) {
cmdargs->cras0 = 1;
cmdargs->AlignCentroids = 0;
}
else if (!strcasecmp(option, "--centroid")) {
cmdargs->AlignCentroids = 1;
cmdargs->cras0 = 0;
}
else if (!strcasecmp(option, "--no-cras0")) cmdargs->cras0 = 0;
else if (!strcasecmp(option, "--ras2ras")) cmdargs->ltatype = LINEAR_RAS_TO_RAS;
else if (!strcasecmp(option, "--regheader")) cmdargs->cras0 = 0;
else if (!strcasecmp(option, "--conf-ref")) cmdargs->refconf = 1;
else if (!strcasecmp(option, "--bf")) cmdargs->DoBF = 1;
else if (!strcasecmp(option, "--no-bf")) cmdargs->DoBF = 0;
else if (!strcasecmp(option, "--mov-oob")) cmdargs->MovOOBFlag = 1;
else if (!strcasecmp(option, "--no-mov-oob")) cmdargs->MovOOBFlag = 0;
else if (!strcasecmp(option, "--rusage")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->rusagefile = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--seed")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%d",&cmdargs->seed);
nargsused = 1;
}
else if (!strcasecmp(option, "--mov")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->mov = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--movout")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->movoutfile = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--mov-idither")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->moviditherfile = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--ref") || !strcasecmp(option, "--targ")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->ref = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--ref-mask")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->refmask = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--no-ref-mask")) cmdargs->refmask = NULL;
else if (!strcasecmp(option, "--mov-mask")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->movmask = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--reg") || !strcasecmp(option, "--lta")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->outreg = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--regdat")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->regdat = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--params")){
if(nargc < 1) CMDargNErr(option,1);
cmdargs->outparamfile = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--final-cost")){
if(nargc < 1) CMDargNErr(option,1);
cmdargs->outcostfile = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--log-cost")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->logcost = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--s")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->subject = pargv[0];
cmdargs->refmask = "aparc+aseg.mgz";
nargsused = 1;
}
else if(!strcmp(option, "--sd") || !strcmp(option, "-SDIR")) {
if(nargc < 1) CMDargNErr(option,1);
setenv("SUBJECTS_DIR",pargv[0],1);
nargsused = 1;
}
else if (!strcasecmp(option, "--sat")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%lf",&cmdargs->SatPct);
nargsused = 1;
}
else if (!strcasecmp(option, "--dof")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%d",&cmdargs->dof);
cmdargs->optschema = 1;
nargsused = 1;
}
else if (!strcasecmp(option, "--xztrans+yrot") || !strcasecmp(option, "--2dz")) {
cmdargs->optschema = 2;
cmdargs->dof = 3;
}
else if (!strcasecmp(option, "--xytrans+zrot")) {
cmdargs->optschema = 4;
cmdargs->dof = 3;
}
else if (!strcasecmp(option, "--xytrans+zrot+xyscale+xyshear")) {
cmdargs->optschema = 5;
cmdargs->dof = 6;
}
else if (!strcasecmp(option, "--zscale")) {
cmdargs->optschema = 3;
cmdargs->dof = 7;
}
else if (!strcasecmp(option, "--bf-lim")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->DoBF = 1;
sscanf(pargv[0],"%lf",&cmdargs->BFLim);
nargsused = 1;
}
else if (!strcasecmp(option, "--bf-nsamp")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->DoBF = 1;
sscanf(pargv[0],"%d",&cmdargs->BFNSamp);
nargsused = 1;
}
else if (!strcasecmp(option, "--6")) cmdargs->dof = 6;
else if (!strcasecmp(option, "--9")) cmdargs->dof = 9;
else if (!strcasecmp(option, "--12")) cmdargs->dof = 12;
else if (!strcasecmp(option, "--nitersmax")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%d",&cmdargs->nitersmax);
nargsused = 1;
}
else if (!strcasecmp(option, "--mat2par")) {
if(nargc < 1) CMDargNErr(option,1);
LTA *lta;
lta = LTAread(pargv[0]);
if(lta==NULL) exit(1);
LTAchangeType(lta,LINEAR_RAS_TO_RAS);
LTAinvert(lta,lta);
double p[12];
TranformExtractAffineParams(lta->xforms[0].m_L,p);
for(int k=0; k < 12; k++) printf("%g ",p[k]);
printf("\n");
nargsused = 1;
exit(0);
}
else if (!strcasecmp(option, "--mat2rot")) {
if(nargc < 2) CMDargNErr(option,2);
LTA *lta;
lta = LTAread(pargv[0]);
if(lta==NULL) exit(1);
LTAmat2RotMat(lta);
int err = LTAwrite(lta,pargv[1]);
nargsused = 2;
exit(err);
}
else if (!strcasecmp(option, "--par2mat")) {
if(nargc < 15) CMDargNErr(option,15);
double par[12];
int k;
for(k=0; k<12; k++) sscanf(pargv[k],"%lf",&par[k]);
for(k=0; k<12; k++) printf("%lf\n",par[k]);
MRI *mrisrc, *mritarg;
mrisrc = MRIread(pargv[12]);
if(mrisrc==NULL) exit(1);
mritarg = MRIread(pargv[13]);
if(mritarg==NULL) exit(1);
MATRIX *T = TranformAffineParams2Matrix(par, NULL);
MatrixInverse(T,T);
LTA *lta = LTAcreate(mrisrc, mritarg, T, LINEAR_RAS_TO_RAS);
if(cmdargs->subject) strncpy(lta->subject,cmdargs->subject,sizeof(lta->subject)-1);
else strncpy(lta->subject,"unknown", sizeof(lta->subject)-1);
int err = LTAwrite(lta,pargv[14]);
MRIfree(&mrisrc);
MRIfree(&mritarg);
MatrixFree(&T);
LTAfree(<a);
exit(err);
}
else if (!strcasecmp(option, "--lrrev")) {
/* This will give an approximate registration for when the
pixels in the input image are left-right reversed (eg, with
mri_convert --left-right-reverse-pix). It approximates the
registration you would get if you registered the LR reversed
input to the target. It would only be exact if the input image
were truly symetrical.
*/
if(nargc < 2) CMDargNErr(option,2);
LTA *lta;
lta = LTAread(pargv[0]);
if(lta==NULL) exit(1);
int srctype = lta->type;
LTAchangeType(lta,LINEAR_RAS_TO_RAS);
// Have to compute where the RAS in the output space moves when
// flipped to get the x offset.
// Compute RAS in the input space for A0 = [0 0 0 1]' = center in output space
// P0 = inv(T)*A0 where T is the reg
// Compute the col,row,slice in the input space
// K0 = ras2vox*P0 is the
// Compute where this point goes to when flipping
// K2 = K0 except in the dim being flipped where it is K2 = (ndim-1)-K0
// Compute where this point is in RAS
// P2 = vox2ras*K2 -- the value of the x offset becomes P2 in the flipping dim
MATRIX *Tinv0 = MatrixInverse(lta->xforms[0].m_L,NULL);
MATRIX *Mvox2ras = vg_i_to_r(<a->xforms[0].src);
MATRIX *Mras2vox = vg_r_to_i(<a->xforms[0].src);
MATRIX *M = MatrixMultiply(Mras2vox,Tinv0,NULL);
MRI *mri = MRIallocFromVolGeom(<a->xforms[0].src, MRI_UCHAR, 1, 1);
char ostr[5]; ostr[4] = '\0';
MRIdircosToOrientationString(mri,ostr);
printf("Orientation %s\n",ostr);
int k = -1, ndim=0;
// Determine which dimension was flipped
if(ostr[0] == 'L' || ostr[0] == 'R') {k = 1; ndim=lta->xforms[0].src.width;}
if(ostr[1] == 'L' || ostr[1] == 'R') {k = 2; ndim=lta->xforms[0].src.height;}
if(ostr[2] == 'L' || ostr[2] == 'R') {k = 3; ndim=lta->xforms[0].src.depth;}
printf("k=%d ndim=%d\n",k,ndim);
MATRIX *K2 = MatrixAlloc(4,1,MATRIX_REAL);
for(int n=1; n <=4 ; n++) K2->rptr[n][1] = M->rptr[n][4];
K2->rptr[k][1] = (ndim-1) - K2->rptr[k][1] ;
MATRIX *P2 = MatrixMultiply(Mvox2ras,K2,NULL);
// Get affine parameters for new matrix based on above and the src params
LTAinvert(lta,lta); // Not entirely sure why I need this here, but see --mat2par
double p[12];
TranformExtractAffineParams(lta->xforms[0].m_L,p);
printf("Input Parameters: ");
for(int k=0; k < 12; k++) printf("%g ",p[k]);
printf("\n");
p[0] = P2->rptr[1][1]; // shift in x
p[4] = -p[4]; // rotation about y
p[5] = -p[5]; // rotation about z
p[9] = -p[9]; // xy shear
p[10] = -p[10]; // xz shear
// Leave the others as they are
printf("New Parameters: ");
for(int k=0; k < 12; k++) printf("%g ",p[k]);
printf("\n");
// Now create a matrix from the parameters
MATRIX *T = TranformAffineParams2Matrix(p, NULL);
//MatrixInverse(T,T); // This was a bug, need to use LTAinvert() as now done below
MatrixCopy(T,lta->xforms[0].m_L);
LTAinvert(lta,lta); // Again with the inverse
LTAchangeType(lta,srctype);
int err = LTAwrite(lta,pargv[1]);
nargsused = 2;
exit(err);
}
else if (!strcasecmp(option, "--landmarks")) {
// --landmarks sxyzfile txyzfile coords mov targ outreg
if(nargc < 6) CMDargNErr(option,6);
RegLandmarks rlm;
rlm.sxyzfile = pargv[0];
rlm.txyzfile = pargv[1];
rlm.coordtypename = pargv[2];
rlm.mrisrcfile = pargv[3];
rlm.mritrgfile = pargv[4];
LTA *lta = rlm.ComputeLTA();
if(lta == NULL) exit(1);
if(cmdargs->subject) strncpy(lta->subject,cmdargs->subject,sizeof(lta->subject)-1);
else strncpy(lta->subject,"unknown", sizeof(lta->subject)-1);
int err = LTAwrite(lta,pargv[5]);
fsPointSet ps = rlm.Coords2PointSet(lta);
if(nargc == 7) ps.save(pargv[6]);
//rlm.WriteCoords("mycoords.dat",lta);
exit(err);
}
else if (!strcasecmp(option, "--rms")) {
if(nargc < 2) CMDargNErr(option,4);
double rms,RMSDiffRad;
FILE *fp;
LTA *lta1,*lta2;
char *RMSDiffFile;
if(isalpha(pargv[0][0])){
printf("ERROR: first arg to --rms must be the radius\n");
exit(1);
}
sscanf(pargv[0],"%lf",&RMSDiffRad);
if(RMSDiffRad <= 0){
printf("ERROR: radius is %lf, must be > 0\n",RMSDiffRad);
exit(1);
}
RMSDiffFile = pargv[1];
lta1 = LTAread(pargv[2]); if(lta1==NULL) exit(1);
LTAchangeType(lta1,REGISTER_DAT);
lta2 = LTAread(pargv[3]); if(lta2==NULL) exit(1);
LTAchangeType(lta2,REGISTER_DAT);
rms = RMSregDiffMJ(lta1->xforms[0].m_L, lta2->xforms[0].m_L, RMSDiffRad);
if(strcmp(RMSDiffFile,"nofile") != 0){
fp = fopen(RMSDiffFile,"w");
fprintf(fp,"%20.10lf\n",rms);
fclose(fp);
}
printf("rms %20.10lf\n",rms);
exit(0);
}
else if (!strcasecmp(option, "--ftol")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%lf",&cmdargs->ftol);
nargsused = 1;
}
else if (!strcasecmp(option, "--linmintol")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%lf",&cmdargs->linmintol);
nargsused = 1;
}
else if (!strcasecmp(option, "--sep")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%d",&cmdargs->seplist[cmdargs->nsep]);
cmdargs->nsep++;
nargsused = 1;
}
else if (!strcasecmp(option, "--ref-fwhm")) {
if(nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%lf",&cmdargs->fwhmc);
cmdargs->fwhmr = cmdargs->fwhmc;
cmdargs->fwhms = cmdargs->fwhmc;
cmdargs->SmoothRef = 1;
cmdargs->DoSmoothing = 0;
nargsused = 1;
}
else if (!strcasecmp(option, "--trans")) {
if(nargc < 3) CMDargNErr(option,3);
sscanf(pargv[0],"%lf",&cmdargs->params[0]);
sscanf(pargv[1],"%lf",&cmdargs->params[1]);
sscanf(pargv[2],"%lf",&cmdargs->params[2]);
cmdargs->cras0 = 0;
nargsused = 3;
}
else if (!strcasecmp(option, "--rot")) {
if(nargc < 3) CMDargNErr(option,3);
sscanf(pargv[0],"%lf",&cmdargs->params[3]);
sscanf(pargv[1],"%lf",&cmdargs->params[4]);
sscanf(pargv[2],"%lf",&cmdargs->params[5]);
nargsused = 3;
}
else if (!strcasecmp(option, "--scale")) {
if(nargc < 3) CMDargNErr(option,3);
sscanf(pargv[0],"%lf",&cmdargs->params[6]);
sscanf(pargv[1],"%lf",&cmdargs->params[7]);
sscanf(pargv[2],"%lf",&cmdargs->params[8]);
nargsused = 3;
}
else if (!strcasecmp(option, "--shear")) {
if(nargc < 3) CMDargNErr(option,3);
sscanf(pargv[0],"%lf",&cmdargs->params[9]);
sscanf(pargv[1],"%lf",&cmdargs->params[10]);
sscanf(pargv[2],"%lf",&cmdargs->params[11]);
nargsused = 3;
}
else if (!strcasecmp(option, "--init-reg")) {
if(nargc < 1) CMDargNErr(option,1);
LTA *lta;
lta = LTAread(pargv[0]);
if(lta==NULL) exit(1);
LTAchangeType(lta,LINEAR_RAS_TO_RAS);
LTAinvert(lta,lta);
TranformExtractAffineParams(lta->xforms[0].m_L,cmdargs->params);
cmdargs->cras0 = 0;
nargsused = 1;
}
else if (!strcasecmp(option, "--init-reg-save")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->InitRegSave = pargv[0];
nargsused = 1;
}
else if (!strcasecmp(option, "--init-reg-save-only")) {
if(nargc < 1) CMDargNErr(option,1);
cmdargs->InitRegSave = pargv[0];
cmdargs->InitRegSaveOnly = 1;
nargsused = 1;
}
else if(!strcmp(option, "--no-coord-dither")) cmdargs->DoCoordDither = 0;
else if(!strcmp(option, "--no-intensity-dither")) cmdargs->DoIntensityDither = 0;
else if(!strcmp(option, "--no-dither")){
cmdargs->DoCoordDither = 0;
cmdargs->DoIntensityDither = 0;
}
else if(!strcasecmp(option, "--threads") || !strcasecmp(option, "--nthreads") ){
if(nargc < 1) CMDargNErr(option,1);
int nthreads;
sscanf(pargv[0],"%d",&nthreads);
#ifdef HAVE_OPENMP
omp_set_num_threads(nthreads);
#endif
nargsused = 1;
}
else if (!strcasecmp(option, "--diag")) {
if (nargc < 1) CMDargNErr(option,1);
sscanf(pargv[0],"%d",&Gdiag_no);
nargsused = 1;
}
else if (!strcasecmp(option, "--diag-show")) Gdiag = (Gdiag & DIAG_SHOW);
else if (!strcasecmp(option, "--diag-verbose")) Gdiag = (Gdiag & DIAG_VERBOSE);
else {
fprintf(stderr,"ERROR: Option %s unknown\n",option);
if (CMDsingleDash(option))
fprintf(stderr," Did you really mean -%s ?\n",option);
exit(-1);
}
nargc -= nargsused;
pargv += nargsused;
}