ITK/Examples/ImageProcessing/ManuallyRemovingLabels

From KitwarePublic
< ITK‎ | Examples
Revision as of 16:09, 24 December 2012 by Lorensen (talk | contribs)
Jump to navigationJump to search

ManuallyRemovingLabels.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileWriter.h"
  3. include "itkImageRegionIterator.h"
  4. include "itkBinaryImageToLabelMapFilter.h"

typedef itk::Image<unsigned char, 2> ImageType; static void CreateImage(ImageType::Pointer image);

int main(int, char *[]) {

 ImageType::Pointer image = ImageType::New();
 CreateImage(image);

 typedef itk::BinaryImageToLabelMapFilter<ImageType> BinaryImageToLabelMapFilterType;
 BinaryImageToLabelMapFilterType::Pointer binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
 binaryImageToLabelMapFilter->SetInput(image);
 binaryImageToLabelMapFilter->Update();

 // The output of this filter is an itk::LabelMap, which contains itk::LabelObject's
 std::cout << "There are " << binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() << " objects." << std::endl;
 std::vector<unsigned long> labelsToRemove;
 
 std::cout << "There are originally " << binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() << " objects." << std::endl;
 
 // Note: do NOT remove the labels inside the loop! The IDs are stored such that they will change when one is deleted.
 // Instead, mark all of the labels to be removed first and then remove them all together.
 for(unsigned int i = 0; i < binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects(); i++)
   {
   // Get the ith region
   BinaryImageToLabelMapFilterType::OutputImageType::LabelObjectType* labelObject = binaryImageToLabelMapFilter->GetOutput()->GetNthLabelObject(i);
   //std::cout << "Region " << i << " has " << labelObject->Size() << " pixels." << std::endl;
 
   // Mark every other label to be removed
   if(i%2 == 0)
     {
     labelsToRemove.push_back(labelObject->GetLabel());
     }
   }
   
 std::cout << "Removing " << labelsToRemove.size() << " objects." << std::endl;
 // Remove all regions that were marked for removal.
 for(unsigned int i = 0; i < labelsToRemove.size(); ++i)
   {
   binaryImageToLabelMapFilter->GetOutput()->RemoveLabel(labelsToRemove[i]);
   }
 std::cout << "There are " << binaryImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() 
           << " objects remaining." << std::endl;
 

 return EXIT_SUCCESS;

}

void CreateImage(ImageType::Pointer image) {

 // Create a black image with a white square
 ImageType::IndexType start;
 start.Fill(0);

 ImageType::SizeType size;
 size.Fill(20);

 ImageType::RegionType region(start,size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 // Make line of non-touching diagonal pixels
 for(unsigned int i = 0; i < 20; i+=2)
   {
   itk::Index<2> pixel;
   pixel[0] = i;
   pixel[1] = i;
   image->SetPixel(pixel, 255);
   }

}

</source>

CMakeLists.txt

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

project(ManuallyRemovingLabels)

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(ManuallyRemovingLabels MACOSX_BUNDLE ManuallyRemovingLabels.cxx) target_link_libraries(ManuallyRemovingLabels

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

</syntaxhighlight>

Download and Build ManuallyRemovingLabels

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

cd ManuallyRemovingLabels/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:

./ManuallyRemovingLabels

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.