<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
The example I was using is based on my own extended levelset function and would require sending a lot of extra code. However, the problem can be easily seen with many of the itk level set functions by simply changing the input images' direction matrices to anything that's not identity. As an example I have taken the GeodesicActiveContourImageFilter.cxx example from the software guide, hard coded the parameters to those used for segmenting white matter on page 571, and changed the direction matrix of the level set inputs to something not identity and pasted the code below. I should note that I changed the level set function weights to emphasize the bug more. While the non-identity direction matrix is being forced here, it could easily happen automatically when working with 3D file formats like analyze, as was the case when I stumbled upon this bug. I have also attached to this e-mail the mask outputs for the default case where the diection matrix is identity and everything is working properly and the modified case where the only difference is that the direction matri is [0 -1; -1 0]. <BR> <BR>Anyway, I've already worked around this in my code by setting UseDirecion off when calculating the gradients, but I thought it was an important, subtle bug that you all might be want to know about.<BR> <BR>- Chad<br><BR>// Software Guide : BeginCodeSnippet<br>#include "itkGeodesicActiveContourLevelSetImageFilter.h"<br>// Software Guide : EndCodeSnippet<BR><br>#include "itkCurvatureAnisotropicDiffusionImageFilter.h"<br>#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"<br>#include "itkSigmoidImageFilter.h"<br>#include "itkFastMarchingImageFilter.h"<br>#include "itkRescaleIntensityImageFilter.h"<br>#include "itkBinaryThresholdImageFilter.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br>#include "itkChangeInformationImageFilter.h"<BR><br>int main()<br>{<br> //Problem Case<br> <br> double d11 = 0;<br> double d10 = -1;<br> double d00 = 0;<br> double d01 = -1;<br> <BR> //Default Case<br> /*<br> double d11 = 1;<br> double d10 = 0;<br> double d00 = 1;<br> double d01 = 0;<br> */<BR><br> // Software Guide : BeginLatex<br> //<br> // We now define the image type using a particular pixel type and<br> // dimension. In this case the \code{float} type is used for the pixels<br> // due to the requirements of the smoothing filter.<br> //<br> // Software Guide : EndLatex<BR> // Software Guide : BeginCodeSnippet<br> typedef float InternalPixelType;<br> const unsigned int Dimension = 2;<br> typedef itk::Image< InternalPixelType, Dimension > InternalImageType;<br> // Software Guide : EndCodeSnippet<BR><br> // The following lines instantiate the thresholding filter that will<br> // process the final level set at the output of the<br> // GeodesicActiveContourLevelSetImageFilter.<br> //<br> typedef unsigned char OutputPixelType;<br> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<br> typedef itk::BinaryThresholdImageFilter<<br> InternalImageType,<br> OutputImageType > ThresholdingFilterType;<BR> ThresholdingFilterType::Pointer thresholder = ThresholdingFilterType::New();<BR> thresholder->SetLowerThreshold( -1000.0 );<br> thresholder->SetUpperThreshold( 0.0 );<BR> thresholder->SetOutsideValue( 0 );<br> thresholder->SetInsideValue( 255 );<BR><br> // We instantiate reader and writer types in the following lines.<br> //<br> typedef itk::ImageFileReader< InternalImageType > ReaderType;<br> typedef itk::ImageFileWriter< OutputImageType > WriterType;<BR> ReaderType::Pointer reader = ReaderType::New();<br> WriterType::Pointer writer = WriterType::New();<BR> reader->SetFileName( "BrainProtonDensitySlice.png" );<br> writer->SetFileName( "Mask.png" );<BR><br> reader->Update();<BR> typedef itk::ChangeInformationImageFilter<InternalImageType> ChangeInfoFilterType;<br> ChangeInfoFilterType::Pointer chInfo = ChangeInfoFilterType::New();<br> chInfo->SetInput(reader->GetOutput());<br> chInfo->ChangeDirectionOn();<BR> InternalImageType::DirectionType dir = reader->GetOutput()->GetDirection();<br> dir(1,1) = d11;<br> dir(0,0) = d00;<br> dir(1,0) = d10;<br> dir(0,1) = d01;<br> chInfo->SetOutputDirection(dir);<br> chInfo->Update();<br> <br> std::cout<<chInfo->GetOutput()->GetDirection()<<"\n";<BR><br> // The RescaleIntensityImageFilter type is declared below. This filter will<br> // renormalize image before sending them to writers.<br> //<br> typedef itk::RescaleIntensityImageFilter<<br> InternalImageType,<br> OutputImageType > CastFilterType;<BR><br> // The \doxygen{CurvatureAnisotropicDiffusionImageFilter} type is<br> // instantiated using the internal image type.<br> //<br> typedef itk::CurvatureAnisotropicDiffusionImageFilter<<br> InternalImageType,<br> InternalImageType > SmoothingFilterType;<BR> SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();<BR><br> // The types of the<br> // GradientMagnitudeRecursiveGaussianImageFilter and<br> // SigmoidImageFilter are instantiated using the internal image<br> // type.<br> //<br> typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<<br> InternalImageType,<br> InternalImageType > GradientFilterType;<br> typedef itk::SigmoidImageFilter<<br> InternalImageType,<br> InternalImageType > SigmoidFilterType;<BR> GradientFilterType::Pointer gradientMagnitude = GradientFilterType::New();<BR> SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();<BR><br> // The minimum and maximum values of the SigmoidImageFilter output<br> // are defined with the methods \code{SetOutputMinimum()} and<br> // \code{SetOutputMaximum()}. In our case, we want these two values to be<br> // $0.0$ and $1.0$ respectively in order to get a nice speed image to feed<br> // the \code{FastMarchingImageFilter}. Additional details on the user of the<br> // \doxygen{SigmoidImageFilter} are presented in<br> // section~\ref{sec:IntensityNonLinearMapping}.<BR> sigmoid->SetOutputMinimum( 0.0 );<br> sigmoid->SetOutputMaximum( 1.0 );<BR><br> // We declare now the type of the FastMarchingImageFilter that<br> // will be used to generate the initial level set in the form of a distance<br> // map.<br> //<br> typedef itk::FastMarchingImageFilter<<br> InternalImageType,<br> InternalImageType > FastMarchingFilterType;<BR><br> // Next we construct one filter of this class using the \code{New()}<br> // method.<br> //<br> FastMarchingFilterType::Pointer fastMarching = FastMarchingFilterType::New();<BR> // Software Guide : BeginLatex<br> //<br> // In the following lines we instantiate the type of the<br> // GeodesicActiveContourLevelSetImageFilter and create an object of this<br> // type using the \code{New()} method.<br> //<br> // Software Guide : EndLatex<BR> // Software Guide : BeginCodeSnippet<br> typedef itk::GeodesicActiveContourLevelSetImageFilter< InternalImageType,<br> InternalImageType > GeodesicActiveContourFilterType;<br> GeodesicActiveContourFilterType::Pointer geodesicActiveContour =<br> GeodesicActiveContourFilterType::New();<br> // Software Guide : EndCodeSnippet<BR><br> // Software Guide : BeginLatex<br> //<br> // For the GeodesicActiveContourLevelSetImageFilter, scaling parameters<br> // are used to trade off between the propagation (inflation), the<br> // curvature (smoothing) and the advection terms. These parameters are set<br> // using methods \code{SetPropagationScaling()},<br> // \code{SetCurvatureScaling()} and \code{SetAdvectionScaling()}. In this<br> // example, we will set the curvature and advection scales to one and let<br> // the propagation scale be a command-line argument.<br> //<br> // \index{itk::Geodesic\-Active\-Contour\-LevelSet\-Image\-Filter!SetPropagationScaling()}<br> // \index{itk::Segmentation\-Level\-Set\-Image\-Filter!SetPropagationScaling()}<br> // \index{itk::Geodesic\-Active\-Contour\-LevelSet\-Image\-Filter!SetCurvatureScaling()}<br> // \index{itk::Segmentation\-Level\-Set\-Image\-Filter!SetCurvatureScaling()}<br> // \index{itk::Geodesic\-Active\-Contour\-LevelSet\-Image\-Filter!SetAdvectionScaling()}<br> // \index{itk::Segmentation\-Level\-Set\-Image\-Filter!SetAdvectionScaling()}<br> //<br> // Software Guide : EndLatex<BR> const double propagationScaling = 1.0;<BR> // Software Guide : BeginCodeSnippet<br> geodesicActiveContour->SetPropagationScaling( propagationScaling );<br> geodesicActiveContour->SetCurvatureScaling(0 );<br> geodesicActiveContour->SetAdvectionScaling( 10.0 );<br> // Software Guide : EndCodeSnippet<BR> // Once activiated the level set evolution will stop if the convergence<br> // criteria or if the maximum number of iterations is reached. The<br> // convergence criteria is defined in terms of the root mean squared (RMS)<br> // change in the level set function. The evolution is said to have<br> // converged if the RMS change is below a user specified threshold. In a<br> // real application is desirable to couple the evolution of the zero set<br> // to a visualization module allowing the user to follow the evolution of<br> // the zero set. With this feedback, the user may decide when to stop the<br> // algorithm before the zero set leaks through the regions of low gradient<br> // in the contour of the anatomical structure to be segmented.<BR> geodesicActiveContour->SetMaximumRMSError( 0.01 );<br> geodesicActiveContour->SetNumberOfIterations( 10000 );<BR><br> // Software Guide : BeginLatex<br> //<br> // The filters are now connected in a pipeline indicated in<br> // Figure~\ref{fig:GeodesicActiveContoursCollaborationDiagram} using the<br> // following lines:<br> //<br> // Software Guide : EndLatex<BR> // Software Guide : BeginCodeSnippet<br> smoothing->SetInput( chInfo->GetOutput() );<br> gradientMagnitude->SetInput( smoothing->GetOutput() );<br> sigmoid->SetInput( gradientMagnitude->GetOutput() );<BR><br> chInfo = ChangeInfoFilterType::New();<br> chInfo->SetInput(fastMarching->GetOutput());<br> chInfo->ChangeDirectionOn();<BR> dir = reader->GetOutput()->GetDirection();<br> dir(1,1) = d11;<br> dir(0,0) = d00;<br> dir(1,0) = d10;<br> dir(0,1) = d01;<br> chInfo->SetOutputDirection(dir);<BR><br> geodesicActiveContour->SetInput( chInfo->GetOutput() );<br> geodesicActiveContour->SetFeatureImage( sigmoid->GetOutput() );<BR> thresholder->SetInput( geodesicActiveContour->GetOutput() );<br> writer->SetInput( thresholder->GetOutput() );<br> // Software Guide : EndCodeSnippet<BR><br> // The CurvatureAnisotropicDiffusionImageFilter requires a couple of<br> // parameter to be defined. The following are typical values for $2D$<br> // images. However they may have to be adjusted depending on the amount of<br> // noise present in the input image. This filter has been discussed in<br> // section~\ref{sec:GradientAnisotropicDiffusionImageFilter}.<BR> smoothing->SetTimeStep( 0.125 );<br> smoothing->SetNumberOfIterations( 5 );<br> smoothing->SetConductanceParameter( 9.0 );<BR><br> // The GradientMagnitudeRecursiveGaussianImageFilter performs the<br> // equivalent of a convolution with a Gaussian kernel, followed by a<br> // derivative operator. The sigma of this Gaussian can be used to control<br> // the range of influence of the image edges. This filter has been discussed<br> // in Section~\ref{sec:GradientMagnitudeRecursiveGaussianImageFilter}<BR> const double sigma = 1;<br> gradientMagnitude->SetSigma( sigma );<BR><br> // The SigmoidImageFilter requires two parameters that define the linear<br> // transformation to be applied to the sigmoid argument. This parameters<br> // have been discussed in Sections~\ref{sec:IntensityNonLinearMapping} and<br> // \ref{sec:FastMarchingImageFilter}.<BR> const double alpha = -0.3;<br> const double beta = 2.0;<BR> sigmoid->SetAlpha( alpha );<br> sigmoid->SetBeta( beta );<BR><br> // The FastMarchingImageFilter requires the user to provide a seed<br> // point from which the level set will be generated. The user can actually<br> // pass not only one seed point but a set of them. Note the the<br> // FastMarchingImageFilter is used here only as a helper in the<br> // determination of an initial level set. We could have used the<br> // \doxygen{DanielssonDistanceMapImageFilter} in the same way.<br> //<br> // The seeds are passed stored in a container. The type of this<br> // container is defined as \code{NodeContainer} among the<br> // FastMarchingImageFilter traits.<br> //<br> typedef FastMarchingFilterType::NodeContainer NodeContainer;<br> typedef FastMarchingFilterType::NodeType NodeType;<BR> NodeContainer::Pointer seeds = NodeContainer::New();<BR> InternalImageType::IndexType seedPosition;<BR> seedPosition[0] = 56;<br> seedPosition[1] = 92;<BR><br> // Nodes are created as stack variables and initialized with a value and an<br> // \doxygen{Index} position. Note that here we assign the value of minus the<br> // user-provided distance to the unique node of the seeds passed to the<br> // FastMarchingImageFilter. In this way, the value will increment<br> // as the front is propagated, until it reaches the zero value corresponding<br> // to the contour. After this, the front will continue propagating until it<br> // fills up the entire image. The initial distance is taken here from the<br> // command line arguments. The rule of thumb for the user is to select this<br> // value as the distance from the seed points at which she want the initial<br> // contour to be.<br> const double initialDistance = 5;<BR> NodeType node;<BR> const double seedValue = - initialDistance;<BR> node.SetValue( seedValue );<br> node.SetIndex( seedPosition );<BR><br> // The list of nodes is initialized and then every node is inserted using<br> // the \code{InsertElement()}.<BR> seeds->Initialize();<br> seeds->InsertElement( 0, node );<BR><br> // The set of seed nodes is passed now to the<br> // FastMarchingImageFilter with the method<br> // \code{SetTrialPoints()}.<br> //<br> fastMarching->SetTrialPoints( seeds );<BR><br> // Since the FastMarchingImageFilter is used here just as a<br> // Distance Map generator. It does not require a speed image as input.<br> // Instead the constant value $1.0$ is passed using the<br> // \code{SetSpeedConstant()} method.<br> //<br> fastMarching->SetSpeedConstant( 1.0 );<BR><br> // Here we configure all the writers required to see the intermediate<br> // outputs of the pipeline. This is added here only for<br> // pedagogical/debugging purposes. These intermediate output are normaly not<br> // required. Only the output of the final thresholding filter should be<br> // relevant. Observing intermediate output is helpful in the process of<br> // fine tuning the parameters of filters in the pipeline.<br> //<br> CastFilterType::Pointer caster1 = CastFilterType::New();<br> CastFilterType::Pointer caster2 = CastFilterType::New();<br> CastFilterType::Pointer caster3 = CastFilterType::New();<br> CastFilterType::Pointer caster4 = CastFilterType::New();<BR> WriterType::Pointer writer1 = WriterType::New();<br> WriterType::Pointer writer2 = WriterType::New();<br> WriterType::Pointer writer3 = WriterType::New();<br> WriterType::Pointer writer4 = WriterType::New();<BR> caster1->SetInput( smoothing->GetOutput() );<br> writer1->SetInput( caster1->GetOutput() );<br> writer1->SetFileName("GeodesicActiveContourImageFilterOutput1.png");<br> caster1->SetOutputMinimum( 0 );<br> caster1->SetOutputMaximum( 255 );<br> writer1->Update();<BR> caster2->SetInput( gradientMagnitude->GetOutput() );<br> writer2->SetInput( caster2->GetOutput() );<br> writer2->SetFileName("GeodesicActiveContourImageFilterOutput2.png");<br> caster2->SetOutputMinimum( 0 );<br> caster2->SetOutputMaximum( 255 );<br> writer2->Update();<BR> caster3->SetInput( sigmoid->GetOutput() );<br> writer3->SetInput( caster3->GetOutput() );<br> writer3->SetFileName("GeodesicActiveContourImageFilterOutput3.png");<br> caster3->SetOutputMinimum( 0 );<br> caster3->SetOutputMaximum( 255 );<br> writer3->Update();<BR> caster4->SetInput( fastMarching->GetOutput() );<br> writer4->SetInput( caster4->GetOutput() );<br> writer4->SetFileName("GeodesicActiveContourImageFilterOutput4.png");<br> caster4->SetOutputMinimum( 0 );<br> caster4->SetOutputMaximum( 255 );<BR><br> // The FastMarchingImageFilter requires the user to specify the<br> // size of the image to be produced as output. This is done using the<br> // \code{SetOutputSize()}. Note that the size is obtained here from the<br> // output image of the smoothing filter. The size of this image is valid<br> // only after the \code{Update()} methods of this filter has been called<br> // directly or indirectly.<br> //<br> fastMarching->SetOutputSize(<br> reader->GetOutput()->GetBufferedRegion().GetSize() );<BR> <BR><br> // Software Guide : BeginLatex<br> //<br> // The invocation of the \code{Update()} method on the writer triggers the<br> // execution of the pipeline. As usual, the call is placed in a<br> // \code{try/catch} block should any errors occur or exceptions be thrown.<br> //<br> // Software Guide : EndLatex<BR> // Software Guide : BeginCodeSnippet<br> try<br> {<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & excep )<br> {<br> std::cerr << "Exception caught !" << std::endl;<br> std::cerr << excep << std::endl;<br> }<br> // Software Guide : EndCodeSnippet<BR> // Print out some useful information<br> std::cout << std::endl;<br> std::cout << "Max. no. iterations: " << geodesicActiveContour->GetNumberOfIterations() << std::endl;<br> std::cout << "Max. RMS error: " << geodesicActiveContour->GetMaximumRMSError() << std::endl;<br> std::cout << std::endl;<br> std::cout << "No. elpased iterations: " << geodesicActiveContour->GetElapsedIterations() << std::endl;<br> std::cout << "RMS change: " << geodesicActiveContour->GetRMSChange() << std::endl;<BR> writer4->Update();<BR><br> // The following writer type is used to save the output of the time-crossing<br> // map in a file with apropiate pixel representation. The advantage of saving<br> // this image in native format is that it can be used with a viewer to help<br> // determine an appropriate threshold to be used on the output of the<br> // fastmarching filter.<br> //<br> typedef itk::ImageFileWriter< InternalImageType > InternalWriterType;<BR> InternalWriterType::Pointer mapWriter = InternalWriterType::New();<br> mapWriter->SetInput( fastMarching->GetOutput() );<br> mapWriter->SetFileName("GeodesicActiveContourImageFilterOutput4.mha");<br> mapWriter->Update();<BR> InternalWriterType::Pointer speedWriter = InternalWriterType::New();<br> speedWriter->SetInput( sigmoid->GetOutput() );<br> speedWriter->SetFileName("GeodesicActiveContourImageFilterOutput3.mha");<br> speedWriter->Update();<BR> InternalWriterType::Pointer gradientWriter = InternalWriterType::New();<br> gradientWriter->SetInput( gradientMagnitude->GetOutput() );<br> gradientWriter->SetFileName("GeodesicActiveContourImageFilterOutput2.mha");<br> gradientWriter->Update();<BR><br> // Software Guide : BeginLatex<br> //<br> // Let's now run this example using as input the image<br> // \code{BrainProtonDensitySlice.png} provided in the directory<br> // \code{Examples/Data}. We can easily segment the major anatomical<br> // structures by providing seeds in the appropriate locations.<br> // Table~\ref{tab:GeodesicActiveContourImageFilterOutput2} presents the<br> // parameters used for some structures.<br> //<br> // \begin{table}<br> // \begin{center}<br> // \begin{tabular}{|l|c|c|c|c|c|c|c|c|}<br> // \hline<br> // Structure & Seed Index & Distance & $\sigma$ &<br> // $\alpha$ & $\beta$ & Propag. & Output Image \\ \hline<br> // Left Ventricle & $(81,114)$ & 5.0 & 1.0 & -0.5 & 3.0 & 2.0 & First \\ \hline<br> // Right Ventricle & $(99,114)$ & 5.0 & 1.0 & -0.5 & 3.0 & 2.0 & Second \\ \hline<br> // White matter & $(56, 92)$ & 5.0 & 1.0 & -0.3 & 2.0 & 10.0 & Third \\ \hline<br> // Gray matter & $(40, 90)$ & 5.0 & 0.5 & -0.3 & 2.0 & 10.0 & Fourth \\ \hline<br> // \end{tabular}<br> // \end{center}<br> // \itkcaption[GeodesicActiveContour segmentation example parameters]{Parameters used<br> // for segmenting some brain structures shown in<br> // Figure~\ref{fig:GeodesicActiveContourImageFilterOutput2} using the filter<br> // GeodesicActiveContourLevelSetImageFilter.<br> // \label{tab:GeodesicActiveContourImageFilterOutput2}}<br> // \end{table}<br> //<br> // Figure~\ref{fig:GeodesicActiveContourImageFilterOutput} presents the<br> // intermediate outputs of the pipeline illustrated in<br> // Figure~\ref{fig:GeodesicActiveContoursCollaborationDiagram}. They are<br> // from left to right: the output of the anisotropic diffusion filter, the<br> // gradient magnitude of the smoothed image and the sigmoid of the gradient<br> // magnitude which is finally used as the edge potential for the<br> // GeodesicActiveContourLevelSetImageFilter.<br> //<br> // \begin{figure} \center<br> // \includegraphics[height=0.40\textheight]{BrainProtonDensitySlice.eps}<br> // \includegraphics[height=0.40\textheight]{GeodesicActiveContourImageFilterOutput1.eps}<br> // \includegraphics[height=0.40\textheight]{GeodesicActiveContourImageFilterOutput2.eps}<br> // \includegraphics[height=0.40\textheight]{GeodesicActiveContourImageFilterOutput3.eps}<br> // \itkcaption[GeodesicActiveContourLevelSetImageFilter intermediate<br> // output]{Images generated by the segmentation process based on the<br> // GeodesicActiveContourLevelSetImageFilter. From left to right and top to<br> // bottom: input image to be segmented, image smoothed with an<br> // edge-preserving smoothing filter, gradient magnitude of the smoothed<br> // image, sigmoid of the gradient magnitude. This last image, the sigmoid, is<br> // used to compute the speed term for the front propagation.}<br> // \label{fig:GeodesicActiveContourImageFilterOutput} \end{figure}<br> //<br> // Segmentations of the main brain structures are presented in<br> // Figure~\ref{fig:GeodesicActiveContourImageFilterOutput2}. The results<br> // are quite similar to those obtained with the<br> // ShapeDetectionLevelSetImageFilter in<br> // Section~\ref{sec:ShapeDetectionLevelSetFilter}.<br> //<br> // Note that a relatively larger propagation scaling value was required to<br> // segment the white matter. This is due to two factors: the lower<br> // contrast at the border of the white matter and the complex shape of the<br> // structure. Unfortunately the optimal value of these scaling parameters<br> // can only be determined by experimentation. In a real application we<br> // could imagine an interactive mechanism by which a user supervises the<br> // contour evolution and adjusts these parameters accordingly.<br> //<br> // \begin{figure} \center<br> // \includegraphics[width=0.24\textwidth]{GeodesicActiveContourImageFilterOutput5.eps}<br> // \includegraphics[width=0.24\textwidth]{GeodesicActiveContourImageFilterOutput6.eps}<br> // \includegraphics[width=0.24\textwidth]{GeodesicActiveContourImageFilterOutput7.eps}<br> // \includegraphics[width=0.24\textwidth]{GeodesicActiveContourImageFilterOutput8.eps}<br> // \itkcaption[GeodesicActiveContourImageFilter segmentations]{Images generated by the<br> // segmentation process based on the GeodesicActiveContourImageFilter. From left to<br> // right: segmentation of the left ventricle, segmentation of the right<br> // ventricle, segmentation of the white matter, attempt of segmentation of<br> // the gray matter.}<br> // \label{fig:GeodesicActiveContourImageFilterOutput2}<br> // \end{figure}<br> //<br> // Software Guide : EndLatex<BR> char ch;<br> std::cin>>ch;<BR> return 0;<br>}<br><BR>                                            </div></body>
</html>