ITK/Examples/Images/NeighborhoodOperatorImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Deprecated content that is moved to sphinx)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==NeighborhoodOperatorImageFilter.cxx==
{{warning|1=The media wiki content on this page is no longer maintainedThe examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.
<source lang="cpp">
}}
#include "itkImage.h"
#include "itkNeighborhoodOperatorImageFilter.h"
#include "itkSobelOperator.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
 
typedef itk::Image<float, 2> FloatImageType;
 
void CreateImage(FloatImageType::Pointer image);
void CastRescaleAndWrite(FloatImageType::Pointer image, const std::string& filename);
 
int main(int, char*[])
{
  FloatImageType::Pointer image = FloatImageType::New();
  CreateImage(image);
  CastRescaleAndWrite(image, "input.png");
 
  typedef itk::SobelOperator<float, 2> SobelOperatorType;
  SobelOperatorType sobelOperator;
  itk::Size<2> radius;
  radius.Fill(1); // a radius of 1x1 creates a 3x3 operator
  sobelOperator.SetDirection(0); // Create the operator for the X axis derivative
  sobelOperator.CreateToRadius(radius);
 
  typedef itk::NeighborhoodOperatorImageFilter<FloatImageType, FloatImageType> NeighborhoodOperatorImageFilterType;
  NeighborhoodOperatorImageFilterType::Pointer filter = NeighborhoodOperatorImageFilterType::New();
  filter->SetOperator(sobelOperator);
  filter->SetInput(image);
  filter->Update();
 
  CastRescaleAndWrite(filter->GetOutput(), "output.png");
 
   return EXIT_SUCCESS;
}
 
void CreateImage(FloatImageType::Pointer image)
{
  FloatImageType::IndexType start;
  start.Fill(0);
 
  FloatImageType::SizeType size;
  size.Fill(100);
 
  FloatImageType::RegionType region(start,size);
 
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(0);
 
  // Make a square
  for(unsigned int r = 20; r < 80; r++)
    {
    for(unsigned int c = 20; c < 80; c++)
      {
      FloatImageType::IndexType pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;
 
      image->SetPixel(pixelIndex, 15);
      }
    }
}
 
void CastRescaleAndWrite(FloatImageType::Pointer image, const std::string& filename)
{
  typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
  typedef itk::RescaleIntensityImageFilter<FloatImageType, UnsignedCharImageType> RescaleFilterType;
  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
  rescaleFilter->SetInput(image);
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);
  rescaleFilter->Update();
 
  typedef  itk::ImageFileWriter<UnsignedCharImageType> WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(filename);
  writer->SetInput(rescaleFilter->GetOutput());
  writer->Update();
 
}
</source>
 
{{ITKVTKCMakeLists|NeighborhoodOperatorImageFilter|}}

Latest revision as of 16:18, 5 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.