1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: HybridSegmentationFuzzyVoronoi.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif
// Software Guide : BeginCommandLineArgs
// INPUTS: {BrainT1Slice.png}
// OUTPUTS: {HybridSegmentationFuzzyVoronoiOutput.png}
// 140 125 140 25 0.2 2.0
// Software Guide : EndCommandLineArgs
// Software Guide : BeginCommandLineArgs
// INPUTS: {FatMRISlice.png}
// OUTPUTS: {HybridSegmentationFuzzyVoronoiOutput2.png}
// 80 200 140 300 0.3 3.0
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
//
// This example illustrates the use of the
// \doxygen{SimpleFuzzyConnectednessScalarImageFilter} and
// \doxygen{VoronoiSegmentationImageFilter} to build a hybrid segmentation that
// integrates fuzzy connectedness with the Voronoi diagram classification.
//
// Please note that the Fuzzy Connectedness algorithm is covered by a patent
// \cite{Udupa1998}. For this reason the current example is located in the
// \texttt{Examples/Patented} subdirectory.
//
// First, we include the header files of the two filters.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkSimpleFuzzyConnectednessScalarImageFilter.h"
#include "itkVoronoiSegmentationImageFilter.h"
// Software Guide : EndCodeSnippet
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
int main( int argc, char *argv[] )
{
if( argc < 9 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " inputImage outputImage seedX seedY " << std::endl;
std::cerr << " estimateMean estimateVariance (used by FuzzySegmentation) " << std::endl;
std::cerr << " meanTolerance standardDeviationTolerance (used by VoronoiSegmentation) " << std::endl;
return 1;
}
// Software Guide : BeginLatex
//
// Next, we declare the pixel type and image dimension and
// specify the image type to be used as input.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef float InputPixelType;
const unsigned int Dimension = 2;
typedef itk::Image< InputPixelType, Dimension > InputImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Fuzzy connectedness segmentation is performed first to generate
// a rough segmentation that yields a sample of tissue from the
// region to be segmented. A binary result, representing the
// sample, is used as a prior for the next step. Here, we use the
// \doxygen{SimpleFuzzyConnectednessScalarImageFilter}, but we may
// also utilize any other image segmentation filter instead. The
// result produced by the fuzzy segmentation filter is stored in a
// binary image. Below, we declare the type of the image using a
// pixel type and a spatial dimension.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char BinaryPixelType;
typedef itk::Image< BinaryPixelType, Dimension > BinaryImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The fuzzy segmentation filter type is instantiated here using the input
// and binary image types as template parameters.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::SimpleFuzzyConnectednessScalarImageFilter<
InputImageType,
BinaryImageType
> FuzzySegmentationFilterType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The fuzzy connectedness segmentation filter is created by invoking the
// \code{New()} method and assigning the result to a
// \doxygen{SmartPointer}.
//
// \index{itk::SimpleFuzzy\-Connectedness\-Scalar\-Image\-Filter!New()}
// \index{itk::SimpleFuzzy\-Connectedness\-Scalar\-Image\-Filter!Pointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
FuzzySegmentationFilterType::Pointer fuzzysegmenter =
FuzzySegmentationFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// In the next step of the hybrid segmentation method, the prior generated
// from the fuzzy segmentation is used to build a homogeneity measurement
// for the object. A VoronoiSegmentationImageFilter uses the
// homogeneity measurement to drive iterative subdivision of Voronoi regions
// and to generate the final segmentation result (for details, please see
// \cite{Imielinska2000b}). In this example, the result of the
// VoronoiSegmentationImageFilter is sent to a writer. Its output
// type is conveniently declared as one that is compatible with the writer.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char OutputPixelType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The following lines instantiate the Voronoi segmentation filter.
//
// \index{itk::Voronoi\-Segmentation\-Image\-Filter!New()}
// \index{itk::Voronoi\-Segmentation\-Image\-Filter!Pointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::VoronoiSegmentationImageFilter<
InputImageType,
OutputImageType,
BinaryImageType>
VoronoiSegmentationFilterType;
VoronoiSegmentationFilterType::Pointer voronoisegmenter =
VoronoiSegmentationFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// \begin{figure} \center
// \includegraphics[width=0.44\textwidth]{BrainT1Slice.eps}
// \includegraphics[width=0.44\textwidth]{HybridSegmentationFuzzyVoronoiOutput.eps}
// \itkcaption[Segmentation results for the hybrid segmentation
// approach]{Segmentation results for the hybrid segmentation approach.}
// \label{fig:HybridSegmentationFuzzyVoronoiOutput}
// \end{figure}
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// \begin{figure} \center
// \includegraphics[width=0.44\textwidth]{FatMRISlice.eps}
// \includegraphics[width=0.44\textwidth]{HybridSegmentationFuzzyVoronoiOutput2.eps}
// \itkcaption[Segmentation result for the hybrid segmentation
// approach]{Another segmentation result for the hybrid segmentation
// approach.}
// \label{fig:HybridSegmentationFuzzyVoronoiOutput2}
// \end{figure}
//
// Software Guide : EndLatex
// We instantiate reader and writer types
//
typedef itk::ImageFileReader< InputImageType > ReaderType;
typedef itk::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 input that is passed to the fuzzy segmentation filter is taken from
// the reader.
//
// \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetInput()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
fuzzysegmenter->SetInput( reader->GetOutput() );
// Software Guide : EndCodeSnippet
InputImageType::IndexType index;
index[0] = atoi(argv[3]);
index[1] = atoi(argv[4]);
const float mean = atof(argv[5]);
const float variance = atof(argv[6]);
const float meanTolerance = atof( argv[7] );
const float stdTolerance = atof( argv[8] );
// Software Guide : BeginLatex
//
// The parameters of the fuzzy segmentation filter are defined here. A seed
// point is provided with the method \code{SetObjectSeed()} in order to
// initialize the region to be grown. Estimated values for the mean and
// variance of the object intensities are also provided with the methods
// \code{SetMean()} and \code{SetVariance()}, respectively. A threshold
// value for generating the binary object is preset with the method
// \code{SetThreshold()}. For details describing the role of the mean and
// variance on the computation of the segmentation, please see
// \cite{Udupa1996}.
//
// \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetObjectSeed()}
// \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetMean()}
// \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetVariance()}
// \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetThreshold()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
fuzzysegmenter->SetObjectSeed( index );
fuzzysegmenter->SetMean( mean );
fuzzysegmenter->SetVariance( variance );
fuzzysegmenter->SetThreshold( 0.5 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The execution of the fuzzy segmentation filter is triggered by the
// \code{Update()} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
fuzzysegmenter->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The input to the Voronoi diagram classification filter is obtained from
// the reader and the prior is obtained from the fuzzy segmentation filter.
//
// \index{itk::VoronoiSegmentationImageFilter!SetInput()}
// \index{itk::VoronoiSegmentationImageFilter!TakeAPrior()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
voronoisegmenter->SetInput( reader->GetOutput() );
voronoisegmenter->TakeAPrior( fuzzysegmenter->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The tolerance levels for testing the mean and standard deviation are set
// with the methods \code{SetMeanPercentError()} and
// \code{SetSTDPercentError()}. Note that the
// fuzzy segmentation filter uses \emph{variance} as parameter while
// the Voronoi segmentation filter uses the tolerance of the
// \emph{standard deviation} as a parameter. For more details on how these
// parameters should be selected, please see \cite{Imielinska2000b}.
//
// \index{itk::VoronoiSegmentationImageFilter!SetMeanPercentError()}
// \index{itk::VoronoiSegmentationImageFilter!SetSTDPercentError()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
voronoisegmenter->SetMeanPercentError( meanTolerance );
voronoisegmenter->SetSTDPercentError( stdTolerance );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The \emph{resolution} of the Voronoi diagram classification can be
// chosen with the method \code{SetMinRegion()}.
//
// \index{itk::VoronoiSegmentationImageFilter!SetMinRegion()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
voronoisegmenter->SetMinRegion( 5 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The execution of the Voronoi segmentation filter is triggered with the
// \code{Update()} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
voronoisegmenter->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The output of the Voronoi diagram classification is an image mask with
// zeros everywhere and ones inside the segmented object. This image will
// appear black on many image viewers since they do not usually stretch
// the gray levels. Here, we add a \doxygen{RescaleIntensityImageFilter}
// in order to expand the dynamic range to more typical values.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::RescaleIntensityImageFilter< OutputImageType,OutputImageType >
ScalerFilterType;
ScalerFilterType::Pointer scaler = ScalerFilterType::New();
scaler->SetOutputMinimum( 0 );
scaler->SetOutputMaximum( 255 );
scaler->SetInput( voronoisegmenter->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The output of the rescaler is passed to the writer. The invocation
// of the \code{Update()} method on the writer triggers the execution of
// the pipeline.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
writer->SetInput( scaler->GetOutput() );
writer->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We execute this program on the image \code{BrainT1Slice.png} available
// in the directory \code{Examples/Data}. The following parameters are
// passed to the command line:
//
// \small
// \begin{verbatim}
//HybridSegmentationFuzzyVoronoi BrainT1Slice.png Output.png 140 125 140 25 0.2 2.0
// \end{verbatim}
// \normalsize
//
// $(140,125)$ specifies the index position of a seed point in the image,
// while $140$ and $25$ are the estimated mean and standard deviation,
// respectively, of the object to be segmented. Finally, $0.2$ and $2.0$
// are the tolerance for the mean and standard deviation, respectively.
// Figure~\ref{fig:HybridSegmentationFuzzyVoronoiOutput} shows the input
// image and the binary mask resulting from the segmentation.
//
// Note that in order to successfully segment other images, these
// parameters have to be adjusted to reflect the data. For example, when
// segmenting the input image \code{FatMRISlice.png} we apply the
// following new set of parameters parameters.
//
// \small
// \begin{verbatim}
//HybridSegmentationFuzzyVoronoi FatMRISlice.png Output.png 80 200 140 300 0.3 3.0
// \end{verbatim}
// \normalsize
//
// Figure~\ref{fig:HybridSegmentationFuzzyVoronoiOutput2} shows the input
// image and the binary mask resulting from this segmentation. Note that,
// we can segment color (RGB) and other multi-channel images using an
// approach similar to this example.
//
// Software Guide : EndLatex
return 0;
}
|