Skip to content

Commit

Permalink
STYLE: Use itk::ReadImage and itk::WriteImage in src/Core examples
Browse files Browse the repository at this point in the history
  • Loading branch information
josetascon committed Jul 7, 2021
1 parent fe75959 commit a63668e
Show file tree
Hide file tree
Showing 46 changed files with 65 additions and 289 deletions.
16 changes: 4 additions & 12 deletions src/Core/Common/AddNoiseToBinaryImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ main(int argc, char * argv[])
}
}
using ImageType = itk::Image<unsigned char, 2>;
using ReaderType = itk::ImageFileReader<ImageType>;
using IteratorType = itk::ImageRandomNonRepeatingIteratorWithIndex<ImageType>;
using WriterType = itk::ImageFileWriter<ImageType>;

// Read the binary file
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
const auto input = itk::ReadImage<ImageType>(argv[1]);

// At x% of the pixels, add a uniform random value between 0 and 255
IteratorType it(reader->GetOutput(), reader->GetOutput()->GetLargestPossibleRegion());
it.SetNumberOfSamples(reader->GetOutput()->GetLargestPossibleRegion().GetNumberOfPixels() * percent);
IteratorType it(input, input->GetLargestPossibleRegion());
it.SetNumberOfSamples(input->GetLargestPossibleRegion().GetNumberOfPixels() * percent);
std::cout << "Number of random samples: " << it.GetNumberOfSamples() << std::endl;
using GeneratorType = itk::Statistics::MersenneTwisterRandomVariateGenerator;
GeneratorType::Pointer random = GeneratorType::New();
Expand All @@ -62,11 +58,7 @@ main(int argc, char * argv[])
++it;
}

// Write the file
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[2]);
writer->SetInput(reader->GetOutput());
writer->Update();
itk::WriteImage(input, argv[2]);

return EXIT_SUCCESS;
}
8 changes: 1 addition & 7 deletions src/Core/Common/ConvertArrayToImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ main(int, char *[])
const bool importImageFilterWillOwnTheBuffer = true;
importFilter->SetImportPointer(localBuffer, numberOfPixels, importImageFilterWillOwnTheBuffer);

using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();

writer->SetFileName("test.png");

writer->SetInput(importFilter->GetOutput());
writer->Update();
itk::WriteImage(importFilter->GetOutput(), "test.mhd");

return EXIT_SUCCESS;
}
1 change: 0 additions & 1 deletion src/Core/Common/CropImageBySpecifyingRegion/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// This is different from CropImageFilter only in the way
// that the region to crop is specified.
#include "itkImage.h"
#include <itkImageFileReader.h>
#include <itkExtractImageFilter.h>

int
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Common/DisplayImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ main(int argc, char * argv[])
}
else
{
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
image = reader->GetOutput();
image = itk::ReadImage<ImageType>(argv[1]);
}

using RescaleFilterType = itk::RescaleIntensityImageFilter<ImageType, ImageType>;
Expand Down
2 changes: 0 additions & 2 deletions src/Core/Common/FilterImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "ImageFilter.h"

Expand Down
2 changes: 0 additions & 2 deletions src/Core/Common/FilterImageWithoutCopying/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "MyInPlaceImageFilter.h"

Expand Down
11 changes: 3 additions & 8 deletions src/Core/Common/FindMaxAndMinInImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,19 @@ main(int argc, char * argv[])

return EXIT_FAILURE;
}
std::string inputFilename = argv[1];

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();

reader->SetFileName(inputFilename.c_str());
reader->Update();
const auto input = itk::ReadImage<ImageType>(argv[1]);

using ImageCalculatorFilterType = itk::MinimumMaximumImageCalculator<ImageType>;

ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New();
imageCalculatorFilter->SetImage(reader->GetOutput());
imageCalculatorFilter->SetImage(input);
imageCalculatorFilter->Compute();

using ConnectorType = itk::ImageToVTKImageFilter<ImageType>;
ConnectorType::Pointer originalConnector = ConnectorType::New();

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

vtkSmartPointer<vtkImageActor> originalActor = vtkSmartPointer<vtkImageActor>::New();
#if VTK_MAJOR_VERSION <= 5
Expand Down
7 changes: 1 addition & 6 deletions src/Core/Common/GetImageSize/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ main(int argc, char * argv[])
using PixelType = unsigned char;

using ImageType = itk::Image<PixelType, 2>;
using ReaderType = itk::ImageFileReader<ImageType>;

ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::RegionType region = image->GetLargestPossibleRegion();

Expand Down
2 changes: 0 additions & 2 deletions src/Core/Common/GetOrSetMemberVariableOfITKClass/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "ImageFilterX.h"

Expand Down
8 changes: 1 addition & 7 deletions src/Core/Common/ImageRegionOverlap/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,9 @@ main(int argc, char * argv[])
++itC;
}

// Writer
using FileWriterType = itk::ImageFileWriter<RGBImageType>;
FileWriterType::Pointer writer = FileWriterType::New();
writer->SetFileName(out_file_name);
writer->SetInput(image);

try
{
writer->Update();
itk::WriteImage(image, out_file_name);
}
catch (itk::ExceptionObject & error)
{
Expand Down
10 changes: 1 addition & 9 deletions src/Core/Common/InPlaceFilterOfImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ main(int argc, char * argv[])
return EXIT_FAILURE;
}

std::string inputFilename = argv[1];

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();

reader->SetFileName(inputFilename.c_str());
reader->Update();

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ApplyThresholding(image);

Expand Down
2 changes: 0 additions & 2 deletions src/Core/Common/IterateImageStartingAtSeed/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkFloodFilledImageFunctionConditionalIterator.h"
#include "itkBinaryThresholdImageFunction.h"
#include "itkImageFileWriter.h"

using ImageType = itk::Image<unsigned char, 2>;

Expand Down
6 changes: 1 addition & 5 deletions src/Core/Common/IterateLineThroughImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ main(int argc, char * argv[])
}
else
{
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
image = reader->GetOutput();
image = itk::ReadImage<ImageType>(argv[1]);
inputFilename = argv[1];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRegionIterator.h"
#include "itkLineConstIterator.h"

Expand Down
1 change: 0 additions & 1 deletion src/Core/Common/IterateOverSpecificRegion/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRegionExclusionConstIteratorWithIndex.h"

namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 5;
Expand Down
7 changes: 1 addition & 6 deletions src/Core/Common/IterateRegionWithNeighborhood/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 50;
Expand Down
7 changes: 1 addition & 6 deletions src/Core/Common/IterateRegionWithWriteAccess/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 5;
Expand Down
7 changes: 1 addition & 6 deletions src/Core/Common/IterateRegionWithoutWriteAccess/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ main(int argc, char * argv[])

using ImageType = itk::Image<unsigned char, 2>;

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

ImageType::Pointer image = reader->GetOutput();
ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

ImageType::SizeType regionSize;
regionSize[0] = 50;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkConstantBoundaryCondition.h"
#include "itkImageRegionIterator.h"
Expand Down
10 changes: 4 additions & 6 deletions src/Core/Common/MultiThreadOilPainting/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ main(int argc, char * argv[])
using ImageType = itk::Image<unsigned char, 2>;
using FilterType = itk::OilPaintingImageFilter<ImageType>;

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
std::string inputFileName = argv[1];
const auto input = itk::ReadImage<ImageType>(inputFileName);

FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetNumberOfBins(numberOfBins);
filter->SetRadius(radius);
filter->Update();

#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(reader->GetFileName()));
viewer.AddImage(input, true, itksys::SystemTools::GetFilenameName(inputFileName));

std::stringstream desc;
desc << "OilPaintingImageFilter, bins = " << numberOfBins << " radius = " << radius;
Expand Down
11 changes: 2 additions & 9 deletions src/Core/Common/MultipleInputsOfDifferentType/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,14 @@ main(int, char *[])
using ScalarImageType = itk::Image<unsigned char, 2>;
using FilterType = itk::ImageFilterMultipleInputsDifferentType<VectorImageType, ScalarImageType>;

using ReaderType = itk::ImageFileReader<VectorImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("Test.jpg");
reader->Update();
const auto input = itk::ReadImage<VectorImageType>("Test.jpg");

// Create and the filter
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->Update();

using WriterType = itk::ImageFileWriter<VectorImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("TestOutput.jpg");
writer->SetInput(filter->GetOutput());
writer->Update();
itk::WriteImage(filter->GetOutput(), "TestOutput.jpg");

return EXIT_SUCCESS;
}
2 changes: 0 additions & 2 deletions src/Core/Common/MultipleInputsOfSameType/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*
*=========================================================================*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkImageFilterMultipleInputs.h"

Expand Down
16 changes: 2 additions & 14 deletions src/Core/Common/MultipleOutputsOfDifferentType/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,9 @@ main(int, char *[])
FilterType::Pointer filter = FilterType::New();
filter->Update();

{
using WriterType = itk::ImageFileWriter<OutputImageType1>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("TestOutput1.jpg");
writer->SetInput(filter->GetOutput1());
writer->Update();
}
itk::WriteImage(filter->GetOutput1(), "TestOutput1.jpg");

{
using WriterType = itk::ImageFileWriter<OutputImageType2>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("TestOutput2.jpg");
writer->SetInput(filter->GetOutput2());
writer->Update();
}
itk::WriteImage(filter->GetOutput2(), "TestOutput2.jpg");

return EXIT_SUCCESS;
}
Loading

0 comments on commit a63668e

Please sign in to comment.