Class AlgorithmBrainExtractor

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.awt.event.WindowListener, java.lang.Runnable, java.util.EventListener

    public class AlgorithmBrainExtractor
    extends AlgorithmBase
    A class for segmenting the brain from a 3D MRI. The algorithm is partially based on the paper:
      BET: Brain Extraction Tool
     
    Stephen M. Smith
    FMRIB Technical Report TR00SMS2
    Oxford Centre for Functional Magnetic Resonance Imaging of the Brain

    See the document BrainExtraction.pdf for a detailed description of the algorithm as implemented in this class. A few modifications to the original algorithm were made.

    • Field Detail

      • SAT_COR

        public static final int SAT_COR
        Indicates that the image is in Sagittal or Coronal orientation.
        See Also:
        Constant Field Values
      • imageFactor

        protected float imageFactor
        Controls how much the image influences the surface evolution.
      • m_afCurvature

        protected float[] m_afCurvature
        Stores curvature information of the surface.
      • m_afLength

        protected float[] m_afLength
        Triangle edge length.
      • m_aiConnect

        protected int[] m_aiConnect
        Surface triangle connection.
      • m_aiImage

        protected int[] m_aiImage
        The 3D MRI image stored as a 1D array. The mapping from (x,y,z) to 1D is: index = x + xbound*(y + ybound*z).
      • m_aiMask

        protected byte[] m_aiMask
        Brain segmenation mask.
      • m_akSNormal

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akSNormal
        Surface normals.
      • m_akSTangent

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akSTangent
        Surface tangents.
      • m_akVertex

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVertex
        Surface vertex list.
      • m_akVMean

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVMean
        Average vector.
      • m_akVNormal

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVNormal
        Vertex normal.
      • m_fEParam

        protected float m_fEParam
        DOCUMENT ME!
      • m_fFParam

        protected float m_fFParam
        DOCUMENT ME!
      • m_fMeanEdgeLength

        protected float m_fMeanEdgeLength
        Mean length of the vector.
      • m_fRayDelta

        protected float m_fRayDelta
        DOCUMENT ME!
      • m_fReductionX

        protected float m_fReductionX
        Reduce the size of the 1st axis of the ellipsoid so that it fits entirely into brain.
      • m_fReductionY

        protected float m_fReductionY
        Reduce the size of the 2st axis of the ellipsoid so that it fits entirely into brain.
      • m_fReductionZ

        protected float m_fReductionZ
        Reduce the size of the 3st axis of the ellipsoid so that it fits entirely into brain.
      • m_fStiffness

        protected float m_fStiffness
        Stiffness of the surface.
      • m_fXDelta

        protected float m_fXDelta
        The size of a voxel, in voxel units.
      • m_fYDelta

        protected float m_fYDelta
        The size of a voxel, in voxel units.
      • m_fZDelta

        protected float m_fZDelta
        The size of a voxel, in voxel units.
      • m_iBackThreshold

        protected int m_iBackThreshold
        Backgroung threshold.
      • m_iBrightThreshold

        protected int m_iBrightThreshold
        High threshold.
      • m_iDMax

        protected int m_iDMax
        Dilation size used in generating boundary from the surface.
      • m_iMaxDepth

        protected int m_iMaxDepth
        The length of the vector _normal_ to the surface to sample along.
      • m_iMaxThreshold

        protected int m_iMaxThreshold
        Histogram maximum intensity parameter.
      • m_iMedianIntensity

        protected int m_iMedianIntensity
        Histogram median intensity parameter.
      • m_iMinThreshold

        protected int m_iMinThreshold
        Histogram minimum intensity parameter.
      • m_iTQuantity

        protected int m_iTQuantity
        The number of triangle in the ellipsoid.
      • m_iVQuantity

        protected int m_iVQuantity
        The number of vertices in the ellipsoid.
      • m_iXBound

        protected int m_iXBound
        The MRI image bounds and quantity of voxels.
      • m_iYBound

        protected int m_iYBound
        The MRI image bounds and quantity of voxels.
      • m_iZBound

        protected int m_iZBound
        The MRI image bounds and quantity of voxels.
      • m_iQuantity

        protected int m_iQuantity
        The MRI image bounds and quantity of voxels.
      • m_kCenter

        protected WildMagic.LibFoundation.Mathematics.Vector3f m_kCenter
        initial ellipsoid parameters.
      • m_kRotate

        protected WildMagic.LibFoundation.Mathematics.Matrix3f m_kRotate
        Used to rotate the initial mesh into the same orientation as the data.
      • nIterations

        protected int nIterations
        Number of surface evolutions.
      • orientationFlag

        protected int orientationFlag
        Indicated the orientation of the dataset. Default sagittal/cornal
      • aboveMedian

        private float aboveMedian
        factor above median at which edge values are taken to zero.
      • box

        private float[] box
        DOCUMENT ME!
      • direction

        private int[] direction
        DOCUMENT ME!
      • extractPaint

        private boolean extractPaint
        If true, do not phyically segment the image but generate a paint mask.
      • fUpdate1

        private float fUpdate1
        Factor used in controlling the input of the tangetial component of the surface evolution.
      • m_fBrainSelection

        private float m_fBrainSelection
        Factored used to adjust image influence - this is a fixed value.
      • nBrainVoxels

        private int nBrainVoxels
        The number of voxels that define the brain.
      • runOneIter

        private boolean runOneIter
        If true run one iteration so that one can observe initial surface location.
      • saveBrainMesh

        private boolean saveBrainMesh
        If true, the surface representing the brain (mesh) is saved.
      • secondStageErosion

        private boolean secondStageErosion
        Tells whether second stage which erodes edge values exceeding median by a factor >= aboveMedian occurs.
      • startLocation

        private float[] startLocation
        Origin of the image. Upper left hand corner of the first image.
      • useSphere

        private boolean useSphere
        If true initialize surface as a sphere.
      • initialMesh

        private WildMagic.LibGraphics.SceneGraph.TriMesh initialMesh
    • Constructor Detail

      • AlgorithmBrainExtractor

        public AlgorithmBrainExtractor​(ModelImage srcImg,
                                       int orientation,
                                       boolean runOneIteration,
                                       boolean estimateSphere,
                                       WildMagic.LibFoundation.Mathematics.Vector3f centerPoint)
        Create an extractor for segmenting the brain from a 3D magnetic resonance image.
        Parameters:
        srcImg - The source image. Should be MR image of the brain
        orientation - Image orienation
        runOneIteration - Run one iteration so that one can observe initial surface location.
        estimateSphere - Use sphere as the initial surface to be evolved.
        centerPoint - The center of the initial ellipsoid or sphere.
      • AlgorithmBrainExtractor

        public AlgorithmBrainExtractor​(ModelImage srcImg,
                                       int orientation,
                                       WildMagic.LibGraphics.SceneGraph.TriMesh initialMesh,
                                       int medianIntensity,
                                       WildMagic.LibFoundation.Mathematics.Vector3f centerPoint)
        Create an extractor for segmenting the brain from a 3D magnetic resonance image.
        Parameters:
        srcImg - The source image. Should be MR image of the brain
        orientation - Image orienation
        runOneIteration - Run one iteration so that one can observe initial surface location.
        estimateSphere - Use sphere as the initial surface to be evolved.
        centerPoint - The center of the initial ellipsoid or sphere.
    • Method Detail

      • extractBrain

        public void extractBrain()
        The segmentation function. Ideally, the only work an application needs to do is create an MjBrainExtractor object and call the method extractBrain(). Various parameters managed by MjBrainExtractor may be modified, if necessary, before the call.
      • finalize

        public void finalize()
        Prepares this class for destruction.
        Overrides:
        finalize in class AlgorithmBase
      • getBrainMask

        public final byte[] getBrainMask()
        Get the 3D image that represents the extracted brain. The image is ternary and has the same dimensions as the input MRI. A voxel value of 0 indicates background. A voxel value of 1 indicates brain surface. A voxel value of 2 indicates a voxel inside the brain surface.
        Returns:
        the image that represents the extracted brain
      • getBrainSelection

        public final float getBrainSelection()
        Get the current brain selection term, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 0.5.
        Returns:
        the current brain selection term
      • getBrainVolume

        public final float getBrainVolume()
        Get the 3D image that represents the extracted brain. The image is ternary and has the same dimensions as the input MRI. A voxel value of 0 indicates background. A voxel value of 1 indicates brain surface. A voxel value of 2 indicates a voxel inside the brain surface.
        Returns:
        the image that represents the extracted brain
      • getDilationSize

        public final int getDilationSize()
        Get the dilation size for dilating the voxelized brain surface obtained by rasterizing the triangle mesh. The rasterization is designed so that the voxel surface has no holes, thereby allowing it to be flood-filled. But just in case numerical round-off errors cause a few holes, this parameter is exposed for public use. The default value is 0 (no dilation). If the value is D > 0, the dilation mask is a cube of size (2*D+1)x(2*D+1)x(2*D+1).
        Returns:
        the current dilation size
      • getMaxDepth

        public final int getMaxDepth()
        Get the maximum depth, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 7.
        Returns:
        the current maximum depth
      • getRayDelta

        public final float getRayDelta()
        Get the spacing along the vertex normal rays, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 1.0.
        Returns:
        the current spacing
      • getReductionX

        public final float getReductionX()
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.6.
        Returns:
        the current reduction factor
      • getReductionY

        public final float getReductionY()
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.4.
        Returns:
        the current reduction factor
      • getReductionZ

        public final float getReductionZ()
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.6.
        Returns:
        the current reduction factor
      • getStiffness

        public final float getStiffness()
        Set the stiffness of the mesh, as described in BrainExtraction.pdf, that is part of the surface normal term in the surface evolution. The default value is 0.1.
        Returns:
        the current stiffness
      • histogramAnalysis

        public void histogramAnalysis()
        Analyze the histogram of the 10-bit binned 3D MRI. The function computes a minimum threshold, a maximum threshold, and a background threshold that are used in the image term of the surface evolution. A brightness threshold is also computed that is used for determining the initial ellipsoid that approximates the brain surface.
      • reIntialize

        public void reIntialize()
        Reinitialize when secondStageErosion has been performed so that the mask will match the eroded image.
      • setAboveMedian

        public final void setAboveMedian​(float aboveMedian)
        sets the aboveMedian ratio for second stage erosion Edge values >= median * aboveMedian are taken to zero.
        Parameters:
        aboveMedian - DOCUMENT ME!
      • setBrainSelection

        public final void setBrainSelection​(float fBrainSelection)
        Set the brain selection term, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 0.5.
        Parameters:
        fBrainSelection - the new brain selection term
      • setDilationSize

        public final void setDilationSize​(int iDMax)
        Set the dilation size for dilating the voxelized brain surface obtained by rasterizing the triangle mesh. The rasterization is designed so that the voxel surface has no holes, thereby allowing it to be flood-filled. But just in case numerical round-off errors cause a few holes, this parameter is exposed for public use. The default value is 0 (no dilation). If the value is D > 0, the dilation mask is a cube of size (2*D+1)x(2*D+1)x(2*D+1).
        Parameters:
        iDMax - the new dilation size
      • setExtractPaint

        public void setExtractPaint​(boolean extractPaint)
        Sets whether to extract the brain to a paint mask instead of removing non-brain image data.
        Parameters:
        extractPaint - whether to extract the brain to a paint mask
      • setImageRatio

        public final void setImageRatio​(float ratio)
        Sets the number of iterations.
        Parameters:
        ratio - DOCUMENT ME!
      • setIterations

        public final void setIterations​(int nIter)
        Sets the number of iterations.
        Parameters:
        nIter - DOCUMENT ME!
      • setJustIntialEllipsoid

        public final void setJustIntialEllipsoid​(boolean flag)
        Indicates if surface evolution should be skipped. This is helpful when determining if the initial ellipsoid is a good estimate.
        Parameters:
        flag - if true skip evolution of surface
      • setMaxDepth

        public final void setMaxDepth​(int iMaxDepth)
        Set the maximum depth, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 7.
        Parameters:
        iMaxDepth - the new maximum depth
      • setOrientation

        public final void setOrientation​(int orientation)
        Indicates the orientation of the image for use in the ellipsoid estimation.
        Parameters:
        orientation - the new image orientation
      • setRayDelta

        public final void setRayDelta​(float fRayDelta)
        Set the spacing along the vertex normal rays, as described in BrainExtraction.pdf, that is part of the image term in the surface evolution. The default value is 1.0.
        Parameters:
        fRayDelta - the new normal ray spacing
      • setReductionX

        public final void setReductionX​(float fReduction)
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.6.
        Parameters:
        fReduction - the amount to reduce the axis of the ellipsoid.
      • setReductionY

        public final void setReductionY​(float fReduction)
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.5.
        Parameters:
        fReduction - the amount to reduce the axis of the ellipsoid.
      • setReductionZ

        public final void setReductionZ​(float fReduction)
        Set the reduction factor for estimating the initial ellipsoid, as described in BrainExtraction.pdf. The default value is 0.6.
        Parameters:
        fReduction - the amount to reduce the axis of the ellipsoid.
      • setSaveBrainMesh

        public void setSaveBrainMesh​(boolean saveMesh)
        Sets whether to save a surface mesh of the extracted brain.
        Parameters:
        saveMesh - whether to save a brain surface mesh
      • setSecondStageErosion

        public final void setSecondStageErosion​(boolean secondStageErosion)
        Sets whether or not second stage erosion occurs taking edge values exceeding median by a factor >= aboveMedian to zero.
        Parameters:
        secondStageErosion - DOCUMENT ME!
      • setStiffness

        public final void setStiffness​(float fStiffness)
        Set the stiffness of the mesh, as described in BrainExtraction.pdf, that is part of the surface normal term in the surface evolution. The default value is 0.1.
        Parameters:
        fStiffness - the new stiffness
      • setUseSphere

        public final void setUseSphere​(boolean flag)
        Don't try estimating ellipse as initial surface use sphere estimate.
        Parameters:
        flag - if true estimate initial surface with sphere and not ellipse.
      • updateMesh

        public void updateMesh()
        The heart of the segmentation. This function is responsible for the evolution of the triangle mesh that approximates the brain surface. The update has a tangential component, a surface normal component, and a vertex normal component for each vertex in the mesh. The first two components control the geometry of the mesh. The last component is based on the MRI data itself. See BrainExtraction.pdf for a detailed description of the update terms.
      • computeMeanEdgeLength

        protected void computeMeanEdgeLength()
        Compute the average length of all the edges in the triangle mesh.
      • computeMedianIntensityInitial

        protected void computeMedianIntensityInitial()
        Compute the median intensity of those voxels inside the initial ellipsoid. This intensity is used in the image term of the surface evolution.
      • computeMedianIntensity

        protected void computeMedianIntensity()
        Compute the median intensity of those voxels inside the initial ellipsoid. This intensity is used in the image term of the surface evolution.
      • computeVertexInformation

        protected void computeVertexInformation()
        Let V[i] be a vertex in the triangle mesh. This function computes VMean[i], the average of the immediate neighbors of V[i]. Define S[i] = VMean[i] - V[i]. The function also computes a surface normal SNormal[i], the component of S[i] in the vertex normal direction. STangent[i] = S[i] - SNormal[i] is computed as an approximation to a tangent to the surface. Finally, Curvature[i] is an approximation of the surface curvature at V[i].
      • computeVertexNormals

        protected void computeVertexNormals()
        Compute the vertex normals of the triangle mesh. Each vertex normal is the unitized average of the non-unit triangle normals for those triangles sharing the vertex.
      • computeNewCenter

        protected WildMagic.LibFoundation.Mathematics.Vector3f computeNewCenter()
      • estimateEllipsoid

        protected boolean estimateEllipsoid()
        Approximate the brain surface by an ellipsoid. The approximation is based on locating all voxels of intensity larger than a brightness threshold and that are part of the upper-half of the head. The idea is that the scalp voxels in the upper-half form lie approximately on an ellipsoidal surface.

        NOTE. The assumption is that the traversal from bottom to top of head is in the y-direction of the 3D image. It does not matter if the top of the head has y-values smaller/larger than those for the bottom of the head. If this assumption is not met, the image should be permuted OR this code must be modified to attempt to recognize the orientation of the head

        Returns:
        DOCUMENT ME!
      • estimateSphere

        protected void estimateSphere()
        Approximate the intial brain surface by an sphere. Find the center of mass and approimate radius
      • floodFill

        protected void floodFill​(int iX,
                                 int iY,
                                 int iZ)
        Identify voxels enclosed by the brain surface by using a flood fill. The flood fill is nonrecursive to avoid overflowing the program stack.
        Parameters:
        iX - the x-value of the seed point for the fill
        iY - the y-value of the seed point for the fill
        iZ - the z-value of the seed point for the fill
      • generateEllipsoidMesh

        protected void generateEllipsoidMesh​(int iSubdivisions)
        Tessellate a unit sphere centered at the origin. Start with an octahedron and subdivide. The final mesh is then affinely mapped to the initial ellipsoid produced by estimateEllipsoid(). The subdivision scheme is described in BrainExtraction.pdf.
        Parameters:
        iSubdivisions - the number of levels to subdivide the ellipsoid
      • getIndex

        protected final int getIndex​(int iX,
                                     int iY,
                                     int iZ)
        A convenience function for mapping the 3D voxel position (iX,iY,iZ) to a 1D array index. The images are stored as 1D arrays, so this function is used frequently.
        Parameters:
        iX - the x-value of the voxel position
        iY - the y-value of the voxel position
        iZ - the z-value of the voxel position
        Returns:
        the 1D array index corresponding to (iX,iY,iZ)
      • getInsideVoxels

        protected void getInsideVoxels​(boolean doErode)
        Identify all voxels that are inside or on the mesh that represents the brain surface. The surface voxels are constructed by rasterizing the triangles of the mesh in 3D. The centroid of these voxels is used as a seed point for a flood fill of the region enclosed by the surface.
        Parameters:
        doErode - DOCUMENT ME!
      • getIntersectX

        protected int getIntersectX​(WildMagic.LibFoundation.Mathematics.Vector3f kV0,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV1,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV2,
                                    int iY,
                                    int iZ)
        Compute the point of intersection between a line (0,iY,iZ)+t(1,0,0) and the triangle defined by the three input points. All calculations are in voxel coordinates and the x-value of the intersection point is truncated to an integer.
        Parameters:
        kV0 - a 3D vertex of the triangle
        kV1 - a 3D vertex of the triangle
        kV2 - a 3D vertex of the triangle
        iY - the y-value of the origin of the line
        iZ - the z-value of the origin of the line
        Returns:
        the x-value of the intersection
      • getIntersectY

        protected int getIntersectY​(WildMagic.LibFoundation.Mathematics.Vector3f kV0,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV1,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV2,
                                    int iX,
                                    int iZ)
        Compute the point of intersection between a line (iX,0,iZ)+t(0,1,0) and the triangle defined by the three input points. All calculations are in voxel coordinates and the y-value of the intersection point is truncated to an integer.
        Parameters:
        kV0 - a 3D vertex of the triangle
        kV1 - a 3D vertex of the triangle
        kV2 - a 3D vertex of the triangle
        iX - the x-value of the origin of the line
        iZ - the z-value of the origin of the line
        Returns:
        the y-value of the intersection
      • getIntersectZ

        protected int getIntersectZ​(WildMagic.LibFoundation.Mathematics.Vector3f kV0,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV1,
                                    WildMagic.LibFoundation.Mathematics.Vector3f kV2,
                                    int iX,
                                    int iY)
        Compute the point of intersection between a line (iX,iY,0)+t(0,0,1) and the triangle defined by the three input points. All calculations are in voxel coordinates and the z-value of the intersection point is truncated to an integer.
        Parameters:
        kV0 - a 3D vertex of the triangle
        kV1 - a 3D vertex of the triangle
        kV2 - a 3D vertex of the triangle
        iX - the x-value of the origin of the line
        iY - the y-value of the origin of the line
        Returns:
        the z-value of the intersection
      • saveMesh

        protected void saveMesh​(boolean flip)
                         throws java.io.IOException
        Internal support to write vertices, normals, and connectivity indices to the file.
        Parameters:
        flip - if the y axis should be flipped - true for extract, false for from another surface
        Throws:
        java.io.IOException - if there is an error writing to the file
      • update2

        protected float update2​(int i)
        Compute the coefficient of the surface normal for the update of the mesh vertex V[i] in the SNormal[i] direction. See BrainExtraction.pdf for a description of the update.
        Parameters:
        i - the index of the vertex to update
        Returns:
        the coefficient of SNormal[i] for the update
      • update3

        protected float update3​(int i)
        Compute the coefficient of the vertex normal for the update of the mesh vertex V[i] in the VNormal[i] direction. See BrainExtraction.pdf for a description of the update.
        Parameters:
        i - the index of the vertex to update
        Returns:
        the coefficient of VNormal[i] for the update
      • updateCenter

        protected boolean updateCenter​(WildMagic.LibFoundation.Mathematics.Vector3f Center)