Apply Cos Image Filter#

Synopsis#

Compute the cosine of each pixel

Results#

Input image

Rescaled Input image [0, 255]#

Output image

Rescaled Output image [0, 255]#

Code#

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkCosImageFilter.h"

int
main(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName>";
    std::cerr << " <OutputFileName>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }
  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];

  constexpr unsigned int Dimension = 2;

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

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

  using FilterType = itk::CosImageFilter<ImageType, ImageType>;
  auto filter = FilterType::New();
  filter->SetInput(input);

  try
  {
    itk::WriteImage(filter->GetOutput(), outputFileName);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class CosImageFilter : public itk::UnaryGeneratorImageFilter<TInputImage, TOutputImage>

Computes the cosine of each pixel.

This filter is templated over the pixel type of the input image and the pixel type of the output image.

The filter walks over all of the pixels in the input image, and for each pixel does the following:

  • cast the pixel value to double,

  • apply the std::cos() function to the double value,

  • cast the double value resulting from std::cos() to the pixel type of the output image,

  • store the cast value into the output image.

The filter expects both images to have the same dimension (e.g. both 2D, or both 3D, or both ND)

See itk::CosImageFilter for additional documentation.