/* FAST4 - FMRIB's Automated Segmentation Tool v4 John Vickers, Mark Jenkinson and Steve Smith FMRIB Image Analysis Group Copyright (C) 2005-2007 University of Oxford */ /* Part of FSL - FMRIB's Software Library http://www.fmrib.ox.ac.uk/fsl fsl@fmrib.ox.ac.uk Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance Imaging of the Brain), Department of Clinical Neurology, Oxford University, Oxford, UK LICENCE FMRIB Software Library, Release 5.0 (c) 2012, The University of Oxford (the "Software") The Software remains the property of the University of Oxford ("the University"). The Software is distributed "AS IS" under this Licence solely for non-commercial use in the hope that it will be useful, but in order that the University as a charitable foundation protects its assets for the benefit of its educational and research purposes, the University makes clear that no condition is made or to be implied, nor is any warranty given or to be implied, as to the accuracy of the Software, or that it will be suitable for any particular purpose or for use under any specific conditions. Furthermore, the University disclaims all responsibility for the use which is made of the Software. It further disclaims any liability for the outcomes arising from using the Software. The Licensee agrees to indemnify the University and hold the University harmless from and against any and all claims, damages and liabilities asserted by third parties (including claims for negligence) which arise directly or indirectly from the use of the Software or the sale of any products based on the Software. No part of the Software may be reproduced, modified, transmitted or transferred in any form or by any means, electronic or mechanical, without the express permission of the University. The permission of the University is not required if the said reproduction, modification, transmission or transference is done without financial return, the conditions of this Licence are imposed upon the receiver of the product, and all original and amended source code is included in any transmitted product. You may be held legally responsible for any copyright infringement that is caused or encouraged by your failure to abide by these terms and conditions. You are not permitted under this Licence to use this Software commercially. Use for which any financial return is received shall be defined as commercial use, and includes (1) integration of all or part of the source code or the Software into a product for sale or license by or on behalf of Licensee to third parties or (2) use of the Software or any derivative of it for research with the final aim of developing software products for sale or license to a third party or (3) use of the Software or any derivative of it for research with the final aim of developing non-software products for sale or license to a third party, or (4) use of the Software to provide any service to an external organisation for which payment is received. If you are interested in using the Software commercially, please contact Isis Innovation Limited ("Isis"), the technology transfer company of the University, to negotiate a licence. Contact details are: innovation@isis.ox.ac.uk quoting reference DE/9564. */ #include "multi_mriseg_two.h" #include "newimage/newimageall.h" using namespace NEWIMAGE; using namespace std; ZMRIMULTISegmentation::ZMRIMULTISegmentation(){} const float PI=3.14159265; void ZMRIMULTISegmentation::TanakaCreate(const NEWIMAGE::volume* images, int nclasses, bool b2d, int nbiter,float nblowpass, float fbeta, int bapused, bool pveboolean, int nchan, bool bbias, int initinitfixed, bool verb, int biterationspve, int winit, float fpveMRFmixeltyp, float Hyp,string mansegfle, int typeoffile) { mansegfile=mansegfle; verboseusage=verb; inititerations=winit; m_nbLowpass=nblowpass; numberofchannels=nchan; m_nWidth=images[0].xsize(); m_nHeight=images[0].ysize(); m_mean.ReSize(nclasses,numberofchannels); m_nDepth=images[0].zsize(); m_nxdim=images[0].xdim(); m_nydim=images[0].ydim(); m_nzdim=images[0].zdim(); m_co_variance=new Matrix[nclasses+1]; for(int i=1;i<=nclasses;i++) m_co_variance[i].ReSize(numberofchannels,numberofchannels); m_inv_co_variance=new Matrix[nclasses+1]; for(int i=1;i<=nclasses;i++) m_inv_co_variance[i].ReSize(numberofchannels,numberofchannels); m_Mricopy=new volume[numberofchannels+1]; imagetype=typeoffile; for(int i=1;i<=numberofchannels;i++) m_Mricopy[i]=volume(images[i-1]); bapusedflag=bapused; maximum=images[0].max(); minimum=images[0].min(); iterationspve=biterationspve; m_Mri=new volume[numberofchannels+1]; pveBmixeltype=fpveMRFmixeltyp; m_nSlicesize=m_nWidth*m_nHeight; noclasses=nclasses; m_nbIter=nbiter; m_post=volume4D(m_nWidth, m_nHeight, m_nDepth, noclasses+1); m_post.copyproperties(images[0]); m_prob=volume4D(m_nWidth, m_nHeight, m_nDepth, noclasses+1); m_prob.copyproperties(images[0]); members=volume4D(m_nWidth, m_nHeight, m_nDepth, noclasses); members.copyproperties(images[0]); m_mask=volume(m_nWidth, m_nHeight, m_nDepth); copybasicproperties(images[0], m_mask); m_maskc=volume(m_nWidth, m_nHeight, m_nDepth); m_maskc.copyproperties(images[0]); m_Segment=volume(m_nWidth, m_nHeight, m_nDepth); copybasicproperties(images[0], m_Segment); biasfieldremoval=bbias; beta=fbeta; if(true) { m_pveSegment=volume(m_nWidth, m_nHeight, m_nDepth); m_pveSegment.copyproperties(m_Segment); } initfixed=initinitfixed; m_nbIter=nbiter; if(bapusedflag>0) { talpriors=volume4D(m_nWidth, m_nHeight, m_nDepth, noclasses+1); talpriors.copyproperties(m_post); } Hyper=Hyp; } void ZMRIMULTISegmentation::Dimensions() { amz=1.0/m_nzdim; amy=1.0/m_nydim; amzy=1.0/sqrt(m_nzdim*m_nzdim+m_nydim*m_nydim); amx=1.0/m_nxdim; amzx=1.0/sqrt(m_nzdim*m_nzdim+m_nxdim*m_nxdim); amxy=1.0/sqrt(m_nxdim*m_nxdim+m_nydim*m_nydim); } int ZMRIMULTISegmentation::TanakaMain(NEWIMAGE::volume& pcsf, NEWIMAGE::volume& pgm, NEWIMAGE::volume& pwm) { for(int z=0;z0.0) { m_Mricopy[i].value(x, y, z)=log(m_Mricopy[i].value(x, y, z) + 1.0); } else { m_Mricopy[i].value(x, y, z)=0.0f; m_mask.value(x, y, z)=0; } } } } } Dimensions(); volumequant=new double[noclasses+1]; InitKernel(); m_Finalbias=new volume[numberofchannels+1]; m_resmean=new volume[numberofchannels+1]; m_meaninvcov=new volume[numberofchannels+1]; p_bias=new volume[numberofchannels+1]; for(int s=1;s<=numberofchannels;s++) { m_Finalbias[s]=volume(m_nWidth, m_nHeight, m_nDepth); m_Finalbias[s].copyproperties(m_Mri[s]); m_resmean[s]=volume(m_nWidth, m_nHeight, m_nDepth); m_resmean[s].copyproperties(m_Mri[s]); m_meaninvcov[s]=volume(m_nWidth, m_nHeight, m_nDepth); m_meaninvcov[s].copyproperties(m_Mri[s]); p_bias[s]=volume(m_nWidth, m_nHeight, m_nDepth); p_bias[s].copyproperties(m_Mri[s]); } long seed=-1; srand(seed); rhs=new float[100]; for(int i=1;i(m_Mricopy[i]); InitWeights(); if(bapusedflag==0) { try{Initialise();} catch (kmeansexception& km){cout << km.what() << endl ; return -1;} } else { InitSimple(pcsf, pgm, pwm); pcsf.destroy(); pgm.destroy(); pwm.destroy(); } if(Hyper<0.0) TanakaPriorHyper(); // first loop to remove bias field BiasRemoval(); for(int iter=0;iter=0.0) beta=Hyper; else TanakaHyper(); if(verboseusage) cout<<" BETA "<0) { PVMoreSophisticated(); Volumesquant(m_pve); pveClassification(); } return 0; } void ZMRIMULTISegmentation::Initialise() { WeightedKMeans(); UpdateMembers(m_post); MeansVariances(noclasses); m_prob=Initclass(noclasses); m_post=m_prob; UpdateMembers(m_post); Classification(); } void ZMRIMULTISegmentation::Classification(int x, int y, int z) { float max=-1e-10; m_Segment(x, y, z)=0; if(m_mask(x, y, z)==1) { for(int c=1;cmax) { max=weight[c-1]*m_post(x, y, z, c); m_Segment(x, y, z)=c; } } } } void ZMRIMULTISegmentation::Classification() { for(int z=0;z0)&&(classnumber0.0) m_prob(x, y, z, c)/=sum; } } } for(int iteration=0;iteration<5;iteration++) { m_post=m_prob; for(int z=0;z0.0f) m_prob.value(x, y, z, c)/=sum; else m_prob.value(x, y, z, c)=0.0f; } } } } } } m_post=m_prob; rhs[betahyp] = MRFWeightsTotal(); betahtemp+=0.05; } } void ZMRIMULTISegmentation::TanakaIterations() { for(int iteration=0;iteration<5;iteration++) { for(int z=0;z0.0f) m_post.value(x, y, z, c)/=sum; else m_post.value(x, y, z, c)=0.0f; } } } } } } if(verboseusage) { for(int channel=1;channel<=numberofchannels;channel++) { cout<<" CHANNEL "<3) mixnum=2*noclasses-1; float step=(float)(1.0f/(float)(iterationspve)); PVprob=volume4D(m_nWidth, m_nHeight, m_nDepth, mixnum); PVprob=0.0f; Matrix mu(numberofchannels, 1); Matrix sig(numberofchannels, numberofchannels); Matrix invsig(numberofchannels, numberofchannels); if(noclasses==3) { for(int z=0;z3) { for(int z=0;z3) { if(type0)) { Matrix submeanrow(1, numberofchannels); Matrix submeancol(numberofchannels, 1); for(int i=1;i<=numberofchannels;i++)submeancol(i, 1)=submeanrow(1, i)=m_Mri[i](x, y, z)-mu(i, 1); float sum=(submeanrow*sig*submeancol).AsScalar(); sum=0.5*sum+log(sqrt(abs(detsig*M_2PI(numberofchannels)))); return sum; } else return 0.0; } void ZMRIMULTISegmentation::ICMPV() { hardPV=volume(m_nWidth, m_nHeight, m_nDepth); hardPV.copyproperties(m_Segment); int mixnum=0; if(noclasses==3) mixnum=6; if(noclasses==2) mixnum=3; if(noclasses>3) mixnum=2*noclasses-1; float* clique=new float[mixnum]; for(int z=0;z3) { for(int type=0;type(noclasses-1))&&((hardPV(x+l, y+m, z+n)==(type-noclasses))||(hardPV(x+l, y+m, z+n)==(type-noclasses+1)))) { clique[type]+=am; continue; } clique[type]-=am; } } if(noclasses==2) { for(int type=0;type<3;type++) { if(type==hardPV(x+l, y+m, z+n)) { clique[type]+=am*2; continue; } if((type==0)&&((hardPV(x+l, y+m, z+n)==2))) { clique[type]+=am; continue; } if((type==1)&&((hardPV(x+l, y+m, z+n)==2))) { clique[type]+=am; continue; } if((type==2)&&((hardPV(x+l, y+m, z+n)==0)||(hardPV(x+l, y+m, z+n)==1))) { clique[type]+=am; continue; } clique[type]-=am; } } } } } } float max=-1; for(int type=0;type(m_nWidth, m_nHeight, m_nDepth, noclasses+1); m_pve.copyproperties(m_Mri[1]); m_pve=0.0f; float step=(float)(1.0f/(float)(iterationspve)); Matrix mu(numberofchannels, 1); Matrix sig(numberofchannels, numberofchannels); for(int z=0;z0) { float val=0.0; if(noclasses==3) { float min=1.0e13; if(hardPV(x, y, z)==0) { m_pve(x, y, z, 1)=1.0; m_pve(x, y, z, 2)=0.0; m_pve(x, y, z, 3)=0.0; continue; } if(hardPV(x, y, z)==1) { m_pve(x, y, z, 1)=0.0; m_pve(x, y, z, 2)=1.0; m_pve(x, y, z, 3)=0.0; continue;; } if(hardPV(x, y, z)==2) { m_pve(x, y, z, 1)=0.0; m_pve(x, y, z, 2)=0.0; m_pve(x, y, z, 3)=1.0; continue; } for(float delta=0.00;delta<=1.0;delta+=step) { if(hardPV(x, y, z)==3) { for(int j=1;j<=numberofchannels;j++) { mu(j, 1)=delta*m_mean(1, j)+(1-delta)*m_mean(2, j); for(int k=1;k<=numberofchannels;k++) { sig(j, k)=delta*delta*m_co_variance[1](j, k)+(1-delta)*(1-delta)*m_co_variance[2](j, k); } } } if(hardPV(x, y, z)==4) { for(int j=1;j<=numberofchannels;j++) { mu(j, 1)=delta*m_mean(1, j)+(1-delta)*m_mean(3, j); for(int k=1;k<=numberofchannels;k++) { sig(j, k)=delta*delta*m_co_variance[1](j, k)+(1-delta)*(1-delta)*m_co_variance[3](j, k); } } } if(hardPV(x, y, z)==5) { for(int j=1;j<=numberofchannels;j++) { mu(j, 1)=delta*m_mean(2, j)+(1-delta)*m_mean(3, j); for(int k=1;k<=numberofchannels;k++) { sig(j, k)=delta*delta*m_co_variance[2](j, k)+(1-delta)*(1-delta)*m_co_variance[3](j, k); } } } val=PVEnergy(x, y, z, mu, sig, sig.Determinant()); if(min>val) { if(hardPV(x, y, z)==3) { m_pve(x, y, z, 1)=delta; m_pve(x, y, z, 2)=1.0-delta; m_pve(x, y, z, 3)=0.0; } if(hardPV(x, y, z)==4) { m_pve(x, y, z, 1)=delta; m_pve(x, y, z, 2)=0.0; m_pve(x, y, z, 3)=1.0-delta; } if(hardPV(x, y, z)==5) { m_pve(x, y, z, 1)=0.0; m_pve(x, y, z, 2)=delta; m_pve(x, y, z, 3)=1.0-delta; } min=val; } } } if(noclasses>3) { float min=1.0e13; if(hardPV(x, y, z)=noclasses) { for(float delta=0.00;delta<=1.0;delta+=step) { for(int j=1;j<=numberofchannels;j++) { mu(j, 1)=delta*m_mean(hardPV(x, y, z)-noclasses+1, j)+(1-delta)*m_mean(hardPV(x, y, z)-noclasses+2, j); for(int k=1;k<=numberofchannels;k++) { sig(j, k)=delta*delta*m_co_variance[hardPV(x, y, z)-noclasses+1](j, k)+(1-delta)*(1-delta)*m_co_variance[hardPV(x, y, z)-noclasses+2](j, k); } } val=PVEnergy(x, y, z, mu, sig, sig.Determinant()); if(min>val) { for(int c=0;cval) { if(hardPV(x, y, z)==2) { m_pve(x, y, z, 0)=delta; m_pve(x, y, z, 1)=1.0f-delta; } min=val; } } } } } } } } float ZMRIMULTISegmentation::pvmeans(int clas) { return 0.0f; } float ZMRIMULTISegmentation::pvvar(int clas) { return 0.0f; } void ZMRIMULTISegmentation::takeexpo() { for(int chan=1;chan<=numberofchannels;chan++) { for(int z=0;z& pcsf, const NEWIMAGE::volume& pgm, const NEWIMAGE::volume& pwm) { if(verboseusage) cout<<"Beginning prior-based initialisation"<0.0) { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=pcsf.value(x, y, z)/norm2; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=pgm.value(x, y, z)/norm2; m_post.value(x, y, z, 3)=m_prob.value(x, y, z, 3)=talpriors.value(x, y, z, 3)=pwm.value(x, y, z)/norm2; } else { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=1.0/3.0f; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=1.0/3.0f; m_post.value(x, y, z, 3)=m_prob.value(x, y, z, 3)=talpriors.value(x, y, z, 3)=1.0/3.0f; } } else { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=0.0; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=0.0; m_post.value(x, y, z, 3)=m_prob.value(x, y, z, 3)=talpriors.value(x, y, z, 3)=0.0; } Classification(x, y, z); } } } } if(noclasses==2) { for(int z=0;z0.0) { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=pcsf.value(x, y, z)/norm2; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=(pgm.value(x, y, z)+pwm.value(x, y, z))/norm2; } else { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=1.0/2.0f; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=1.0/2.0f; } } else { m_post.value(x, y, z, 1)=m_prob.value(x, y, z, 1)=talpriors.value(x, y, z, 1)=0.0; m_post.value(x, y, z, 2)=m_prob.value(x, y, z, 2)=talpriors.value(x, y, z, 2)=0.0; } Classification(x, y, z); } } } } UpdateMembers(m_post); MeansVariances(noclasses); } void ZMRIMULTISegmentation::Volumesquant(const NEWIMAGE::volume4D& probs) { double tot=0.0; for(int c=1;c ZMRIMULTISegmentation::Convolve(NEWIMAGE::volume& resfieldimage) { return convolve_separable(resfieldimage, kernelx, kernely, kernelz); } void ZMRIMULTISegmentation::MeansVariances(int numberofclasses) { float* normtemp=new float[noclasses+1]; for(int c=1;c ZMRIMULTISegmentation::InitclassAlt(int numberofsegs) { volume4D probability; probability=volume4D(m_nWidth, m_nHeight, m_nDepth, noclasses+1); probability=0.0; if(numberofsegs==3) { for(int z=0;z0.0)&&(m_mask.value(x, y, z)==1)) { probability.value(x, y, z, c)/=tot; } else { probability.value(x, y, z, c)=0.0; } } } } } } } return probability; } NEWIMAGE::volume4D ZMRIMULTISegmentation::Initclass(int noclasses) { for(int z=0;z0) tot += m_prob(x, y, z, c) = exp(-1.0* logGaussian(c, x, y, z)); } for(int c=1;c0.0)&&(m_mask(x, y, z)==1)) m_prob(x, y, z, c)/=tot; else m_prob(x, y, z, c)=0.0; } Classification(x, y, z); } } } return m_prob; } void ZMRIMULTISegmentation::UpdateMembers(NEWIMAGE::volume4D& probability) { for(int x=0;x0) { float sum=0.0f; for(int c=0;c0.0) members(x, y, z, c)/=sum; else members(x, y, z, c)=0.0f; } } } } } } void ZMRIMULTISegmentation::UpdateWeights() { float sum=0.0f; for(int c=0;c0) { for(int c=0;c inputMeans; if (mansegfile!="") { ifstream inputfile(mansegfile.c_str()); copy(istream_iterator (inputfile),istream_iterator (),back_inserter(inputMeans)); inputfile.close(); } m_post=m_prob=0.0f; for(int z=0;z postcopy; postcopy=volume4D(m_post); volume4D probcopy; probcopy=volume4D(m_prob); sort(tempmean+1, tempmean+noclasses+1); for (c=1; c& m_posttemp) { } Matrix ZMRIMULTISegmentation:: covariancematrix(int classid, volume4D probability) { float tot=0.0f; Matrix cov(numberofchannels, numberofchannels);cov=0.0; if(classid