Class AlgorithmObjectExtractor

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

    public class AlgorithmObjectExtractor
    extends AlgorithmBase
    implements AlgorithmInterface
    A class for segmenting objects. 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

      • c1Factor

        protected float c1Factor
        DOCUMENT ME!
      • c3Factor

        protected float c3Factor
        DOCUMENT ME!
      • m_afCurvature

        protected float[] m_afCurvature
        DOCUMENT ME!
      • m_afImage

        protected float[] m_afImage
        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_afLength

        protected float[] m_afLength
        DOCUMENT ME!
      • m_aiConnect

        protected int[] m_aiConnect
        DOCUMENT ME!
      • m_aiMask

        protected byte[] m_aiMask
        object mask creation.
      • m_akSNormal

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akSNormal
        DOCUMENT ME!
      • m_akSTangent

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akSTangent
        DOCUMENT ME!
      • m_akVertex

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVertex
        DOCUMENT ME!
      • m_akVMean

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVMean
        DOCUMENT ME!
      • m_akVNormal

        protected WildMagic.LibFoundation.Mathematics.Vector3f[] m_akVNormal
        DOCUMENT ME!
      • m_fEParam

        protected float m_fEParam
        DOCUMENT ME!
      • m_fFParam

        protected float m_fFParam
        DOCUMENT ME!
      • m_fMeanEdgeLength

        protected float m_fMeanEdgeLength
        update parameters.
      • m_fRayDelta

        protected float m_fRayDelta
        DOCUMENT ME!
      • m_fReductionX

        protected float m_fReductionX
        DOCUMENT ME!
      • m_fReductionY

        protected float m_fReductionY
        DOCUMENT ME!
      • m_fReductionZ

        protected float m_fReductionZ
        DOCUMENT ME!
      • m_fStiffness

        protected float m_fStiffness
        DOCUMENT ME!
      • 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_iDMax

        protected int m_iDMax
        DOCUMENT ME!
      • m_iEQuantity

        protected int m_iEQuantity
        DOCUMENT ME!
      • m_iMedianIntensity

        protected int m_iMedianIntensity
        DOCUMENT ME!
      • m_iTQuantity

        protected int m_iTQuantity
        DOCUMENT ME!
      • m_iVQuantity

        protected int m_iVQuantity
        mesh data.
      • 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
        DOCUMENT ME!
      • triMesh

        protected WildMagic.LibGraphics.SceneGraph.TriMesh triMesh
        DOCUMENT ME!
      • box

        private float[] box
        DOCUMENT ME!
      • direction

        private int[] direction
        DOCUMENT ME!
      • gvfBuffer

        private float[] gvfBuffer
        DOCUMENT ME!
      • iSubdivisions

        private int iSubdivisions
        DOCUMENT ME!
      • oldUVal

        private float oldUVal
        DOCUMENT ME!
      • oldVVal

        private float oldVVal
        DOCUMENT ME!
      • oldWVal

        private float oldWVal
        DOCUMENT ME!
      • onlyInit

        private boolean onlyInit
        DOCUMENT ME!
      • saveGVF

        private boolean saveGVF
        DOCUMENT ME!
      • startLocation

        private float[] startLocation
        DOCUMENT ME!
      • uVal

        private float[] uVal
        DOCUMENT ME!
      • voi

        private VOI voi
        DOCUMENT ME!
      • vVal

        private float[] vVal
        DOCUMENT ME!
      • wVal

        private float[] wVal
        DOCUMENT ME!
      • outputBuffer

        private float[] outputBuffer
    • Constructor Detail

      • AlgorithmObjectExtractor

        public AlgorithmObjectExtractor​(ModelImage srcImg,
                                        VOI voi,
                                        boolean justInit,
                                        boolean saveGVF,
                                        WildMagic.LibGraphics.SceneGraph.TriMesh triMesh,
                                        float[] uVal,
                                        float[] vVal,
                                        float[] wVal)
        Create an extractor for segmenting an object from an image.
        Parameters:
        srcImg - the source image.
        voi - initial voi used to estimate the inital ellipsoid
        justInit - if true just perform one iteration
        saveGVF - if true save uvf, vvf, and wvf files
        triMesh - if not null use mesh instead of voi
        uVal - x component of GVF field
        vVal - y component of GVF field
        wVal - z component of GVF field
    • Method Detail

      • extractObject

        public void extractObject()
        The segmentation function. Various parameters may be modified, if necessary, before the call.
      • finalize

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

        public final int getDilationSize()
        Get the dilation size for dilating the voxelized object 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
      • getObjectMask

        public final byte[] getObjectMask()
        Get the 3D image that represents the extracted object. 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 object surface. A voxel value of 2 indicates a voxel inside the object surface.
        Returns:
        the image that represents the extracted object
      • 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.75.
        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.75.
        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.75.
        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
      • getTriLinear

        public final float getTriLinear​(float x,
                                        float y,
                                        float z,
                                        float[] buffer)
        DOCUMENT ME!
        Parameters:
        x - DOCUMENT ME!
        y - DOCUMENT ME!
        z - DOCUMENT ME!
        buffer - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • setDilationSize

        public final void setDilationSize​(int iDMax)
        Set the dilation size for dilating the voxelized object 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
      • setJustIntial

        public final void setJustIntial​(boolean flag)
        Indicates if surface evolution should be skipped. This is helpful when determining if the initial ellipsoid is a good estimate.
        Parameters:
        flag - whether the surface evolution should be skipped
      • 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 spacing along the vertex normal rays
      • 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.
      • 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
      • updateMesh

        public void updateMesh()
        The heart of the segmentation. This function is responsible for the evolution of the triangle mesh that approximates the object 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.
      • 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.
      • estimateEllipsoid

        protected void 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

      • 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()
        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.
      • 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()
        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.
      • 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 inverted - 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 void 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
      • calcGradMag

        private void calcGradMag()
        DOCUMENT ME!
      • calcGVF3D

        private void calcGVF3D()
        Calculate GVF from 3D image buffer.
      • algorithmPerformed

        public void algorithmPerformed​(AlgorithmBase algorithm)
        Description copied from interface: AlgorithmInterface
        Called after an algorithm this listener is registered to exits (maybe successfully, maybe not). If the algorithm is run in a separate thread, this call will be made within that thread. If not, this call will be made from that same, shared thread.
        Specified by:
        algorithmPerformed in interface AlgorithmInterface
        Parameters:
        algorithm - the algorithm which has just completed