Skip to content

Commit

Permalink
ENH: Implement ITK's WriteImage functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mseng10 committed Mar 11, 2021
1 parent ab38077 commit de347bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
16 changes: 2 additions & 14 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 @@ -44,7 +42,7 @@ main(int argc, char * argv[])
else
{
CreateImage(image);
WriteImage(image, "input.png");
itk::WriteImage(image, "input.png");
}

using BinaryThinningImageFilterType = itk::BinaryThinningImageFilter<ImageType, ImageType>;
Expand All @@ -60,7 +58,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 +91,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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,22 @@ CreateImage(ImageType * const image);
void
CreateImageOfSquare(ImageType * const image, const itk::Index<2> & cornerOfSquare);

template <typename TImage>
void
WriteImage(const TImage * const image, const std::string & filename);

int
main(int, char *[])
{
// Setup mask
MaskType::Pointer mask = MaskType::New();
CreateMask(mask);

WriteImage(mask.GetPointer(), "mask.png");
itk::WriteImage(mask.GetPointer(), "mask.png");

// Setup image1
ImageType::Pointer image1 = ImageType::New();
itk::Index<2> cornerOfSquare1;
cornerOfSquare1[0] = 3;
cornerOfSquare1[1] = 8;
CreateImageOfSquare(image1, cornerOfSquare1);
WriteImage(image1.GetPointer(), "image1.png");
itk::WriteImage(image1.GetPointer(), "image1.png");

// Setup image2
itk::Index<2> offset;
Expand All @@ -72,7 +68,7 @@ main(int, char *[])
cornerOfSquare2[1] = cornerOfSquare1[1] + offset[1];
CreateImageOfSquare(image2, cornerOfSquare2);

WriteImage(image2.GetPointer(), "image2.png");
itk::WriteImage(image2.GetPointer(), "image2.png");

// Create a kernel from an image
itk::ImageKernelOperator<unsigned char> kernelOperator;
Expand Down Expand Up @@ -101,15 +97,15 @@ main(int, char *[])
correlationFilter->SetTemplate(kernelOperator);
correlationFilter->Update();

WriteImage(correlationFilter->GetOutput(), "correlation.mha");
itk::WriteImage(correlationFilter->GetOutput(), "correlation.mha");

using RescaleFilterType = itk::RescaleIntensityImageFilter<FloatImageType, ImageType>;
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetInput(correlationFilter->GetOutput());
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
rescaleFilter->Update();
WriteImage(rescaleFilter->GetOutput(), "correlation.png");
itk::WriteImage(rescaleFilter->GetOutput(), "correlation.png");

using MinimumMaximumImageCalculatorType = itk::MinimumMaximumImageCalculator<FloatImageType>;
MinimumMaximumImageCalculatorType::Pointer minimumMaximumImageCalculatorFilter =
Expand Down Expand Up @@ -210,14 +206,3 @@ CreateImageOfSquare(ImageType * const image, const itk::Index<2> & cornerOfSquar
++imageIterator;
}
}

template <typename TImage>
void
WriteImage(const TImage * const image, const std::string & filename)
{
using WriterType = itk::ImageFileWriter<TImage>;
typename WriterType::Pointer writer = WriterType::New();
writer->SetFileName(filename);
writer->SetInput(image);
writer->Update();
}

0 comments on commit de347bf

Please sign in to comment.