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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates the use of the \doxygen{RGBGibbsPriorFilter}.
// The filter outputs a binary segmentation that can be improved by the
// deformable model. It is the first part of our hybrid framework.
//
// First, we include the appropriate header file.
//
// Software Guide : EndLatex
#include <iostream>
#include <string>
#include <cmath>
// Software Guide : BeginCodeSnippet
#include "itkRGBGibbsPriorFilter.h"
// Software Guide : EndCodeSnippet
// classes help the Gibbs filter to segment the image
#include "itkImageClassifierBase.h"
#include "itkImageGaussianModelEstimator.h"
#include "itkMahalanobisDistanceMembershipFunction.h"
#include "itkMinimumDecisionRule.h"
// image storage and I/O classes
#include "itkSize.h"
#include "itkImage.h"
#include "itkVector.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#define NUM_CLASSES 3
#define MAX_NUM_ITER 1
int
main(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " inputImage trainimage outputImage" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Gibbs Prior Test Begins: " << std::endl;
// Software Guide : BeginLatex
//
// The input is a single channel 2D image; the channel number is
// \code{NUMBANDS} = 1, and \code{NDIMENSION} is set to 3.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
constexpr unsigned short NUMBANDS = 1;
constexpr unsigned short NDIMENSION = 3;
using VecImageType =
itk::Image<itk::Vector<unsigned short, NUMBANDS>, NDIMENSION>;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The Gibbs prior segmentation is performed first to generate a rough
// segmentation that yields a sample of tissue from a region to be
// segmented, which will be combined to form the input for the
// isocontouring method. We define the pixel type of the output of the
// Gibbs prior filter to be \code{unsigned short}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using ClassImageType = itk::Image<unsigned short, NDIMENSION>;
// Software Guide : EndCodeSnippet
// We instantiate reader and writer types
//
using ReaderType = itk::ImageFileReader<ClassImageType>;
using WriterType = itk::ImageFileWriter<ClassImageType>;
auto inputimagereader = ReaderType::New();
auto trainingimagereader = ReaderType::New();
auto writer = WriterType::New();
inputimagereader->SetFileName(argv[1]);
trainingimagereader->SetFileName(argv[2]);
writer->SetFileName(argv[3]);
// We convert the input into vector images
//
auto vecImage = VecImageType::New();
using VecImagePixelType = VecImageType::PixelType;
VecImageType::SizeType vecImgSize = { { 181, 217, 1 } };
VecImageType::IndexType index;
index.Fill(0);
VecImageType::RegionType region;
region.SetSize(vecImgSize);
region.SetIndex(index);
vecImage->SetLargestPossibleRegion(region);
vecImage->SetBufferedRegion(region);
vecImage->Allocate();
enum
{
VecImageDimension = VecImageType::ImageDimension
};
using VecIterator = itk::ImageRegionIterator<VecImageType>;
VecIterator vecIt(vecImage, vecImage->GetBufferedRegion());
vecIt.GoToBegin();
inputimagereader->Update();
trainingimagereader->Update();
using ClassIterator = itk::ImageRegionIterator<ClassImageType>;
ClassIterator inputIt(inputimagereader->GetOutput(),
inputimagereader->GetOutput()->GetBufferedRegion());
inputIt.GoToBegin();
// Set up the vector to store the image data
using DataVector = VecImageType::PixelType;
DataVector dblVec;
while (!vecIt.IsAtEnd())
{
dblVec[0] = inputIt.Get();
vecIt.Set(dblVec);
++vecIt;
++inputIt;
}
//----------------------------------------------------------------------
// Set membership function (Using the statistics objects)
//----------------------------------------------------------------------
namespace stat = itk::Statistics;
using VecImagePixelType = VecImageType::PixelType;
using MembershipFunctionType =
stat::MahalanobisDistanceMembershipFunction<VecImagePixelType>;
using MembershipFunctionPointer = MembershipFunctionType::Pointer;
using MembershipFunctionPointerVector =
std::vector<MembershipFunctionPointer>;
//----------------------------------------------------------------------
// Set the image model estimator (train the class models)
//----------------------------------------------------------------------
using ImageGaussianModelEstimatorType =
itk::ImageGaussianModelEstimator<VecImageType,
MembershipFunctionType,
ClassImageType>;
auto applyEstimateModel = ImageGaussianModelEstimatorType::New();
applyEstimateModel->SetNumberOfModels(NUM_CLASSES);
applyEstimateModel->SetInputImage(vecImage);
applyEstimateModel->SetTrainingImage(trainingimagereader->GetOutput());
// Run the gaussian classifier algorithm
applyEstimateModel->Update();
std::cout << " site 1 " << std::endl;
applyEstimateModel->Print(std::cout);
MembershipFunctionPointerVector membershipFunctions =
applyEstimateModel->GetMembershipFunctions();
std::cout << " site 2 " << std::endl;
//----------------------------------------------------------------------
// Set the decision rule
//----------------------------------------------------------------------
using DecisionRuleBasePointer = itk::Statistics::DecisionRule::Pointer;
using DecisionRuleType = itk::Statistics::MinimumDecisionRule;
auto myDecisionRule = DecisionRuleType::New();
std::cout << " site 3 " << std::endl;
//----------------------------------------------------------------------
// Set the classifier to be used and assigned the parameters for the
// supervised classifier algorithm except the input image which is
// grabbed from the Gibbs application pipeline.
//----------------------------------------------------------------------
//---------------------------------------------------------------------
// Software Guide : BeginLatex
//
// Then we define the classifier that is needed
// for the Gibbs prior model to make correct segmenting decisions.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using ClassifierType =
itk::ImageClassifierBase<VecImageType, ClassImageType>;
using ClassifierPointer = ClassifierType::Pointer;
ClassifierPointer myClassifier = ClassifierType::New();
// Software Guide : EndCodeSnippet
// Set the Classifier parameters
myClassifier->SetNumberOfClasses(NUM_CLASSES);
// Set the decision rule
myClassifier->SetDecisionRule((DecisionRuleBasePointer)myDecisionRule);
// Add the membership functions
for (unsigned int i = 0; i < NUM_CLASSES; ++i)
{
myClassifier->AddMembershipFunction(membershipFunctions[i]);
}
// Set the Gibbs Prior labeler
// Software Guide : BeginLatex
//
// After that we can define the multi-channel Gibbs prior model.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using GibbsPriorFilterType =
itk::RGBGibbsPriorFilter<VecImageType, ClassImageType>;
auto applyGibbsImageFilter = GibbsPriorFilterType::New();
// Software Guide : EndCodeSnippet
// Set the MRF labeler parameters
// Software Guide : BeginLatex
//
// The parameters for the Gibbs prior filter are defined
// below. \code{NumberOfClasses} indicates how many different objects are
// in the image. The maximum number of iterations is the number of
// minimization steps. \code{ClusterSize} sets the lower limit on the
// object's size. The boundary gradient is the estimate of the variance
// between objects and background at the boundary region.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->SetNumberOfClasses(NUM_CLASSES);
applyGibbsImageFilter->SetMaximumNumberOfIterations(MAX_NUM_ITER);
applyGibbsImageFilter->SetClusterSize(10);
applyGibbsImageFilter->SetBoundaryGradient(6);
applyGibbsImageFilter->SetObjectLabel(1);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We now set the input classifier for the Gibbs prior filter and the
// input to the classifier. The classifier will calculate the mean and
// variance of the object using the class image, and the results will be
// used as parameters for the Gibbs prior model.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->SetInput(vecImage);
applyGibbsImageFilter->SetClassifier(myClassifier);
applyGibbsImageFilter->SetTrainingImage(trainingimagereader->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we execute the Gibbs prior filter using the Update() method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->Update();
// Software Guide : EndCodeSnippet
std::cout << "applyGibbsImageFilter: " << applyGibbsImageFilter;
writer->SetInput(applyGibbsImageFilter->GetOutput());
writer->Update();
// Software Guide : BeginLatex
//
// We execute this program on the image \code{brainweb89.png}. The
// following parameters are passed to the command line:
//
// \small
// \begin{verbatim}
// GibbsGuide.exe brainweb89.png brainweb89_train.png brainweb_gp.png
// \end{verbatim}
// \normalsize
//
// \code{brainweb89train} is a training image that helps to estimate the
// object statistics.
//
// Note that in order to successfully segment other images, one has to
// create suitable training images for them. We can also segment color
// (RGB) and other multi-channel images.
//
// Software Guide : EndLatex
return EXIT_SUCCESS;
}
|