Commit 3bcc0ab3 authored by Jordi Inglada's avatar Jordi Inglada
Browse files

Exemple Connected Threshold

parent 6a5f7ff9
......@@ -7,6 +7,7 @@ SUBDIRS(
DataRepresentation
IO
Filtering
Segmentation
ChangeDetection
Visu
Learning
......
Examples/Data/QB_Suburb.png

78.6 KB | W: | H:

Examples/Data/QB_Suburb.png

51.5 KB | W: | H:

Examples/Data/QB_Suburb.png
Examples/Data/QB_Suburb.png
Examples/Data/QB_Suburb.png
Examples/Data/QB_Suburb.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -171,27 +171,6 @@ int main( int argc, char ** argv )
writer->Update();
// Software Guide : BeginLatex
// Figure~\ref{fig:Align} shows the result of applying the alignment
// detection to a small patch extracted from a VHR image.
// \begin{figure}
// \center
// \includegraphics[width=0.35\textwidth]{QB_Suburb.eps}
// \includegraphics[width=0.35\textwidth]{QB_SuburbAlign.eps}
// \itkcaption[Lee Filter Application]{Result of applying the
// \doxygen{ImageToPathListAlignFilter} to a VHR image of a suburb.}
// \label{fig:Align}
// \end{figure}
//
// Software Guide : EndLatex
// \relatedClasses
// \begin{itemize}
// \item \doxygen{FrostImageFilter}
// \end{itemize}
//
// Software Guide : EndLatex
return EXIT_SUCCESS;
......
#
# Examples on the use of segmentation algorithms
#
PROJECT(ImageSegmentation)
INCLUDE_REGULAR_EXPRESSION("^.*$")
ADD_EXECUTABLE(ConnectedThresholdImageFilter ConnectedThresholdImageFilter.cxx )
TARGET_LINK_LIBRARIES(ConnectedThresholdImageFilter OTBCommon OTBIO ITKNumerics ITKIO)
#ADD_EXECUTABLE(ConfidenceConnected ConfidenceConnected.cxx )
#TARGET_LINK_LIBRARIES(ConfidenceConnected ITKNumerics ITKIO)
#ADD_EXECUTABLE(NeighborhoodConnectedImageFilter NeighborhoodConnectedImageFilter.cxx )
#TARGET_LINK_LIBRARIES(NeighborhoodConnectedImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(IsolatedConnectedImageFilter IsolatedConnectedImageFilter.cxx )
#TARGET_LINK_LIBRARIES(IsolatedConnectedImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(ConnectedThresholdImageFilter ConnectedThresholdImageFilter.cxx )
#TARGET_LINK_LIBRARIES(ConnectedThresholdImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(FastMarchingImageFilter FastMarchingImageFilter.cxx )
#TARGET_LINK_LIBRARIES(FastMarchingImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(GeodesicActiveContourShapePriorLevelSetImageFilter GeodesicActiveContourShapePriorLevelSetImageFilter.cxx )
#TARGET_LINK_LIBRARIES(GeodesicActiveContourShapePriorLevelSetImageFilter ITKNumerics ITKIO ITKStatistics)
#ADD_EXECUTABLE(DeformableModel2 DeformableModel2.cxx )
#TARGET_LINK_LIBRARIES(DeformableModel2 ITKAlgorithms ITKNumerics ITKIO)
#ADD_EXECUTABLE(GibbsPriorImageFilter1 GibbsPriorImageFilter1.cxx )
#TARGET_LINK_LIBRARIES(GibbsPriorImageFilter1 ITKAlgorithms ITKNumerics ITKIO)
#ADD_EXECUTABLE(HoughTransform2DCirclesImageFilter HoughTransform2DCirclesImageFilter.cxx )
#TARGET_LINK_LIBRARIES(HoughTransform2DCirclesImageFilter ITKBasicFilters ITKIO)
#ADD_EXECUTABLE(HoughTransform2DLinesImageFilter HoughTransform2DLinesImageFilter.cxx )
#TARGET_LINK_LIBRARIES(HoughTransform2DLinesImageFilter ITKBasicFilters ITKIO)
#ADD_EXECUTABLE(VectorConfidenceConnected VectorConfidenceConnected.cxx )
#TARGET_LINK_LIBRARIES(VectorConfidenceConnected ITKNumerics ITKIO)
#ADD_EXECUTABLE(RelabelComponentImageFilter RelabelComponentImageFilter.cxx )
#TARGET_LINK_LIBRARIES(RelabelComponentImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(CannySegmentationLevelSetImageFilter CannySegmentationLevelSetImageFilter.cxx )
#TARGET_LINK_LIBRARIES(CannySegmentationLevelSetImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(GeodesicActiveContourImageFilter GeodesicActiveContourImageFilter.cxx )
#TARGET_LINK_LIBRARIES(GeodesicActiveContourImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(ShapeDetectionLevelSetFilter ShapeDetectionLevelSetFilter.cxx )
#TARGET_LINK_LIBRARIES(ShapeDetectionLevelSetFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(SegmentationExamples2 SegmentationExamples2.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples2 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(SegmentationExamples3 SegmentationExamples3.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples3 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(CurvesLevelSetImageFilter CurvesLevelSetImageFilter.cxx )
#TARGET_LINK_LIBRARIES(CurvesLevelSetImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(WatershedSegmentation1 WatershedSegmentation1.cxx )
#TARGET_LINK_LIBRARIES(WatershedSegmentation1 ITKAlgorithms ITKNumerics ITKIO)
#ADD_EXECUTABLE(SegmentationExamples SegmentationExamples.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon)
#ADD_EXECUTABLE(SegmentationExamples5 SegmentationExamples5.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples5 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(SegmentationExamples8 SegmentationExamples8.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples8 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(SegmentationExamples9 SegmentationExamples9.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples9 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(CellularSegmentation1 CellularSegmentation1.cxx )
#TARGET_LINK_LIBRARIES(CellularSegmentation1 ITKAlgorithms ITKCommon ITKNumerics ITKIO)
#ADD_EXECUTABLE(DeformableModel1 DeformableModel1.cxx )
#TARGET_LINK_LIBRARIES(DeformableModel1 ITKAlgorithms ITKNumerics ITKIO)
#ADD_EXECUTABLE(LaplacianSegmentationLevelSetImageFilter LaplacianSegmentationLevelSetImageFilter.cxx )
#TARGET_LINK_LIBRARIES(LaplacianSegmentationLevelSetImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(ThresholdSegmentationLevelSetImageFilter ThresholdSegmentationLevelSetImageFilter.cxx )
#TARGET_LINK_LIBRARIES(ThresholdSegmentationLevelSetImageFilter ITKNumerics ITKIO)
#ADD_EXECUTABLE(SegmentationExamples4 SegmentationExamples4.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples4 ITKAlgorithms ITKIO ITKBasicFilters ITKCommonITKStatistics)
#ADD_EXECUTABLE(SegmentationExamples6 SegmentationExamples6.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples6 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#ADD_EXECUTABLE(SegmentationExamples7 SegmentationExamples7.cxx )
#TARGET_LINK_LIBRARIES(SegmentationExamples7 ITKAlgorithms ITKIO
#ITKBasicFilters ITKCommon ITKStatistics)
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
// Software Guide : BeginCommandLineArgs
// INPUTS: {QB_Suburb.png}
// OUTPUTS: {ConnectedThresholdOutput1.png}
// 110 38 50 100
// Software Guide : EndCommandLineArgs
// Software Guide : BeginCommandLineArgs
// INPUTS: {QB_Suburb.png}
// OUTPUTS: {ConnectedThresholdOutput2.png}
// 118 100 0 10
// Software Guide : EndCommandLineArgs
// Software Guide : BeginCommandLineArgs
// INPUTS: {QB_Suburb.png}
// OUTPUTS: {ConnectedThresholdOutput3.png}
// 169 146 220 255
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
//
// The following example illustrates the use of the
// \doxygen{itk::ConnectedThresholdImageFilter}. This filter uses the
// flood fill iterator. Most of the algorithmic complexity of a region
// growing method comes from visiting neighboring pixels. The flood
// fill iterator assumes this responsibility and greatly simplifies
// the implementation of the region growing algorithm. Thus the
// algorithm is left to establish a criterion to decide whether a
// particular pixel should be included in the current region or not.
//
// \index{itk::FloodFillIterator!In Region Growing}
// \index{itk::ConnectedThresholdImageFilter}
// \index{itk::ConnectedThresholdImageFilter!header}
//
// The criterion used by the ConnectedThresholdImageFilter is based on an
// interval of intensity values provided by the user. Values of lower and
// upper threshold should be provided. The region growing algorithm includes
// those pixels whose intensities are inside the interval.
//
// \begin{equation}
// I(\mathbf{X}) \in [ \mbox{lower}, \mbox{upper} ]
// \end{equation}
//
// Let's look at the minimal code required to use this algorithm. First, the
// following header defining the ConnectedThresholdImageFilter class
// must be included.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkConnectedThresholdImageFilter.h"
// Software Guide : EndCodeSnippet
#include "itkImage.h"
#include "itkCastImageFilter.h"
// Software Guide : BeginLatex
//
// Noise present in the image can reduce the capacity of this filter to grow
// large regions. When faced with noisy images, it is usually convenient to
// pre-process the image by using an edge-preserving smoothing filter. Any of
// the filters discussed in Section~\ref{sec:EdgePreservingSmoothingFilters}
// could be used to this end. In this particular example we use the
// \doxygen{CurvatureFlowImageFilter}, hence we need to include its header
// file.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkCurvatureFlowImageFilter.h"
// Software Guide : EndCodeSnippet
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
int main( int argc, char *argv[])
{
if( argc < 7 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " inputImage outputImage seedX seedY lowerThreshold upperThreshold" << std::endl;
return 1;
}
// Software Guide : BeginLatex
//
// We declare the image type based on a particular pixel type and
// dimension. In this case the \code{float} type is used for the pixels
// due to the requirements of the smoothing filter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef float InternalPixelType;
const unsigned int Dimension = 2;
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
// Software Guide : EndCodeSnippet
typedef unsigned char OutputPixelType;
//FIXME : OTBIMAGE
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::CastImageFilter< InternalImageType, OutputImageType >
CastingFilterType;
CastingFilterType::Pointer caster = CastingFilterType::New();
// We instantiate reader and writer types
//
typedef otb::ImageFileReader< InternalImageType > ReaderType;
typedef otb::ImageFileWriter< OutputImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( argv[1] );
writer->SetFileName( argv[2] );
// Software Guide : BeginLatex
//
//
// The smoothing filter is instantiated using the image type as
// a template parameter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::CurvatureFlowImageFilter< InternalImageType, InternalImageType >
CurvatureFlowImageFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then the filter is created by invoking the \code{New()} method and
// assigning the result to a \doxygen{itk::SmartPointer}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
CurvatureFlowImageFilterType::Pointer smoothing =
CurvatureFlowImageFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We now declare the type of the region growing filter. In this case it is
// the ConnectedThresholdImageFilter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ConnectedThresholdImageFilter< InternalImageType,
InternalImageType > ConnectedFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then we construct one filter of this class using the \code{New()}
// method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ConnectedFilterType::Pointer connectedThreshold = ConnectedFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now it is time to connect a simple, linear pipeline. A file reader is
// added at the beginning of the pipeline and a cast filter and writer
// are added at the end. The cast filter is required to convert
// \code{float} pixel types to integer types since only a few image file
// formats support \code{float} types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
smoothing->SetInput( reader->GetOutput() );
connectedThreshold->SetInput( smoothing->GetOutput() );
caster->SetInput( connectedThreshold->GetOutput() );
writer->SetInput( caster->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The CurvatureFlowImageFilter requires a couple of parameters to
// be defined. The following are typical values, however
// they may have to be adjusted depending on the amount of noise present in
// the input image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
smoothing->SetNumberOfIterations( 5 );
smoothing->SetTimeStep( 0.125 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The ConnectedThresholdImageFilter has two main parameters to be
// defined. They are the lower and upper thresholds of the interval in
// which intensity values should fall in order to be included in the
// region. Setting these two values too close will not allow enough
// flexibility for the region to grow. Setting them too far apart will
// result in a region that engulfs the image.
//
// \index{itk::ConnectedThresholdImageFilter!SetUpper()}
// \index{itk::ConnectedThresholdImageFilter!SetLower()}
//
// Software Guide : EndLatex
const InternalPixelType lowerThreshold = atof( argv[5] );
const InternalPixelType upperThreshold = atof( argv[6] );
// Software Guide : BeginCodeSnippet
connectedThreshold->SetLower( lowerThreshold );
connectedThreshold->SetUpper( upperThreshold );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The output of this filter is a binary image with zero-value pixels
// everywhere except on the extracted region. The intensity value set
// inside the region is selected with the method \code{SetReplaceValue()}
//
// \index{itk::ConnectedThresholdImageFilter!SetReplaceValue()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
connectedThreshold->SetReplaceValue(
itk::NumericTraits<OutputPixelType>::max() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The initialization of the algorithm requires the user to provide a seed
// point. It is convenient to select this point to be placed in a
// \emph{typical} region of the structure to be segmented. The
// seed is passed in the form of a \doxygen{itk::Index} to the \code{SetSeed()}
// method.
//
// \index{itk::ConnectedThresholdImageFilter!SetSeed()}
//
// Software Guide : EndLatex
InternalImageType::IndexType index;
index[0] = atoi( argv[3] );
index[1] = atoi( argv[4] );
// Software Guide : BeginCodeSnippet
connectedThreshold->SetSeed( index );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The invocation of the \code{Update()} method on the writer triggers the
// execution of the pipeline. It is usually wise to put update calls in a
// \code{try/catch} block in case errors occur and exceptions are thrown.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
writer->Update();
}
catch( itk::ExceptionObject & excep )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Let's run this example using as input the image
// \code{QB\_Suburb.png} provided in the directory
// \code{Examples/Data}. We can easily segment the major
// structures by providing seeds in the appropriate locations and defining
// values for the lower and upper thresholds.
// Figure~\ref{fig:ConnectedThresholdOutput} illustrates several examples of
// segmentation. The parameters used are presented in
// Table~\ref{tab:ConnectedThresholdOutput}.
//
// \begin{table}
// \begin{center}
// \begin{tabular}{|l|c|c|c|c|}
// \hline
// Structure & Seed Index & Lower & Upper & Output Image \\ \hline
// Road & $(110,38)$ & 50 & 100 & Second from left in Figure \ref{fig:ConnectedThresholdOutput} \\ \hline
// Shadow & $(118,100)$ & 0 & 10 & Third from left in Figure \ref{fig:ConnectedThresholdOutput} \\ \hline
// Building & $(169,146)$ & 220 & 255 & Fourth from left in Figure \ref{fig:ConnectedThresholdOutput} \\ \hline
// \end{tabular}
// \end{center}
// \itkcaption[ConnectedThreshold example parameters]{Parameters used for
// segmenting some structures shown in
// Figure~\ref{fig:ConnectedThresholdOutput} with the filter
// \doxygen{ConnectedThresholdImageFilter}.\label{tab:ConnectedThresholdOutput}}
// \end{table}
//
// \begin{figure} \center
// \includegraphics[width=0.24\textwidth]{QB_Suburb.eps}
// \includegraphics[width=0.24\textwidth]{ConnectedThresholdOutput1.eps}
// \includegraphics[width=0.24\textwidth]{ConnectedThresholdOutput2.eps}
// \includegraphics[width=0.24\textwidth]{ConnectedThresholdOutput3.eps}
// \itkcaption[ConnectedThreshold segmentation results]{Segmentation results
// for the ConnectedThreshold filter for various seed points.}
// \label{fig:ConnectedThresholdOutput}
// \end{figure}
//
// Notice that some objects are not being completely segmented. This
// illustrates the vulnerability of the region growing methods when the
// structures to be segmented do not have a homogeneous
// statistical distribution over the image space. You may want to
// experiment with different values of the lower and upper thresholds to
// verify how the accepted region will extend.
//
// Another option for segmenting regions is to take advantage of the
// functionality provided by the ConnectedThresholdImageFilter for
// managing multiple seeds. The seeds can be passed one by one to the
// filter using the \code{AddSeed()} method. You could imagine a user
// interface in which an operator clicks on multiple points of the object
// to be segmented and each selected point is passed as a seed to this
// filter.
//
// Software Guide : EndLatex
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment