Class CAAMUtil
- java.lang.Object
-
- gov.nih.mipav.view.renderer.WildMagic.AAM.CAAMObject
-
- gov.nih.mipav.view.renderer.WildMagic.AAM.CAAMUtil
-
public class CAAMUtil extends CAAMObject
This is the Java modified version of C++ active appearance model API (AAM_API). It is modified with a subset of required functions for automatic MRI prostate segmentation. AAM-API LICENSE - file: license.txt This software is freely available for non-commercial use such as research and education. Please see the full disclaimer below. All publications describing work using this software should cite the reference given below. Copyright (c) 2000-2003 Mikkel B. Stegmann, mbs@imm.dtu.dk IMM, Informatics & Mathematical Modelling DTU, Technical University of Denmark Richard Petersens Plads, Building 321 DK-2800 Lyngby, Denmark http://www.imm.dtu.dk/~aam/ REFERENCES Please use the reference below, when writing articles, reports etc. where the AAM-API has been used. A draft version the article is available from the homepage. I will be happy to receive pre- or reprints of such articles. /Mikkel ------------- M. B. Stegmann, B. K. Ersboll, R. Larsen, "FAME -- A Flexible Appearance Modelling Environment", IEEE Transactions on Medical Imaging, IEEE, 2003 (to appear) ------------- 3RD PART SOFTWARE The software is partly based on the following libraries: - The Microsoft(tm) Vision Software Developers Kit, VisSDK - LAPACK DISCLAIMER This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any non-commercial purpose, and to alter it, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. -- No guarantees of performance accompany this software, nor is any responsibility assumed on the part of the author or IMM. This software is provided by Mikkel B. Stegmann and IMM ``as is'' and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall IMM or Mikkel B. Stegmann be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. $Revision: 1.4 $ $Date: 2003/04/23 14:49:15 $ Utility methods for the AAM project. This class consists of methods that are self-containing and could not logically fit into any other AAM class. Thus all methods are static, so remember that there is never need for an instantiation of this class.- Author:
- Ruida Cheng
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.List<java.lang.String>
vFilenames
File name vector
-
Constructor Summary
Constructors Constructor Description CAAMUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
AddBackSlash(java.lang.String path)
Ensures that a string is terminated with a backslash.static void
ASF2PTS(java.lang.String path)
Converts AAM-API shape files (.asf) to the ISBE .pts format.static void
CalcShapeDistances(CAAMShape optimized, CAAMShape groundTruth, double[] ptpt, double[] ptcrv, CDVector pvDists)
Calculates optimization results.static boolean
ContainsPoint(int x, int y, ModelSimpleImage image)
Check the point is inside the image or notstatic boolean
CreateTest(java.lang.String file)
Tests if a file can be created for writing.static double
Deg2Rad(double deg)
Converts from degrees to radians.static double
DistEuclidianAssBorder(CAAMShape s1, CAAMShape s2)
Wrapper to calculate the point to curve error.static double
DistEuclidianAssBorder(CAAMShape s1, CAAMShape s2, CDVector pvDist)
Calculates the point to curve error between two shapes.static double
DistEuclidianAssBorder(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2, CDVector pvDist)
Calculates the point to curve error between two shapes.static double
DistEuclidianPoints(CAAMShape s1, CAAMShape s2)
Calculates the point to point error between two shapes.static double
DistEuclidianPoints(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2)
Calculates the point to point error between two shapes.static void
DrawLine(int[] x1, int[] y1, int[] x2, int[] y2, double[] tVal, boolean fInsideCheck, ModelSimpleImage image)
Draw linesstatic void
ExpandImg2DyadicSize(ModelSimpleImage img, ModelSimpleImage out)
Expands an image to have dyadic size.static boolean
FileExists(java.lang.String filename)
Tests if a file exists.static java.lang.String
FindVacantFilename(java.lang.String filename_suggestion)
Finds a file name that is not 'occupied'.static java.lang.String
ForceExt(java.lang.String filename, java.lang.String ext)
Forces an extension onto a file name.static java.lang.String
GetExt(java.lang.String s)
Returns the extension of a file name.static java.lang.String
GetFilename(java.lang.String filename)
Returns the file name of a path+file name string.static java.lang.String
GetPath(java.lang.String fullfilename)
Returns the path of a filename.static void
MirrorEdge(ModelSimpleImage img, ModelSimpleImage mask, int edgeWidth)
Mirrors the edge of an arbitrary shape.static void
MirrorEdge(CDMatrix m, ModelSimpleImage mask, int edgeWidth)
Mirrors the edge of an arbitrary shape.static void
PlotMeshIntoImage(ModelSimpleImage image, CAAMMesh mesh, double[] point_color, double[] line_color)
Plots a mesh into an image.static void
PlotShapeIntoImage(ModelSimpleImage image, CAAMShape shape, double[] point_color, double[] line_color, boolean drawNormals, boolean drawArea, boolean drawPoints)
Wrapper to draw shape onto imagestatic void
PlotShapeIntoImage(ModelSimpleImage image, CAAMShape shape, double[] point_color, double[] line_color, boolean drawNormals, boolean drawArea, boolean drawPoints, boolean drawLines)
Draws the shape into an image with a given color.static void
PointOnAssBorder(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2, CDVector vXBorder, CDVector vYBorder, CDVector vDist)
Calculates the point to curve error between two shapes.private static void
processDir(java.lang.String pathBS, java.lang.String ext)
Process directory.static double
ProjPointOnLine(double dXL1, double dYL1, double dXL2, double dYL2, double dXP, double dYP, double[] dXProj, double[] dYProj)
Find the projection dProj of the point dP on the line through dL1 and dL2.static double
Rad2Deg(double rad)
Convert from radians to degrees.static ModelSimpleImage
ReadExample(ModelImage targetImageSlice, ModelSimpleImage img, CAAMShape shape, int rfactor)
Read example from target image, scale down if necessary.static ModelSimpleImage
ReadExample(java.lang.String filename, ModelSimpleImage img, CAAMShape shape)
Wrapper to read image and VOIsstatic ModelSimpleImage
ReadExample(java.lang.String filename, ModelSimpleImage img, CAAMShape shape, int rfactor)
static ModelSimpleImage
ReadExample_init(ModelImage targetImageSlice, ModelSimpleImage img, CAAMShape shape, int rfactor)
Read example from target image, scale down if necessary.static java.util.Vector<java.lang.String>
ReadLines(java.lang.String filename)
Reads a file into an array of lines.static void
RegistrationMovie(java.lang.String filename, java.lang.String asfPath, boolean useConvexHull, boolean writeRefShape)
Writes a movie file containing all shapes from a directory warped to their mean shape.static void
RegistrationMovie(java.lang.String filename, java.util.Vector<CDVector> vTexture, CAAMReferenceFrame rf)
Writes a movie file containing a set of textures warped to their mean shape.static java.lang.String
RemoveExt(java.lang.String s)
Removes the extension of a file name.static void
SampleTextures(CAAMShapeCollection unalignedShapes, java.util.Vector<CDVector> vTextures, CAAMReferenceFrame outputRF, int imageReduction, boolean removeMean, boolean useTSP, boolean useConvexHull)
Samples a set of texture vectors given a set of shape in absolute (i.e. image) coordinates.static java.util.List<java.lang.String>
ScanSortDir(java.lang.String path, java.lang.String extension)
Scans and sorts a directory for files with a specific extension.static java.lang.String
Secs2Mins(double secs)
Converts seconds to a MM:SS string.static boolean
ShapeInsideImage(CAAMShape s, ModelSimpleImage img)
Tests if a shape is fully inside an image.static double
ShapeOverlap(CAAMShape model, CAAMShape gt)
Calculates the overlap between two shapes as specified in "Active Shape Model Segmentation With Optimal Features" Bram van Ginneken et al., IEEE TMI 21(8) Aug. 2002.static void
Swap(int[] a, int[] b)
Swap valuesstatic double
SymmetricPtCrv(CAAMShape s1, CAAMShape s2)
A symmetric poin- to-curve measure, i.e.static void
VecCat3(CDVector dest, CDVector v1, CDVector v2, CDVector v3)
Concatenates three vectors.-
Methods inherited from class gov.nih.mipav.view.renderer.WildMagic.AAM.CAAMObject
FromFile, ToFile
-
-
-
-
Method Detail
-
PlotMeshIntoImage
public static void PlotMeshIntoImage(ModelSimpleImage image, CAAMMesh mesh, double[] point_color, double[] line_color)
Plots a mesh into an image.- Parameters:
image
- The image in which the shape should be plotted.shape
- The shape to be plotted.point_color
- The point color of the mesh.line_color
- The line color of the mesh.
-
PlotShapeIntoImage
public static void PlotShapeIntoImage(ModelSimpleImage image, CAAMShape shape, double[] point_color, double[] line_color, boolean drawNormals, boolean drawArea, boolean drawPoints)
Wrapper to draw shape onto image- Parameters:
image
- The image in which the shape should be plotted.shape
- The shape to be plotted.point_color
- The point color of the shape.line_color
- The line color of the shape.drawNormals
- If true the point normals are drawn.drawArea
- If true the inside area of the shape is drawn.drawPoints
- If true the points of the shape is drawn.
-
PlotShapeIntoImage
public static void PlotShapeIntoImage(ModelSimpleImage image, CAAMShape shape, double[] point_color, double[] line_color, boolean drawNormals, boolean drawArea, boolean drawPoints, boolean drawLines)
Draws the shape into an image with a given color.- Parameters:
image
- The image in which the shape should be plotted.shape
- The shape to be plotted.point_color
- The point color of the shape.line_color
- The line color of the shape.drawNormals
- If true the point normals are drawn.drawArea
- If true the inside area of the shape is drawn.drawPoints
- If true the points of the shape is drawn.drawLines
- If true the lines of the shape is drawn.
-
ContainsPoint
public static boolean ContainsPoint(int x, int y, ModelSimpleImage image)
Check the point is inside the image or not- Parameters:
x
- point x coordinatey
- point y coordinateimage
- model image reference.- Returns:
- true inside, false outside.
-
Swap
public static void Swap(int[] a, int[] b)
Swap values- Parameters:
a
-b
-
-
DrawLine
public static void DrawLine(int[] x1, int[] y1, int[] x2, int[] y2, double[] tVal, boolean fInsideCheck, ModelSimpleImage image)
Draw lines- Parameters:
x1
-y1
-x2
-y2
-tVal
-fInsideCheck
-image
-
-
VecCat3
public static void VecCat3(CDVector dest, CDVector v1, CDVector v2, CDVector v3)
Concatenates three vectors.- Parameters:
dest
- Output vector.v1
- First input vector.v2
- Second input vector.v3
- Third input vector.
-
ScanSortDir
public static java.util.List<java.lang.String> ScanSortDir(java.lang.String path, java.lang.String extension)
Scans and sorts a directory for files with a specific extension.- Parameters:
path
- Path to read from.extension
- The file extension to search for. ex. "hips".- Returns:
- The filenames found without any path.
-
processDir
private static void processDir(java.lang.String pathBS, java.lang.String ext)
Process directory.- Parameters:
pathBS
- file pathext
- file extension
-
ReadExample
public static ModelSimpleImage ReadExample(java.lang.String filename, ModelSimpleImage img, CAAMShape shape)
Wrapper to read image and VOIs- Parameters:
filename
- file nameimg
- imageshape
- voi- Returns:
-
ReadExample
public static ModelSimpleImage ReadExample(java.lang.String filename, ModelSimpleImage img, CAAMShape shape, int rfactor)
- Parameters:
filename
- The filename of an annotation. Ex. "horse.asf".img
- Output image.shape
- Output shape.rfactor
- The reduction factor. Performs a scaling of the input of 1/rfactor.- Returns:
- img image
-
ReadExample
public static ModelSimpleImage ReadExample(ModelImage targetImageSlice, ModelSimpleImage img, CAAMShape shape, int rfactor)
Read example from target image, scale down if necessary.- Parameters:
targetImageSlice
- target image sliceimg
- sample imageshape
- voirfactor
- subsample factor- Returns:
- image
-
ReadExample_init
public static ModelSimpleImage ReadExample_init(ModelImage targetImageSlice, ModelSimpleImage img, CAAMShape shape, int rfactor)
Read example from target image, scale down if necessary.- Parameters:
targetImageSlice
- target image sliceimg
- sample imageshape
- voirfactor
- subsample factor- Returns:
- image
-
DistEuclidianPoints
public static double DistEuclidianPoints(CAAMShape s1, CAAMShape s2)
Calculates the point to point error between two shapes.- Parameters:
s1
- Shape 1.s2
- Shape 2.- Returns:
- The average point to point error.
-
DistEuclidianAssBorder
public static double DistEuclidianAssBorder(CAAMShape s1, CAAMShape s2)
Wrapper to calculate the point to curve error.- Parameters:
s1
- shape 1s2
- shape 2- Returns:
- the average point to curve error.
-
DistEuclidianAssBorder
public static double DistEuclidianAssBorder(CAAMShape s1, CAAMShape s2, CDVector pvDist)
Calculates the point to curve error between two shapes.- Parameters:
s1
- Shape 1.s2
- Shape 2.pvDist
- Optional pointer to an output vector containing all point to curve distances (one for each landmark).- Returns:
- The average point to curve error.
-
DistEuclidianPoints
public static double DistEuclidianPoints(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2)
Calculates the point to point error between two shapes.- Parameters:
vX1
- X-positions of shape 1.vY1
- Y-positions of shape 1.vX2
- X-positions of shape 2.vY2
- Y-positions of shape 2.- Returns:
- The average point to point error.
-
DistEuclidianAssBorder
public static double DistEuclidianAssBorder(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2, CDVector pvDist)
Calculates the point to curve error between two shapes.- Parameters:
vX1
- X-positions of shape 1.vY1
- Y-positions of shape 1.vX2
- X-positions of shape 2.vY2
- Y-positions of shape 2.pvDist
- Optional pointer to an output vector containing all point to curve distances (one for each landmark).- Returns:
- The average point to curve error.
-
PointOnAssBorder
public static void PointOnAssBorder(CDVector vX1, CDVector vY1, CDVector vX2, CDVector vY2, CDVector vXBorder, CDVector vYBorder, CDVector vDist)
Calculates the point to curve error between two shapes.- Parameters:
vX1
- X-positions of shape 1.vY1
- Y-positions of shape 1.vX2
- X-positions of shape 2.vY2
- Y-positions of shape 2.vXBorder
- Output x border.vYBorder
- Output y border.vDist
- Point to curve distance for each landmark.
-
ProjPointOnLine
public static double ProjPointOnLine(double dXL1, double dYL1, double dXL2, double dYL2, double dXP, double dYP, double[] dXProj, double[] dYProj)
Find the projection dProj of the point dP on the line through dL1 and dL2. Returns the distance between dProj and dP, if the dProj lays between dL1 and dL2 otherwise DBL_MAX. dXL1,dYL1,dXL2,dYL2: points on line dXP,dYP: points to project dXProj,dYProj: projected point on the line- Returns:
- The distance.
-
RemoveExt
public static java.lang.String RemoveExt(java.lang.String s)
Removes the extension of a file name.- Parameters:
s
- Input file name.- Returns:
- File name without extension.
-
GetExt
public static java.lang.String GetExt(java.lang.String s)
Returns the extension of a file name.- Parameters:
s
- Input filename- Returns:
- The extension.
-
CreateTest
public static boolean CreateTest(java.lang.String file)
Tests if a file can be created for writing.- Parameters:
file
- Input file name.- Returns:
- True, if the file could be created (and deleted again).
-
AddBackSlash
public static java.lang.String AddBackSlash(java.lang.String path)
Ensures that a string is terminated with a backslash. If the already has a terminating backslash, nothing is done.- Parameters:
path
- Input string.- Returns:
- Backslash-terminated output string.
-
GetPath
public static java.lang.String GetPath(java.lang.String fullfilename)
Returns the path of a filename.- Parameters:
fullfilename
- Filename including any path.- Returns:
- The path to the filename.
-
GetFilename
public static java.lang.String GetFilename(java.lang.String filename)
Returns the file name of a path+file name string.- Parameters:
filename
- Full qualified filename including path.- Returns:
- The file name including any extension, but without any path.
-
ForceExt
public static java.lang.String ForceExt(java.lang.String filename, java.lang.String ext)
Forces an extension onto a file name. The method tests the extension of a filename. If the wanted extension is presented nothing is done. If not, the extension is appended.- Parameters:
filename
- Input file name.ext
- Wanted extension.- Returns:
- The file name including the wanted extension.
-
FileExists
public static boolean FileExists(java.lang.String filename)
Tests if a file exists.- Parameters:
filename
- File name to test.- Returns:
- True if the file exists.
-
ExpandImg2DyadicSize
public static void ExpandImg2DyadicSize(ModelSimpleImage img, ModelSimpleImage out)
Expands an image to have dyadic size. The method expands an image by using zero-padding so that the resulting image has width and height that are powers of two.- Parameters:
img
- Input image.out
- Output dyadic image.
-
MirrorEdge
public static void MirrorEdge(CDMatrix m, ModelSimpleImage mask, int edgeWidth)
Mirrors the edge of an arbitrary shape. Mirrors the edge of an arbitrary shape mask in a matrix using poor-mans mirroring.- Parameters:
img
- Input matrix. Overwritten by mirrored version.mask
- Image mask defining the shape.edgeWidth
- The width of the edge.
-
MirrorEdge
public static void MirrorEdge(ModelSimpleImage img, ModelSimpleImage mask, int edgeWidth)
Mirrors the edge of an arbitrary shape. Mirrors the edge of an arbitrary shape mask in an image using poor-mans mirroring.- Parameters:
img
- Input image. Overwritten by mirrored version.mask
- Image mask defining the shape.edgeWidth
- The width of the edge.
-
ShapeInsideImage
public static boolean ShapeInsideImage(CAAMShape s, ModelSimpleImage img)
Tests if a shape is fully inside an image.- Parameters:
s
- Input shape.img
- Input image.- Returns:
- True if the shape is fully inside the image.
-
Deg2Rad
public static double Deg2Rad(double deg)
Converts from degrees to radians.- Parameters:
deg
- degree- Returns:
- radian
-
Rad2Deg
public static double Rad2Deg(double rad)
Convert from radians to degrees.- Parameters:
rad
- radian- Returns:
- degree
-
CalcShapeDistances
public static void CalcShapeDistances(CAAMShape optimized, CAAMShape groundTruth, double[] ptpt, double[] ptcrv, CDVector pvDists)
Calculates optimization results.- Parameters:
optimized
- Model shape.groundTruth
- Ground truth shape.ptpt
- Average point to point landmark error.ptcrv
- Average point to curve landmark error. NOTICE: This is not a symmetric measure!
-
Secs2Mins
public static java.lang.String Secs2Mins(double secs)
Converts seconds to a MM:SS string.- Parameters:
secs
- Time in seconds.- Returns:
- The time in MM:SS.
-
FindVacantFilename
public static java.lang.String FindVacantFilename(java.lang.String filename_suggestion)
Finds a file name that is not 'occupied'.- Parameters:
filename_suggestion
- Suggestion including extension.- Returns:
- An unused filename resembling the suggestion.
-
SampleTextures
public static void SampleTextures(CAAMShapeCollection unalignedShapes, java.util.Vector<CDVector> vTextures, CAAMReferenceFrame outputRF, int imageReduction, boolean removeMean, boolean useTSP, boolean useConvexHull)
Samples a set of texture vectors given a set of shape in absolute (i.e. image) coordinates.- Parameters:
unalignedShapes
- Shapes in absolute coordinates.vTextures
- The set of textures.outputRF
- The output reference frame generated for sampling the textures.removeMean
- If true the mean from each texture vector (i.e. the DC) is removed.useTSP
- Use tangent space projection to align the shapes.useConvexHull
- If true the convex hull is used to determine the extent of a shape.
-
RegistrationMovie
public static void RegistrationMovie(java.lang.String filename, java.util.Vector<CDVector> vTexture, CAAMReferenceFrame rf)
Writes a movie file containing a set of textures warped to their mean shape. Curently diabled.- Parameters:
filename
- Output movie filename.vTexture
- The set of textures.rf
- The reference frame used for sampling the textures.
-
RegistrationMovie
public static void RegistrationMovie(java.lang.String filename, java.lang.String asfPath, boolean useConvexHull, boolean writeRefShape)
Writes a movie file containing all shapes from a directory warped to their mean shape.- Parameters:
filename
- Output movie filename.asfPath
- Path to annotation files.useConvexHull
- If true the convex hull is used to determine the extent of a shape.writeRefShape
- If true the reference shape corresponding to the movie file is written as "regshape.asf".
-
ASF2PTS
public static void ASF2PTS(java.lang.String path)
Converts AAM-API shape files (.asf) to the ISBE .pts format. Output is written in the directory 'pts'.- Parameters:
path
- Path to .asf files.
-
SymmetricPtCrv
public static double SymmetricPtCrv(CAAMShape s1, CAAMShape s2)
A symmetric poin- to-curve measure, i.e. SymmetricPtCrv(a,b) == SymmetricPtCrv( b,a );- Parameters:
s1
- First shapes2
- Second shape- Returns:
- The average symmetric point-to-curve error over all landmarks.
-
ShapeOverlap
public static double ShapeOverlap(CAAMShape model, CAAMShape gt)
Calculates the overlap between two shapes as specified in "Active Shape Model Segmentation With Optimal Features" Bram van Ginneken et al., IEEE TMI 21(8) Aug. 2002. Notice that this only makes sense for one-path closed shapes.- Parameters:
model
- Model shapegt
- Ground truth shape- Returns:
- The shape overlap (1 = perfect match, 0 = no overlap).
-
ReadLines
public static java.util.Vector<java.lang.String> ReadLines(java.lang.String filename)
Reads a file into an array of lines.- Parameters:
filename
- Name of input file.- Returns:
- A vector of text lines.
-
-