From 52bde6a9ebc44e8be7b929659a89437ca44367d8 Mon Sep 17 00:00:00 2001 From: Sadhana Ravikumar Date: Thu, 23 Jan 2025 11:23:09 -0500 Subject: [PATCH] STYLE: Use itk::ReadImage and itk::WriteImage in Examples/Statistics --- Examples/Statistics/BayesianClassifier.cxx | 16 ++----- .../BayesianClassifierInitializer.cxx | 16 ++----- Examples/Statistics/ImageEntropy1.cxx | 10 +--- Examples/Statistics/ImageHistogram1.cxx | 47 ++++++------------- Examples/Statistics/ImageHistogram2.cxx | 11 +---- Examples/Statistics/ImageHistogram3.cxx | 11 +---- Examples/Statistics/ImageHistogram4.cxx | 11 +---- .../Statistics/ImageMutualInformation1.cxx | 17 +++---- .../ScalarImageKmeansClassifier.cxx | 41 +++------------- .../ScalarImageKmeansModelEstimator.cxx | 10 +--- .../ScalarImageMarkovRandomField1.cxx | 16 +++---- 11 files changed, 50 insertions(+), 156 deletions(-) diff --git a/Examples/Statistics/BayesianClassifier.cxx b/Examples/Statistics/BayesianClassifier.cxx index cc64de6c9e2..4142be8674c 100644 --- a/Examples/Statistics/BayesianClassifier.cxx +++ b/Examples/Statistics/BayesianClassifier.cxx @@ -75,10 +75,7 @@ main(int argc, char * argv[]) constexpr unsigned int Dimension = 2; using InputPixelType = float; using InputImageType = itk::VectorImage; - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - reader->SetFileName(membershipImageFileName); + const auto input = itk::ReadImage(membershipImageFileName); using LabelType = unsigned char; using PriorType = float; @@ -94,7 +91,7 @@ main(int argc, char * argv[]) auto filter = ClassifierFilterType::New(); - filter->SetInput(reader->GetOutput()); + filter->SetInput(input); if (argv[3]) { @@ -130,19 +127,12 @@ main(int argc, char * argv[]) rescaler->SetOutputMinimum(0); rescaler->SetOutputMaximum(255); - using WriterType = itk::ImageFileWriter; - - 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) { diff --git a/Examples/Statistics/BayesianClassifierInitializer.cxx b/Examples/Statistics/BayesianClassifierInitializer.cxx index 043f3cf5ac4..62149ac59f8 100644 --- a/Examples/Statistics/BayesianClassifierInitializer.cxx +++ b/Examples/Statistics/BayesianClassifierInitializer.cxx @@ -85,13 +85,9 @@ main(int argc, char * argv[]) itk::BayesianClassifierInitializationImageFilter; auto bayesianInitializer = BayesianInitializerType::New(); - using ReaderType = itk::ImageFileReader; - auto reader = ReaderType::New(); - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -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; - auto writer = WriterType::New(); - writer->SetInput(bayesianInitializer->GetOutput()); - writer->SetFileName(argv[2]); - try { bayesianInitializer->Update(); @@ -124,7 +114,7 @@ main(int argc, char * argv[]) try { - writer->Update(); + itk::WriteImage(bayesianInitializer->GetOutput(), argv[2]) } catch (const itk::ExceptionObject & excp) { diff --git a/Examples/Statistics/ImageEntropy1.cxx b/Examples/Statistics/ImageEntropy1.cxx index 4ac41ea6556..09f7c375f74 100644 --- a/Examples/Statistics/ImageEntropy1.cxx +++ b/Examples/Statistics/ImageEntropy1.cxx @@ -84,15 +84,9 @@ main(int argc, char * argv[]) using ImageType = itk::Image; // Software Guide : EndCodeSnippet - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -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 diff --git a/Examples/Statistics/ImageHistogram1.cxx b/Examples/Statistics/ImageHistogram1.cxx index 88360357a56..5e2e42f2863 100644 --- a/Examples/Statistics/ImageHistogram1.cxx +++ b/Examples/Statistics/ImageHistogram1.cxx @@ -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; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); + try + { + const auto input = itk::ReadImage(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 @@ -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} // @@ -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 diff --git a/Examples/Statistics/ImageHistogram2.cxx b/Examples/Statistics/ImageHistogram2.cxx index 5cede9e4611..a56f77ffb15 100644 --- a/Examples/Statistics/ImageHistogram2.cxx +++ b/Examples/Statistics/ImageHistogram2.cxx @@ -74,16 +74,9 @@ main(int argc, char * argv[]) using ImageType = itk::Image; // Software Guide : EndCodeSnippet - - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -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 diff --git a/Examples/Statistics/ImageHistogram3.cxx b/Examples/Statistics/ImageHistogram3.cxx index 970d8c016d6..a1db2133145 100644 --- a/Examples/Statistics/ImageHistogram3.cxx +++ b/Examples/Statistics/ImageHistogram3.cxx @@ -80,16 +80,9 @@ main(int argc, char * argv[]) using RGBImageType = itk::Image; // Software Guide : EndCodeSnippet - - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -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 diff --git a/Examples/Statistics/ImageHistogram4.cxx b/Examples/Statistics/ImageHistogram4.cxx index 08391a5a18b..a904696893a 100644 --- a/Examples/Statistics/ImageHistogram4.cxx +++ b/Examples/Statistics/ImageHistogram4.cxx @@ -89,16 +89,9 @@ main(int argc, char * argv[]) using RGBImageType = itk::Image; // Software Guide : EndCodeSnippet - - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -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 diff --git a/Examples/Statistics/ImageMutualInformation1.cxx b/Examples/Statistics/ImageMutualInformation1.cxx index 55518fdc3b1..48b61a000b6 100644 --- a/Examples/Statistics/ImageMutualInformation1.cxx +++ b/Examples/Statistics/ImageMutualInformation1.cxx @@ -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; - - auto reader1 = ReaderType::New(); - auto reader2 = ReaderType::New(); - - reader1->SetFileName(argv[1]); - reader2->SetFileName(argv[2]); + const auto input1 = itk::ReadImage(argv[1]); + const auto input2 = itk::ReadImage(argv[2]); // Software Guide : EndCodeSnippet @@ -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 diff --git a/Examples/Statistics/ScalarImageKmeansClassifier.cxx b/Examples/Statistics/ScalarImageKmeansClassifier.cxx index 188344c7068..e40a495959d 100644 --- a/Examples/Statistics/ScalarImageKmeansClassifier.cxx +++ b/Examples/Statistics/ScalarImageKmeansClassifier.cxx @@ -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 @@ -72,10 +70,7 @@ main(int argc, char * argv[]) constexpr unsigned int Dimension = 2; using ImageType = itk::Image; - - using ReaderType = itk::ImageFileReader; - auto reader = ReaderType::New(); - reader->SetFileName(inputImageFileName); + const auto input = itk::ReadImage(inputImageFileName); // Software Guide : EndCodeSnippet @@ -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 @@ -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; - - 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) { diff --git a/Examples/Statistics/ScalarImageKmeansModelEstimator.cxx b/Examples/Statistics/ScalarImageKmeansModelEstimator.cxx index 61a7c2f1a50..5801ba6f588 100644 --- a/Examples/Statistics/ScalarImageKmeansModelEstimator.cxx +++ b/Examples/Statistics/ScalarImageKmeansModelEstimator.cxx @@ -62,15 +62,9 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - - auto reader = ReaderType::New(); - - reader->SetFileName(argv[1]); - try { - reader->Update(); + const auto input = itk::ReadImage(argv[1]); } catch (const itk::ExceptionObject & excp) { @@ -88,7 +82,7 @@ main(int argc, char * argv[]) auto adaptor = AdaptorType::New(); - adaptor->SetImage(reader->GetOutput()); + adaptor->SetImage(input); // Create the K-d tree structure using TreeGeneratorType = diff --git a/Examples/Statistics/ScalarImageMarkovRandomField1.cxx b/Examples/Statistics/ScalarImageMarkovRandomField1.cxx index 3e529767a0e..2ac835f2c5c 100644 --- a/Examples/Statistics/ScalarImageMarkovRandomField1.cxx +++ b/Examples/Statistics/ScalarImageMarkovRandomField1.cxx @@ -105,9 +105,8 @@ 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. In this particular case we choose to use + // to classify and read the input image.In this particular case we choose to + // use // \code{short} as pixel type, which is typical for MicroMRI and CT // data sets. // @@ -119,9 +118,7 @@ main(int argc, char * argv[]) using ImageType = itk::Image; - using ReaderType = itk::ImageFileReader; - auto reader = ReaderType::New(); - reader->SetFileName(inputImageFileName); + const auto input = itk::ReadImage(inputImageFileName); // Software Guide : EndCodeSnippet @@ -139,9 +136,8 @@ main(int argc, char * argv[]) using LabelImageType = itk::Image; - using LabelReaderType = itk::ImageFileReader; - auto labelReader = LabelReaderType::New(); - labelReader->SetFileName(inputLabelImageFileName); + const auto labelInput = + itk::ReadImage(inputLabelImageFileName); // Software Guide : EndCodeSnippet @@ -166,7 +162,7 @@ main(int argc, char * argv[]) itk::ComposeImageFilter; auto scalarToArrayFilter = ScalarToArrayFilterType::New(); - scalarToArrayFilter->SetInput(reader->GetOutput()); + scalarToArrayFilter->SetInput(input); // Software Guide : EndCodeSnippet