static int compute_rigid_gradient(MRI_SURFACE *mris, MRI *mri, double *pdx, double *pdy, double *pdz) { int vno ; VERTEX *v ; Real val, xw, yw, zw, dx, dy, dz, delV, x, y, z, Ix, Iy, Iz, xv, yv, zv ; dx = dy = dz = 0.0 ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; if (vno == Gdiag_no) DiagBreak() ; x = v->x ; y = v->y ; z = v->z ; MRISvertexToVoxel(mris, v, mri, &xw, &yw, &zw); MRIsampleVolume(mri, xw, yw, zw, &val) ; MRIsampleVolumeGradient(mri, xw, yw, zw, &Ix, &Iy, &Iz) ; // convert back to surface coords xw += Ix ; yw += Iy ; zw += Iz ; if (mris->useRealRAS) MRIvoxelToWorld(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIvoxelToSurfaceRAS(mri, xw, yw, zw, &xv, &yv, &zv) ; Ix = xv-v->x ; Iy = yv-v->y; Iz = zv-v->z ; delV = v->val - val ; dx += delV * Ix ; dy += delV * Iy ; dz += delV * Iz ; if (!finitep((float)dx)) DiagBreak() ; if (!finitep((float)dy)) DiagBreak() ; if (!finitep((float)dz)) DiagBreak() ; } dx /= mris->nvertices ; dy /= mris->nvertices ; dz /= mris->nvertices ; *pdx = dx ; *pdy = dy ; *pdz = dz ; return(NO_ERROR) ; }
void LayerFCD::UpdateRASImage( vtkImageData* rasImage) { if (!m_fcd) { return; } int n[3]; double pos[3]; int* dim = rasImage->GetDimensions(); memset( rasImage->GetScalarPointer(), 0, dim[0] * dim[1] * dim[2] * rasImage->GetScalarSize() ); if ( m_fcd->nlabels == 0 ) { cout << "No labels found\n"; return; } for (int cnt = 0; cnt < m_fcd->nlabels; cnt++) { LABEL* label = m_fcd->labels[cnt]; FSVolume* ref_vol = m_layerSource->GetSourceVolume(); for ( int i = 0; i < label->n_points; i++ ) { pos[0] = label->lv[i].x; pos[1] = label->lv[i].y; pos[2] = label->lv[i].z; if ( label->coords == LABEL_COORDS_VOXEL ) { MRIvoxelToWorld(ref_vol->GetMRI(), pos[0], pos[1], pos[2], pos, pos+1, pos+2); } else if (label->coords == LABEL_COORDS_TKREG_RAS) { ref_vol->TkRegToNativeRAS( pos, pos ); } ref_vol->NativeRASToRAS( pos, pos ); ref_vol->RASToTargetIndex( pos, n ); if ( n[0] >= 0 && n[0] < dim[0] && n[1] >= 0 && n[1] < dim[1] && n[2] >= 0 && n[2] < dim[2] ) { rasImage->SetScalarComponentFromFloat ( n[0], n[1], n[2], 0, label->lv[i].vno ); } } } }
void LayerFCD::SetLabelVisible(int nIndex, bool visible) { LABEL* label = m_fcd->labels[nIndex]; FSVolume* ref_vol = m_layerSource->GetSourceVolume(); int n[3]; double pos[3]; int* dim = m_imageData->GetDimensions(); for ( int i = 0; i < label->n_points; i++ ) { pos[0] = label->lv[i].x; pos[1] = label->lv[i].y; pos[2] = label->lv[i].z; if ( label->coords == LABEL_COORDS_VOXEL ) { MRIvoxelToWorld(ref_vol->GetMRI(), pos[0], pos[1], pos[2], pos, pos+1, pos+2); } else if (label->coords == LABEL_COORDS_TKREG_RAS) { ref_vol->TkRegToNativeRAS( pos, pos ); } ref_vol->NativeRASToRAS( pos, pos ); ref_vol->RASToTargetIndex( pos, n ); if ( n[0] >= 0 && n[0] < dim[0] && n[1] >= 0 && n[1] < dim[1] && n[2] >= 0 && n[2] < dim[2] ) { m_imageData->SetScalarComponentFromFloat ( n[0], n[1], n[2], 0, visible?label->lv[i].vno:0 ); } } for (int i = 0; i < 3; i++) { mReslice[i]->Modified(); } emit ActorUpdated(); }
void LayerFCD::GetLabelCentroidPosition(int nLabelIndex, double *pos) { if (nLabelIndex >= m_fcd->nlabels) { return; } LABEL* label = m_fcd->labels[nLabelIndex]; if (label->n_points > 0) { double x = 0, y = 0, z = 0; for ( int i = 0; i < label->n_points; i++ ) { x += label->lv[i].x; y += label->lv[i].y; z += label->lv[i].z; } pos[0] = x / label->n_points; pos[1] = y / label->n_points; pos[2] = z / label->n_points; FSVolume* ref_vol = m_layerSource->GetSourceVolume(); if ( label->coords == LABEL_COORDS_VOXEL ) { MRIvoxelToWorld(ref_vol->GetMRI(), pos[0], pos[1], pos[2], pos, pos+1, pos+2); } else if (label->coords == LABEL_COORDS_TKREG_RAS) { ref_vol->TkRegToNativeRAS( pos, pos ); } ref_vol->NativeRASToRAS( pos, pos ); m_layerSource->RASToTarget(pos, pos); } }
/* * check * * conduct a sanity check of particular labels, most importantly * hippocampus, that such labels do not exist in talairach coords * where they are known not to belong (indicating a bad manual edit) */ static int check(MRI *mri_seg, char *subjects_dir, char *subject_name) { int x, y, z, label, errors=0; Real xw=0.0, yw=0.0, zw=0.0; // RAS coords Real xmt=0.0, ymt=0.0, zmt=0.0; // MNI tal coords float xt=0.0, yt=0.0, zt=0.0; // 'real' tal coords MRI *mri_fixed = NULL; float max_xtal_l_hippo = -1000; float max_xtal_l_caudate = -1000; float max_xtal_l_amygdala = -1000; float max_xtal_l_putamen = -1000; float max_xtal_l_pallidum = -1000; float min_xtal_r_hippo = 1000; float min_xtal_r_caudate = 1000; float min_xtal_r_amygdala = 1000; float min_xtal_r_putamen = 1000; float min_xtal_r_pallidum = 1000; printf("checking labels in subject %s...\n",subject_name); if (do_fix_badsubjs) { mri_fixed = MRIcopy(mri_seg,NULL); } if (NULL == mri_seg->linear_transform) { ErrorExit(ERROR_BADFILE, "ERROR: mri_ca_train: talairach.xfm not found in %s!\n" "Run mri_add_xform_to_header to add to volume.\n", seg_dir); } // now conduct checks for voxels in locations where they shouldnt be for (x = 0 ; x < mri_seg->width ; x++) { for (y = 0 ; y < mri_seg->height ; y++) { for (z = 0 ; z < mri_seg->depth ; z++) { /* * rules: * - no label should have a voxel coord < 6 or > 249 * - no left or right hippo labels with z tal coord > 15 * - no left hippo, caudate, amydala, putamen or pallidum * labels with x tal coord > 5 (or right, with x tal coord < -5) */ label = MRIgetVoxVal(mri_seg, x, y, z, 0) ; int proper_label = label; // used for making corrections if (label != Unknown) { // note: these won't be caught if -mask brainmask.mgz is included // on the command-line, since voxels outside of brainmask get // labelled as Unknown. if ((x < 6) || (y < 6) || (z < 6) || (x > 249) || (y > 249) || (z > 249)) { printf ("ERROR: %s: found label outside of brain: " "%d %d %d\n", cma_label_to_name(label),x,y,z); fflush(stdout) ; errors++; proper_label = Unknown; } } if ((label == Left_Hippocampus) || (label == Right_Hippocampus) || (label == Left_Caudate) || (label == Right_Caudate) || (label == Left_Amygdala) || (label == Right_Amygdala) || (label == Left_Putamen) || (label == Right_Putamen) || (label == Left_Pallidum) || (label == Right_Pallidum) || (label == Left_Inf_Lat_Vent) || (label == Right_Inf_Lat_Vent)) { // the 'if' statement above spares some cpu cycles in having to // calculate the coord transform for every voxel, ie these 3 lines: MRIvoxelToWorld(mri_seg, x, y, z, &xw, &yw, &zw) ; transform_point(mri_seg->linear_transform, xw, yw, zw, &xmt, &ymt, &zmt);// get mni tal coords FixMNITal(xmt,ymt,zmt, &xt,&yt,&zt); // get 'real' tal coords switch (label) { case Left_Hippocampus: if (zt > 15) { printf ("ERROR: %s: " "%d %d %d, tal x=%f, y=%f, *** z=%f > 15 ***\n", cma_label_to_name(label),x,y,z,xt,yt,zt); fflush(stdout) ; errors++; } // no break (check xt) case Left_Caudate: case Left_Amygdala: case Left_Putamen: case Left_Pallidum: case Left_Inf_Lat_Vent: if (xt > 5) { printf ("ERROR: %s: " "%d %d %d, tal *** x=%f > 5 ***, y=%f, z=%f\n", cma_label_to_name(label), x,y,z,xt,yt,zt); fflush(stdout) ; errors++; if (label == Left_Hippocampus) proper_label = Right_Hippocampus; else if (label == Left_Caudate) proper_label = Right_Caudate; else if (label == Left_Amygdala) proper_label = Right_Amygdala; else if (label == Left_Putamen) proper_label = Right_Putamen; else if (label == Left_Pallidum) proper_label = Right_Pallidum; else if (label == Left_Inf_Lat_Vent) proper_label = Right_Inf_Lat_Vent; } break; case Right_Hippocampus: if (zt > 15) { printf ("ERROR: %s: " "%d %d %d, tal x=%f, y=%f, *** z=%f > 15 ***\n", cma_label_to_name(label),x,y,z,xt,yt,zt); fflush(stdout) ; errors++; } // no break (check xt) case Right_Caudate: case Right_Amygdala: case Right_Putamen: case Right_Pallidum: case Right_Inf_Lat_Vent: if (xt < -5) { printf ("ERROR: %s: " "%d %d %d, tal *** x=%f < -5 ***, y=%f, z=%f\n", cma_label_to_name(label), x,y,z,xt,yt,zt); fflush(stdout) ; errors++; if (label == Right_Hippocampus) proper_label = Left_Hippocampus; else if (label == Right_Caudate) proper_label = Left_Caudate; else if (label == Right_Amygdala) proper_label = Left_Amygdala; else if (label == Right_Putamen) proper_label = Left_Putamen; else if (label == Right_Pallidum) proper_label = Left_Pallidum; else if (label == Right_Inf_Lat_Vent) proper_label = Left_Inf_Lat_Vent; } break; default: break ; } /* * collect stats on positioning of structures. * used to determine reasonable boundaries. */ switch (label) { case Left_Hippocampus: if (xt > max_xtal_l_hippo) max_xtal_l_hippo = xt; break; case Left_Caudate: if (xt > max_xtal_l_caudate) max_xtal_l_caudate = xt; break; case Left_Amygdala: if (xt > max_xtal_l_amygdala) max_xtal_l_amygdala = xt; break; case Left_Putamen: if (xt > max_xtal_l_putamen) max_xtal_l_putamen = xt; break; case Left_Pallidum: if (xt > max_xtal_l_pallidum) max_xtal_l_pallidum = xt; break; case Right_Hippocampus: if (xt < min_xtal_r_hippo) min_xtal_r_hippo = xt; break; case Right_Caudate: if (xt < min_xtal_r_caudate) min_xtal_r_caudate = xt; break; case Right_Amygdala: if (xt < min_xtal_r_amygdala) min_xtal_r_amygdala = xt; break; case Right_Putamen: if (xt < min_xtal_r_putamen) min_xtal_r_putamen = xt; break; case Right_Pallidum: if (xt < min_xtal_r_pallidum) min_xtal_r_pallidum = xt; break; default: break ; } } /* * if -check_and_fix is being used, then mod our fixed volume */ if (do_fix_badsubjs && (label != proper_label)) { MRIsetVoxVal(mri_fixed, x, y, z, 0, proper_label) ; } } } } // stats used to determine optimal boundaries printf("max_xtal_l_hippo = %4.1f\n",max_xtal_l_hippo); printf("max_xtal_l_caudate = %4.1f\n",max_xtal_l_caudate); printf("max_xtal_l_amygdala = %4.1f\n",max_xtal_l_amygdala); printf("max_xtal_l_putamen = %4.1f\n",max_xtal_l_putamen); printf("max_xtal_l_pallidum = %4.1f\n",max_xtal_l_pallidum); printf("min_xtal_r_hippo = %4.1f\n",min_xtal_r_hippo); printf("min_xtal_r_caudate = %4.1f\n",min_xtal_r_caudate); printf("min_xtal_r_amygdala = %4.1f\n",min_xtal_r_amygdala); printf("min_xtal_r_putamen = %4.1f\n",min_xtal_r_putamen); printf("min_xtal_r_pallidum = %4.1f\n",min_xtal_r_pallidum); if ( do_fix_badsubjs && errors) { char fname[STRLEN]; sprintf(fname, "%s/%s/mri/seg_fixed.mgz", subjects_dir, subject_name); printf("Writing corrected volume to %s\n",fname); MRIwrite(mri_fixed,fname); MRIfree(&mri_fixed); } fflush(stdout); return(errors) ; }