ITK/Examples/Morphology/BinaryMorphologicalOpeningImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Deprecated content that is moved to sphinx)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
<div class="floatcenter">[[File:ITK_Examples_Baseline_Morphology_TestBinaryMorphologicalOpeningImageFilter.png]]</div>
{{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.}}
This doesn't seem to actually change the image?
 
==BinaryMorphologicalOpeningImageFilter.cxx==
<source lang="cpp">
#include "itkImage.h"
#include "itkBinaryMorphologicalOpeningImageFilter.h"
#include "itkImageFileReader.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkImageFileWriter.h"
 
#include "QuickView.h"
 
typedef itk::Image<unsigned char, 2>  ImageType;
 
static void CreateImage(ImageType* const image);
 
int main(int argc, char *argv[])
{
  ImageType::Pointer image;
 
  if(argc == 1)
    {
//    std::cerr << "Usage: " << std::endl;
//    std::cerr << argv[0] << " InputImageFile OutputImageFile [radius]" << std::endl;
    image = ImageType::New();
    CreateImage(image);
    }
  else
  {
    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();
    image = reader->GetOutput();
  }
 
  unsigned int radius = 5;
  if (argc == 3)
    {
    std::stringstream ss(argv[2]);
    ss >> radius;
    }
 
  typedef itk::BinaryBallStructuringElement<ImageType::PixelType, ImageType::ImageDimension>
              StructuringElementType;
  StructuringElementType structuringElement;
  structuringElement.SetRadius(radius);
  structuringElement.CreateStructuringElement();
 
  typedef itk::BinaryMorphologicalOpeningImageFilter <ImageType, ImageType, StructuringElementType>
          BinaryMorphologicalOpeningImageFilterType;
  BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter
          = BinaryMorphologicalOpeningImageFilterType::New();
  openingFilter->SetInput(image);
  openingFilter->SetKernel(structuringElement);
  openingFilter->Update();
 
//   typedef itk::ImageFileWriter< ImageType > WriterType;
//   WriterType::Pointer writer = WriterType::New();
//  writer->SetInput( dilateFilter->GetOutput() );
//  writer->SetFileName( argv[2] );
//  writer->Update();
 
  QuickView viewer;
  viewer.AddImage(image.GetPointer());
  viewer.AddImage(openingFilter->GetOutput());
   viewer.Visualize();
 
  return EXIT_SUCCESS;
}
 
 
void CreateImage(ImageType* const image)
{
  // Create an image with 2 connected components
  itk::Index<2> corner = {{0,0}};
 
  itk::Size<2> size;
  unsigned int NumRows = 200;
  unsigned int NumCols = 300;
  size[0] = NumRows;
  size[1] = NumCols;
 
  itk::ImageRegion<2> region(corner, size);
 
  image->SetRegions(region);
  image->Allocate();
 
  // Make a square
  for(unsigned int r = 40; r < 100; r++)
    {
    for(unsigned int c = 40; c < 100; c++)
      {
      itk::Index<2> pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;
 
      image->SetPixel(pixelIndex, 50);
      }
    }
}
</source>
 
{{ITKCMakeLists|BinaryMorphologicalOpeningImageFilter}}

Latest revision as of 18:44, 6 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.