/* -------------------------------------------------------------- regio_read_xfm4() - reads a 4x4 transform as the last four lines of the xfmfile. Blank lines at the end will defeat it. -------------------------------------------------------------- */ int regio_read_xfm4(char *xfmfile, MATRIX **R) { FILE *fp; char tmpstr[1000]; int r,c,n,nlines; float val; memset(tmpstr,'\0',1000); fp = fopen(xfmfile,"r"); if (fp==NULL) { perror("regio_read_xfm4"); fprintf(stderr,"Could read %s\n",xfmfile); return(1); } /* Count the number of lines */ nlines = 0; while (fgets(tmpstr,1000,fp) != NULL) nlines ++; rewind(fp); /* skip all but the last 3 lines */ for (n=0;n<nlines-4;n++) fgets(tmpstr,1000,fp); *R = MatrixAlloc(4,4,MATRIX_REAL); if (*R == NULL) { fprintf(stderr,"regio_read_xfm4(): could not alloc R\n"); fclose(fp); return(1); } MatrixClear(*R); /* registration matrix */ for (r=0;r<3;r++) { /* note: upper limit = 3 for xfm */ for (c=0;c<4;c++) { n = fscanf(fp,"%f",&val); if (n != 1) { perror("regio_read_xfm4()"); fprintf(stderr,"Error reading R[%d][%d] from %s\n",r,c,xfmfile); fclose(fp); return(1); } (*R)->rptr[r+1][c+1] = val; /*printf("%7.4f ",val);*/ } /*printf("\n");*/ } (*R)->rptr[3+1][3+1] = 1.0; fclose(fp); return(0); }
void MatrixQRDecompColMajor(Matrix*mtR,Matrix*mt){ // gram-schmidt orthonormalization (Lt and Q) double t, *aT[mt->W]; int W = mt->W; int i,j; MatrixClear(mtR); for (i = 0; i < W;i++) { aT[i] = Row(mt,i); for (j = 0; j < i; j++) { Elem(mtR,j,i) = t = VP(aT[j], aT[i], W); VSA(aT[i], aT[j], -t, W); } Elem(mtR,i,i) = t = sqrt(VP(aT[i],aT[i],W)); VSS(aT[i], 1/t, W); } /* Elem(mtR,0,0) = t = sqrt(VP(aT[0],aT[0],W)); VSS(aT[0], 1/t, W); Elem(mtR,0,1) = t = VP(aT[0], aT[1], W); VSA(aT[1], aT[0], -t, W); Elem(mtR,1,1) = t = sqrt(VP(aT[1],aT[1],W)); VSS(aT[1], 1/t, W); /////////// Elem(mtR,0,2) = t = VP(aT[0], aT[2], W); VSA(aT[2], aT[0], -t, W); Elem(mtR,1,2) = t = VP(aT[1], aT[2], W); VSA(aT[2], aT[1], -t, W); Elem(mtR,2,2) = t = sqrt(VP(aT[2],aT[2],W)); VSS(aT[2], 1/t, W); ////// 以下略 */ }
static int compute_cluster_statistics(MRI_SURFACE *mris, MRI *mri_profiles, MATRIX **m_covs, VECTOR **v_means, int k) { int i, vno, cluster, nsamples, num[MAX_CLUSTERS]; int singular, cno_pooled, cno ; MATRIX *m1, *mpooled, *m_inv_covs[MAX_CLUSTERS] ; VECTOR *v1 ; FILE *fp ; double det, det_pooled ; memset(num, 0, sizeof(num)) ; nsamples = mri_profiles->nframes ; v1 = VectorAlloc(nsamples, MATRIX_REAL) ; m1 = MatrixAlloc(nsamples, nsamples, MATRIX_REAL) ; mpooled = MatrixAlloc(nsamples, nsamples, MATRIX_REAL) ; for (cluster = 0 ; cluster < k ; cluster++) { VectorClear(v_means[cluster]) ; MatrixClear(m_covs[cluster]) ; } // compute means // fp = fopen("co.dat", "w") ; fp = NULL ; for (vno = 0 ; vno < mris->nvertices ; vno++) { cluster = mris->vertices[vno].curv ; for (i = 0 ; i < nsamples ; i++) { VECTOR_ELT(v1, i+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, i) ; if (cluster == 0 && fp) fprintf(fp, "%f ", VECTOR_ELT(v1, i+1)); } if (cluster == 0 && fp) fprintf(fp, "\n") ; num[cluster]++ ; VectorAdd(v_means[cluster], v1, v_means[cluster]) ; } if (fp) fclose(fp) ; for (cluster = 0 ; cluster < k ; cluster++) if (num[cluster] > 0) VectorScalarMul(v_means[cluster], 1.0/(double)num[cluster], v_means[cluster]) ; // compute inverse covariances for (vno = 0 ; vno < mris->nvertices ; vno++) { cluster = mris->vertices[vno].curv ; for (i = 0 ; i < nsamples ; i++) VECTOR_ELT(v1, i+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, i) ; VectorSubtract(v_means[cluster], v1, v1) ; VectorOuterProduct(v1, v1, m1) ; MatrixAdd(m_covs[cluster], m1, m_covs[cluster]) ; MatrixAdd(mpooled, m1, mpooled) ; } MatrixScalarMul(mpooled, 1.0/(double)mris->nvertices, mpooled) ; cno_pooled = MatrixConditionNumber(mpooled) ; det_pooled = MatrixDeterminant(mpooled) ; for (cluster = 0 ; cluster < k ; cluster++) if (num[cluster] > 0) MatrixScalarMul(m_covs[cluster], 1.0/(double)num[cluster], m_covs[cluster]) ; // invert all the covariance matrices MatrixFree(&m1) ; singular = 0 ; for (cluster = 0 ; cluster < k ; cluster++) { m1 = MatrixInverse(m_covs[cluster], NULL) ; cno = MatrixConditionNumber(m_covs[cluster]) ; det = MatrixDeterminant(m_covs[cluster]) ; if (m1 == NULL) singular++ ; while (cno > 100*cno_pooled || 100*det < det_pooled) { if (m1) MatrixFree(&m1) ; m1 = MatrixScalarMul(mpooled, 0.1, NULL) ; MatrixAdd(m_covs[cluster], m1, m_covs[cluster]) ; MatrixFree(&m1) ; cno = MatrixConditionNumber(m_covs[cluster]) ; m1 = MatrixInverse(m_covs[cluster], NULL) ; det = MatrixDeterminant(m_covs[cluster]) ; } m_inv_covs[cluster] = m1 ; } for (cluster = 0 ; cluster < k ; cluster++) { if (m_inv_covs[cluster] == NULL) DiagBreak() ; else { MatrixFree(&m_covs[cluster]) ; m_covs[cluster] = m_inv_covs[cluster] ; // MatrixIdentity(m_covs[cluster]->rows, m_covs[cluster]); } } MatrixFree(&mpooled) ; VectorFree(&v1) ; return(NO_ERROR) ; }
/* -------------------------------------------------------------- regio_read_mincxfm() - reads a 3x4 transform as the last three lines of the xfmfile. Blank lines at the end will defeat it. If fileinfo != NULL, reads in the "fileinfo". This is the 3rd line in the minc xfm file. It will contain information about the center of the transform (-center). This is used by ltaMNIreadEx() to account for the non-zero center of the MNI talairach template. If the center infomation is not there, ltaMNIreadEx() assumes that the center is 0 and will produce the wrong transform. Thus, if one is going to write out the xfm, then one needs to keep track of this information when reading it in. regio_write_mincxfm() takes fileinfo as an argument. If one is not going to write out the xfm, then simply set fileinfo to NULL. -------------------------------------------------------------- */ int regio_read_mincxfm(char *xfmfile, MATRIX **R, char **fileinfo) { FILE *fp; char tmpstr[1000]; int r,c,n,nlines; float val; memset(tmpstr,'\0',1000); fp = fopen(xfmfile,"r"); if (fp==NULL) { perror("regio_read_mincxfm"); printf("ERROR: could not read %s\n",xfmfile); return(1); } fgetl(tmpstr, 900, fp) ; /* MNI Transform File */ if (strncmp("MNI Transform File", tmpstr, 18)) { printf("ERROR: %s does not start as 'MNI Transform File'",xfmfile); return(1); } fgetl(tmpstr, 900, fp) ; /* fileinfo */ if (fileinfo != NULL) { *fileinfo = strcpyalloc(tmpstr); printf("\n%s\n\n",*fileinfo); } else printf("Not reading in xfm fileinfo\n"); // Close it and open it up again to rewind it fclose(fp); fp = fopen(xfmfile,"r"); /* Count the number of lines */ nlines = 0; while (fgets(tmpstr,1000,fp) != NULL) nlines ++; rewind(fp); /* skip all but the last 3 lines */ for (n=0;n<nlines-3;n++) fgets(tmpstr,1000,fp); *R = MatrixAlloc(4,4,MATRIX_REAL); if (*R == NULL) { fprintf(stderr,"regio_read_mincxfm(): could not alloc R\n"); fclose(fp); return(1); } MatrixClear(*R); /* registration matrix */ for (r=0;r<3;r++) { /* note: upper limit = 3 for xfm */ for (c=0;c<4;c++) { n = fscanf(fp,"%f",&val); if (n != 1) { perror("regio_read_mincxfm()"); fprintf(stderr,"Error reading R[%d][%d] from %s\n",r,c,xfmfile); fclose(fp); return(1); } (*R)->rptr[r+1][c+1] = val; /*printf("%7.4f ",val);*/ } /*printf("\n");*/ } (*R)->rptr[3+1][3+1] = 1.0; fclose(fp); return(0); }