Skip to content

Commit

Permalink
STYLE: Use itk::ReadImage and itk::WriteImage in Examples/Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
sadhana-r committed Jan 23, 2025
1 parent ec75692 commit 52bde6a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 156 deletions.
16 changes: 3 additions & 13 deletions Examples/Statistics/BayesianClassifier.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ main(int argc, char * argv[])
constexpr unsigned int Dimension = 2;
using InputPixelType = float;
using InputImageType = itk::VectorImage<InputPixelType, Dimension>;
using ReaderType = itk::ImageFileReader<InputImageType>;

auto reader = ReaderType::New();
reader->SetFileName(membershipImageFileName);
const auto input = itk::ReadImage<InputImageType>(membershipImageFileName);

using LabelType = unsigned char;
using PriorType = float;
Expand All @@ -94,7 +91,7 @@ main(int argc, char * argv[])
auto filter = ClassifierFilterType::New();


filter->SetInput(reader->GetOutput());
filter->SetInput(input);

if (argv[3])
{
Expand Down Expand Up @@ -130,19 +127,12 @@ main(int argc, char * argv[])
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);

using WriterType = itk::ImageFileWriter<OutputImageType>;

auto writer = WriterType::New();
writer->SetFileName(labelMapImageFileName);

//
// Write labelmap to file
//
writer->SetInput(rescaler->GetOutput());

try
{
writer->Update();
itk::WriteImage(rescaler->GetOutput(), labelMapImageFileName)
}
catch (const itk::ExceptionObject & excp)
{
Expand Down
16 changes: 3 additions & 13 deletions Examples/Statistics/BayesianClassifierInitializer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ main(int argc, char * argv[])
itk::BayesianClassifierInitializationImageFilter<ImageType>;
auto bayesianInitializer = BayesianInitializerType::New();

using ReaderType = itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(argv[1]);

try
{
reader->Update();
const auto input = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
Expand All @@ -100,17 +96,11 @@ main(int argc, char * argv[])
return EXIT_FAILURE;
}

bayesianInitializer->SetInput(reader->GetOutput());
bayesianInitializer->SetInput(input);
bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));

// TODO add test where we specify membership functions

using WriterType =
itk::ImageFileWriter<BayesianInitializerType::OutputImageType>;
auto writer = WriterType::New();
writer->SetInput(bayesianInitializer->GetOutput());
writer->SetFileName(argv[2]);

try
{
bayesianInitializer->Update();
Expand All @@ -124,7 +114,7 @@ main(int argc, char * argv[])

try
{
writer->Update();
itk::WriteImage(bayesianInitializer->GetOutput(), argv[2])
}
catch (const itk::ExceptionObject & excp)
{
Expand Down
10 changes: 2 additions & 8 deletions Examples/Statistics/ImageEntropy1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,9 @@ main(int argc, char * argv[])
using ImageType = itk::Image<PixelType, Dimension>;
// Software Guide : EndCodeSnippet

using ReaderType = itk::ImageFileReader<ImageType>;

auto reader = ReaderType::New();

reader->SetFileName(argv[1]);

try
{
reader->Update();
const auto input = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
Expand Down Expand Up @@ -144,7 +138,7 @@ main(int argc, char * argv[])
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
histogramGenerator->SetInput(reader->GetOutput());
histogramGenerator->SetInput(input);

histogramGenerator->Compute();
// Software Guide : EndCodeSnippet
Expand Down
47 changes: 15 additions & 32 deletions Examples/Statistics/ImageHistogram1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,24 @@ main(int argc, char * argv[])

// Software Guide : BeginLatex
//
// Using the same image type we instantiate the type of the image reader
// Using the same image type we read the image file
// that will provide the image source for our example.
// As usual, this must be done inside a try/catch block because the read
// operation can potentially throw exceptions.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
using ReaderType = itk::ImageFileReader<ImageType>;

auto reader = ReaderType::New();

reader->SetFileName(argv[1]);
try
{
const auto input = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
Expand All @@ -104,7 +111,7 @@ main(int argc, char * argv[])
// samples. We instantiate the type of the adaptor by using the actual image
// type. Then construct the adaptor by invoking its \code{New()} method and
// assigning the result to the corresponding smart pointer. Finally we
// connect the output of the image reader to the input of the adaptor.
// specify the input to the adaptor.
//
// \index{itk::Statistics::Scalar\-Image\-To\-List\-Adaptor!instantiation}
//
Expand All @@ -115,31 +122,7 @@ main(int argc, char * argv[])

auto adaptor = AdaptorType::New();

adaptor->SetImage(reader->GetOutput());
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
//
// You must keep in mind that adaptors are not pipeline objects. This means
// that they do not propagate update calls. It is therefore your
// responsibility to make sure that you invoke the \code{Update()} method of
// the reader before you attempt to use the output of the adaptor. As usual,
// this must be done inside a try/catch block because the read operation can
// potentially throw exceptions.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
try
{
reader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
adaptor->SetImage(input);
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
Expand Down
11 changes: 2 additions & 9 deletions Examples/Statistics/ImageHistogram2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,9 @@ main(int argc, char * argv[])
using ImageType = itk::Image<PixelType, Dimension>;
// Software Guide : EndCodeSnippet


using ReaderType = itk::ImageFileReader<ImageType>;

auto reader = ReaderType::New();

reader->SetFileName(argv[1]);

try
{
reader->Update();
const auto input = itk::ReadImage<ImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
Expand Down Expand Up @@ -119,7 +112,7 @@ main(int argc, char * argv[])
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
histogramGenerator->SetInput(reader->GetOutput());
histogramGenerator->SetInput(input);
// Software Guide : EndCodeSnippet


Expand Down
11 changes: 2 additions & 9 deletions Examples/Statistics/ImageHistogram3.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,9 @@ main(int argc, char * argv[])
using RGBImageType = itk::Image<RGBPixelType, Dimension>;
// Software Guide : EndCodeSnippet


using ReaderType = itk::ImageFileReader<RGBImageType>;

auto reader = ReaderType::New();

reader->SetFileName(argv[1]);

try
{
reader->Update();
const auto input = itk::ReadImage<RGBImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
Expand Down Expand Up @@ -192,7 +185,7 @@ main(int argc, char * argv[])
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
histogramFilter->SetInput(reader->GetOutput());
histogramFilter->SetInput(input);

histogramFilter->Update();
// Software Guide : EndCodeSnippet
Expand Down
11 changes: 2 additions & 9 deletions Examples/Statistics/ImageHistogram4.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,9 @@ main(int argc, char * argv[])
using RGBImageType = itk::Image<RGBPixelType, Dimension>;
// Software Guide : EndCodeSnippet


using ReaderType = itk::ImageFileReader<RGBImageType>;

auto reader = ReaderType::New();

reader->SetFileName(argv[1]);

try
{
reader->Update();
const auto input = itk::ReadImage<RGBImageType>(argv[1]);
}
catch (const itk::ExceptionObject & excp)
{
Expand Down Expand Up @@ -187,7 +180,7 @@ main(int argc, char * argv[])
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
histogramFilter->SetInput(reader->GetOutput());
histogramFilter->SetInput(input);
// Software Guide : EndCodeSnippet


Expand Down
17 changes: 6 additions & 11 deletions Examples/Statistics/ImageMutualInformation1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,14 @@ main(int argc, char * argv[])

// Software Guide : BeginLatex
//
// Using the image type we proceed to instantiate the readers for both input
// images. Then, we take their filenames from the command line arguments.
// Using the image type we proceed to read in the input images
// taking their filenames from the command line arguments.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
using ReaderType = itk::ImageFileReader<ImageType>;

auto reader1 = ReaderType::New();
auto reader2 = ReaderType::New();

reader1->SetFileName(argv[1]);
reader2->SetFileName(argv[2]);
const auto input1 = itk::ReadImage<ImageType>(argv[1]);
const auto input2 = itk::ReadImage<ImageType>(argv[2]);
// Software Guide : EndCodeSnippet


Expand All @@ -142,8 +137,8 @@ main(int argc, char * argv[])

auto joinFilter = JoinFilterType::New();

joinFilter->SetInput1(reader1->GetOutput());
joinFilter->SetInput2(reader2->GetOutput());
joinFilter->SetInput1(input1);
joinFilter->SetInput2(input2);
// Software Guide : EndCodeSnippet


Expand Down
41 changes: 7 additions & 34 deletions Examples/Statistics/ScalarImageKmeansClassifier.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ main(int argc, char * argv[])
// Software Guide : BeginLatex
//
// First we define the pixel type and dimension of the image that we intend
// to classify. With this image type we can also declare the
// \doxygen{ImageFileReader} needed for reading the input image, create one
// and set its input filename.
// to classify. With this image type we can also read the input image.
//
// Software Guide : EndLatex

Expand All @@ -72,10 +70,7 @@ main(int argc, char * argv[])
constexpr unsigned int Dimension = 2;

using ImageType = itk::Image<PixelType, Dimension>;

using ReaderType = itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(inputImageFileName);
const auto input = itk::ReadImage<ImageType>(inputImageFileName);
// Software Guide : EndCodeSnippet


Expand All @@ -92,7 +87,7 @@ main(int argc, char * argv[])

auto kmeansFilter = KMeansFilterType::New();

kmeansFilter->SetInput(reader->GetOutput());
kmeansFilter->SetInput(input);

const unsigned int numberOfInitialClasses = std::stoi(argv[4]);
// Software Guide : EndCodeSnippet
Expand Down Expand Up @@ -162,39 +157,17 @@ main(int argc, char * argv[])
// The \doxygen{ScalarImageKmeansImageFilter} is predefined for producing an
// 8 bits scalar image as output. This output image contains labels
// associated to each one of the classes in the K-Means algorithm. In the
// following lines we use the \code{OutputImageType} in order to instantiate
// the type of a \doxygen{ImageFileWriter}. Then create one, and connect it
// to the output of the classification filter.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
using OutputImageType = KMeansFilterType::OutputImageType;

using WriterType = itk::ImageFileWriter<OutputImageType>;

auto writer = WriterType::New();

writer->SetInput(kmeansFilter->GetOutput());

writer->SetFileName(outputImageFileName);
// Software Guide : EndCodeSnippet


// Software Guide : BeginLatex
//
// We are now ready for triggering the execution of the pipeline. This is
// done by simply invoking the \code{Update()} method in the writer. This
// call will propagate the update request to the reader and then to the
// classifier.
// following lines we use the \code{OutputImageType}to write the output of
// the classification filter to file.
//
// Software Guide : EndLatex


// Software Guide : BeginCodeSnippet
using OutputImageType = KMeansFilterType::OutputImageType;
try
{
writer->Update();
itk::WriteImage(kmeansFilter->GetOutput(), outputImageFileName)
}
catch (const itk::ExceptionObject & excp)
{
Expand Down
Loading

0 comments on commit 52bde6a

Please sign in to comment.