Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Use itk::ReadImage and itk::WriteImage in src/Filtering examples #305

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ main(int argc, char * argv[])
const InputPixelType timeStep = std::stod(argv[4]);
const InputPixelType conductance = std::stod(argv[5]);

using ReaderType = itk::ImageFileReader<InputImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<InputImageType>(inputFileName);

using FilterType = itk::CurvatureAnisotropicDiffusionImageFilter<InputImageType, InputImageType>;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetNumberOfIterations(numberOfIterations);
filter->SetTimeStep(timeStep);
filter->SetConductanceParameter(conductance);
Expand All @@ -65,14 +63,9 @@ main(int argc, char * argv[])
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());

using WriterType = itk::ImageFileWriter<OutputImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(rescaler->GetOutput());

try
{
writer->Update();
itk::WriteImage(rescaler->GetOutput(), outputFileName);
}
catch (itk::ExceptionObject & error)
{
Expand Down
13 changes: 3 additions & 10 deletions src/Filtering/AnisotropicSmoothing/ComputeCurvatureFlow/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ main(int argc, char * argv[])
const int numberOfIterations = std::stoi(argv[3]);
const InputPixelType timeStep = std::stod(argv[4]);

using ReaderType = itk::ImageFileReader<InputImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<InputImageType>(inputFileName);

using FilterType = itk::CurvatureFlowImageFilter<InputImageType, InputImageType>;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetNumberOfIterations(numberOfIterations);
filter->SetTimeStep(timeStep);

Expand All @@ -63,14 +61,9 @@ main(int argc, char * argv[])
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());

using WriterType = itk::ImageFileWriter<OutputImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(rescaler->GetOutput());

try
{
writer->Update();
itk::WriteImage(rescaler->GetOutput(), outputFileName);
}
catch (itk::ExceptionObject & error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ main(int argc, char * argv[])
const InputPixelType timeStep = std::stod(argv[4]);
const InputPixelType conductance = std::stod(argv[5]);

using ReaderType = itk::ImageFileReader<InputImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<InputImageType>(inputFileName);

using FilterType = itk::GradientAnisotropicDiffusionImageFilter<InputImageType, InputImageType>;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetNumberOfIterations(numberOfIterations);
filter->SetTimeStep(timeStep);
filter->SetConductanceParameter(conductance);
Expand All @@ -65,14 +63,9 @@ main(int argc, char * argv[])
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());

using WriterType = itk::ImageFileWriter<OutputImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(rescaler->GetOutput());

try
{
writer->Update();
itk::WriteImage(rescaler->GetOutput(), outputFileName);
}
catch (itk::ExceptionObject & error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,20 @@ main(int argc, char * argv[])
using InputPixelType = unsigned char;
using InputImageType = itk::Image<InputPixelType, Dimension>;

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

using OutputPixelType = float;
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
using FilterType = itk::GradientAnisotropicDiffusionImageFilter<InputImageType, OutputImageType>;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetNumberOfIterations(std::stoi(argv[3]));
filter->SetTimeStep(0.125);
filter->SetConductanceParameter(std::stod(argv[4]));

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

try
{
writer->Update();
itk::WriteImage(filter->GetOutput(), argv[2]);
}
catch (itk::ExceptionObject & error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ main(int argc, char * argv[])
using RGBImageType = itk::Image<itk::RGBPixel<float>, 2>;

// 1) Read the RGB image
using ReaderType = itk::ImageFileReader<RGBImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<RGBImageType>(inputFileName);

// 2) Cast to Vector image for processing
using AdaptorInputType = itk::RGBToVectorImageAdaptor<RGBImageType>;
AdaptorInputType::Pointer adaptInput = AdaptorInputType::New();
adaptInput->SetImage(reader->GetOutput());
adaptInput->SetImage(input);
using CastInputType = itk::CastImageFilter<AdaptorInputType, FloatImageType>;
CastInputType::Pointer castInput = CastInputType::New();
castInput->SetInput(adaptInput);
Expand Down Expand Up @@ -90,7 +88,7 @@ main(int argc, char * argv[])
// 5) Display the input and smoothed images
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddRGBImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(inputFileName));
viewer.AddRGBImage(input, true, itksys::SystemTools::GetFilenameName(inputFileName));

std::stringstream desc;
desc << "VectorGradientAnisotropicDiffusionImageFilter\niterations: " << filter->GetNumberOfIterations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ main(int argc, char * argv[])
using RGBImageType = itk::Image<itk::RGBPixel<float>, 2>;

// 1) Read the RGB image
using ReaderType = itk::ImageFileReader<RGBImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<RGBImageType>(inputFileName);

// 2) Cast to Vector image for processing
using AdaptorInputType = itk::RGBToVectorImageAdaptor<RGBImageType>;
AdaptorInputType::Pointer adaptInput = AdaptorInputType::New();
adaptInput->SetImage(reader->GetOutput());
adaptInput->SetImage(input);
using CastInputType = itk::CastImageFilter<AdaptorInputType, FloatImageType>;
CastInputType::Pointer castInput = CastInputType::New();
castInput->SetInput(adaptInput);
Expand Down Expand Up @@ -89,7 +87,7 @@ main(int argc, char * argv[])
// 5) Display the input and smoothed images
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddRGBImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(inputFileName));
viewer.AddRGBImage(input, true, itksys::SystemTools::GetFilenameName(inputFileName));

std::stringstream desc;
desc << "VectorCurvatureAnisotropicDiffusionImageFilter\niterations: " << filter->GetNumberOfIterations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,21 @@ main(int argc, char * argv[])
}

constexpr unsigned int Dimension = 2;

using PixelType = float;

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

using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
const auto input = itk::ReadImage<ImageType>(inputFileName);

using FilterType = itk::AntiAliasBinaryImageFilter<ImageType, ImageType>;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
filter->SetInput(input);
filter->SetMaximumRMSError(maximumRMSError);
filter->SetNumberOfIterations(numberOfIterations);
filter->SetNumberOfLayers(numberOfLayers);

using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(filter->GetOutput());
try
{
writer->Update();
itk::WriteImage(filter->GetOutput(), outputFileName);
}
catch (itk::ExceptionObject & error)
{
Expand Down
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]);
}

unsigned int radius = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ main(int argc, char * argv[])

using PixelType = unsigned char;
constexpr unsigned int Dimension = 2;

using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputImage);

const auto input = itk::ReadImage<ImageType>(inputImage);

using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
Expand All @@ -52,18 +50,13 @@ main(int argc, char * argv[])
using BinaryDilateImageFilterType = itk::BinaryDilateImageFilter<ImageType, ImageType, StructuringElementType>;

BinaryDilateImageFilterType::Pointer dilateFilter = BinaryDilateImageFilterType::New();
dilateFilter->SetInput(reader->GetOutput());
dilateFilter->SetInput(input);
dilateFilter->SetKernel(structuringElement);
dilateFilter->SetForegroundValue(255); // Value to dilate

using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetInput(dilateFilter->GetOutput());
writer->SetFileName(outputImage);

try
{
writer->Update();
itk::WriteImage(dilateFilter->GetOutput(), outputImage);
}
catch (itk::ExceptionObject & error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ main(int argc, char * argv[])

using PixelType = unsigned char;
constexpr unsigned int Dimension = 2;

using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputImage);

const auto input = itk::ReadImage<ImageType>(inputImage);

using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
Expand All @@ -52,19 +50,14 @@ main(int argc, char * argv[])
using BinaryErodeImageFilterType = itk::BinaryErodeImageFilter<ImageType, ImageType, StructuringElementType>;

BinaryErodeImageFilterType::Pointer erodeFilter = BinaryErodeImageFilterType::New();
erodeFilter->SetInput(reader->GetOutput());
erodeFilter->SetInput(input);
erodeFilter->SetKernel(structuringElement);
erodeFilter->SetForegroundValue(255); // Intensity value to erode
erodeFilter->SetBackgroundValue(0); // Replacement value for eroded voxels

using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetInput(erodeFilter->GetOutput());
writer->SetFileName(outputImage);

try
{
writer->Update();
itk::WriteImage(erodeFilter->GetOutput(), outputImage);
}
catch (itk::ExceptionObject & e)
{
Expand Down
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]);
}

unsigned int radius = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "itkBinaryPruningImageFilter.h"
#include "itkImageFileReader.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkImageFileWriter.h"

#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
Expand Down Expand Up @@ -47,10 +46,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]);
std::stringstream ssIteration(argv[2]);
}

Expand Down
23 changes: 3 additions & 20 deletions src/Filtering/BinaryMathematicalMorphology/ThinImage/Code.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

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

static void
WriteImage(const ImageType::Pointer image, const std::string & fileName);
static void
CreateImage(ImageType::Pointer image);

Expand All @@ -34,17 +32,12 @@ main(int argc, char * argv[])
ImageType::Pointer image = ImageType::New();
if (argc == 2)
{
using ImageReader = itk::ImageFileReader<ImageType>;
ImageReader::Pointer reader = ImageReader::New();
std::string fileName = argv[1];
reader->SetFileName(fileName);
reader->Update();
image = reader->GetOutput();
image = itk::ReadImage<ImageType>(argv[1]);
}
else
{
CreateImage(image);
WriteImage(image, "input.png");
itk::WriteImage(image, "input.png");
}

using BinaryThinningImageFilterType = itk::BinaryThinningImageFilter<ImageType, ImageType>;
Expand All @@ -60,7 +53,7 @@ main(int argc, char * argv[])
rescaler->SetOutputMaximum(255);
rescaler->Update();

WriteImage(rescaler->GetOutput(), "output.png");
itk::WriteImage(rescaler->GetOutput(), "output.png");

return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -93,13 +86,3 @@ CreateImage(ImageType::Pointer image)
}
}
}

void
WriteImage(const ImageType::Pointer image, const std::string & fileName)
{
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(fileName);
writer->SetInput(image);
writer->Update();
}
Loading