ITK/Examples/ImageProcessing/MaskNegatedImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Fix signed/unsigned comparison warning)
(Deprecated content that is moved to sphinx)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
You need the git master version of ITK from 6/30/2011 or newer for this functionality.
{{warning|1=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 releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.
 
}}
==MaskNegatedImageFilter.cxx==
<source lang="cpp">
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkMaskNegatedImageFilter.h"
 
typedef itk::Image<unsigned char, 2>  ImageType;
 
static void CreateHalfMask(ImageType::Pointer image, ImageType::Pointer mask);
static void CreateImage(ImageType::Pointer image);
 
int main(int, char *[])
{
  ImageType::Pointer image = ImageType::New();
  CreateImage(image);
 
  ImageType::Pointer mask = ImageType::New();
  CreateHalfMask(image, mask);
 
  typedef itk::MaskNegatedImageFilter< ImageType, ImageType > MaskNegatedImageFilterType;
  MaskNegatedImageFilterType::Pointer maskNegatedImageFilter = MaskNegatedImageFilterType::New();
  maskNegatedImageFilter->SetInput(image);
  maskNegatedImageFilter->SetMaskImage(mask);
  maskNegatedImageFilter->Update();;
 
  typedef itk::ImageFileWriter<ImageType> FileWriterType;
  FileWriterType::Pointer writer = FileWriterType::New();
  writer->SetFileName("output.png");
  writer->SetInput(maskNegatedImageFilter->GetOutput());
  writer->Update();
 
  return EXIT_SUCCESS;
}
 
 
void CreateHalfMask(ImageType::Pointer image, ImageType::Pointer mask)
{
  ImageType::RegionType region = image->GetLargestPossibleRegion();
 
  mask->SetRegions(region);
  mask->Allocate();
 
  ImageType::SizeType regionSize = region.GetSize();
 
  itk::ImageRegionIterator<ImageType> imageIterator(mask,region);
 
  // Make the left half of the mask white and the right half black
  while(!imageIterator.IsAtEnd())
  {
    if(static_cast<unsigned int>(imageIterator.GetIndex()[0]) > regionSize[0] / 2)
        {
        imageIterator.Set(0);
        }
      else
        {
        imageIterator.Set(1);
        }
 
    ++imageIterator;
   }
 
}
 
void CreateImage(ImageType::Pointer image)
{
  ImageType::IndexType start;
  start.Fill(0);
  ImageType::SizeType size;
  size.Fill(100);
  ImageType::RegionType region(start,size);
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(122);
}
 
</source>
 
 
{{ITKCMakeLists|MaskNegatedImageFilter|}}
</source>

Latest revision as of 19:47, 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.