ITK/Examples/ImageProcessing/MaskImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(ITK4.4)
Line 18: Line 18:
#include "itkImageFileReader.h"
#include "itkImageFileReader.h"
#include "itkMaskImageFilter.h"
#include "itkMaskImageFilter.h"
 
#include "itkImageRegionIterator.h"
#include "QuickView.h"
#include "QuickView.h"


Line 99: Line 99:
}
}
#endif
#endif
</source>
</source>




{{ITKVTKCMakeLists|{{SUBPAGENAME}}}}
{{ITKVTKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 14:52, 31 May 2013

ITK Examples Baseline ImageProcessing TestMaskImageFilter.png

This example shows how to mask an image with a binary mask. Pixels in the original image where the mask is non zero will remain the same. Pixels in the original image where the mask is equal to zero will be set to the "outside" value.

The output shows the input image, the mask, and the masked output image.

MaskImageFilter.cxx

<source lang="cpp">

  1. include "itkConfigure.h"
  1. if ( ITK_VERSION_MAJOR < 4 ) //These are all defaults in ITKv4

// Not supported in ITKv3. int main(int argc, char *argv[]) {

 return 0;

}

  1. else
  2. include "itkImage.h"
  3. include "itkImageFileReader.h"
  4. include "itkMaskImageFilter.h"
  5. include "itkImageRegionIterator.h"
  6. include "QuickView.h"

typedef itk::Image<unsigned char, 2> ImageType;

void CreateHalfMask(ImageType::Pointer image, ImageType::Pointer &mask);

int main(int argc, char *argv[]) {

 if(argc < 2)
   {
   std::cerr << "Usage: " << argv[0] << " filename" << std::endl;
   return EXIT_FAILURE;
   }
 typedef itk::ImageFileReader<ImageType> ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName(argv[1]);
 reader->Update();
 ImageType::Pointer mask = ImageType::New();
 CreateHalfMask(reader->GetOutput(), mask);
 
 typedef itk::MaskImageFilter< ImageType, ImageType > MaskFilterType;
 MaskFilterType::Pointer maskFilter = MaskFilterType::New();
 maskFilter->SetInput(reader->GetOutput());
 maskFilter->SetMaskImage(mask);
 mask->Print(std::cout);
 QuickView viewer;
 viewer.AddImage(
   reader->GetOutput(),true,
   itksys::SystemTools::GetFilenameName(argv[1]));  
 std::stringstream desc;
 desc << "Mask";
 viewer.AddImage(
   mask.GetPointer(),
   true,
   desc.str());  
 std::stringstream desc2;
 desc2 << "MaskFilter";
 viewer.AddImage(
   maskFilter->GetOutput(),
   true,
   desc2.str());  
 viewer.Visualize();
 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(imageIterator.GetIndex()[0] > static_cast<ImageType::IndexValueType>(regionSize[0]) / 2)
       {
       imageIterator.Set(0);
       }
     else
       {
       imageIterator.Set(255);
       }
   ++imageIterator;
 }

}

  1. endif

</source>


CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(MaskImageFilter)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(MaskImageFilter MACOSX_BUNDLE MaskImageFilter.cxx) target_link_libraries(MaskImageFilter

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build MaskImageFilter

Click here to download MaskImageFilter. and its CMakeLists.txt file. Once the tarball MaskImageFilter.tar has been downloaded and extracted,

cd MaskImageFilter/build 
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project,

make

and run it:

./MaskImageFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.