MorphologyComparison
Repository source: MorphologyComparison
Description¶
Although preprocessing can do a lot to improve segmentation results, postprocessing can also be useful. Morphological filters, which operate on binary or discrete images, can be useful for manipulating the shape of the segmented regions. In this brief discussion we will only consider operations that use circular footprints, even though these morphological filters can be defined much more generally.
Erosion is implemented by removing pixels within a specified distance of a border. For each pixel not in the segmented region, all the neighbors in a circular region around the pixels are turned off. This erosion filter shrinks the segmented region and small isolated regions disappear.
The opposite of erosion is dilation. This filter grows the area of segmented regions. Small holes in the segmented region are completely closed. Any pixel not in the segmented region but near the region is turned on.
Dilation and erosion are dual filters with nearly identical implementations. Dilating the “on” pixels is equivalent to eroding “off” pixels in a binary image. Holes in the map disappear. However, dilation alone also grows the boundaries of the segmented regions. When dilation is followed by erosion in a closing operation, small holes are removed; however, the boundary of the segmented regions remain in the same general location.
Opening is the dual of closing. Opening removes small islands of pixels. It is implemented with an initial erosion, followed by a dilation.
Connectivity filters can also remove small regions without affecting the remaining boundaries of segmented regions. This set of filters separate the segmented pixels into equivalence classes based on a neighbor relation. Two pixels belong to the same class if they are touching. There are two common neighbor relations in two-dimensional images: four connectivity considers pixels neighbors if they are edge neighbors, and eight connectivity considers pixels neighbors if pixels share any vertex.
This example demonstrates various binary filters that can alter the shape of segmented regions. From left to right, top to bottom: original image, connectivity, erosion, dilation, opening, closing.
Info
See this figure in Chapter 10 the VTK Textbook.
Other languages
See (Python), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
MorphologyComparison.cxx
#include <vtkCamera.h>
#include <vtkDataArray.h>
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageDilateErode3D.h>
#include <vtkImageMapper3D.h>
#include <vtkImageProperty.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageSeedConnectivity.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vector>
int main(int argc, char* argv[])
{
// Verify input arguments.
if (argc != 2)
{
std::cout << "Usage: " << argv[0] << " Filename e.g. binary.pgm"
<< std::endl;
return EXIT_FAILURE;
}
// Read the image
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> reader;
reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
reader->SetFileName(argv[1]);
// Dilate
vtkNew<vtkImageDilateErode3D> dilate;
dilate->SetInputConnection(reader->GetOutputPort());
dilate->SetDilateValue(0);
dilate->SetErodeValue(255);
dilate->SetKernelSize(31, 31, 1);
// Erode
vtkNew<vtkImageDilateErode3D> erode;
erode->SetInputConnection(reader->GetOutputPort());
erode->SetDilateValue(255);
erode->SetErodeValue(0);
erode->SetKernelSize(31, 31, 1);
// Opening - dilate then erode
vtkNew<vtkImageDilateErode3D> dilate1;
dilate1->SetInputConnection(reader->GetOutputPort());
dilate1->SetDilateValue(0);
dilate1->SetErodeValue(255);
dilate1->SetKernelSize(31, 31, 1);
vtkNew<vtkImageDilateErode3D> erode1;
erode1->SetInputConnection(dilate1->GetOutputPort());
erode1->SetDilateValue(255);
erode1->SetErodeValue(0);
erode1->SetKernelSize(31, 31, 1);
// Closing - erode then dilate
vtkNew<vtkImageDilateErode3D> erode2;
erode2->SetInputConnection(reader->GetOutputPort());
erode2->SetDilateValue(255);
erode2->SetErodeValue(0);
erode2->SetKernelSize(31, 31, 1);
vtkNew<vtkImageDilateErode3D> dilate2;
dilate2->SetInputConnection(erode2->GetOutputPort());
dilate2->SetDilateValue(0);
dilate2->SetErodeValue(255);
dilate2->SetKernelSize(31, 31, 1);
// Connectivity
vtkNew<vtkImageSeedConnectivity> con;
con->SetInputConnection(reader->GetOutputPort());
con->AddSeed(300, 200);
con->SetInputConnectValue(0);
con->SetOutputConnectedValue(0);
con->SetOutputUnconnectedValue(255);
// Actors
vtkNew<vtkImageActor> originalActor;
originalActor->GetMapper()->SetInputConnection(reader->GetOutputPort());
originalActor->GetProperty()->SetInterpolationTypeToNearest();
vtkNew<vtkImageActor> connectedActor;
connectedActor->GetMapper()->SetInputConnection(con->GetOutputPort());
connectedActor->GetProperty()->SetInterpolationTypeToNearest();
vtkNew<vtkImageActor> erodeActor;
erodeActor->GetMapper()->SetInputConnection(erode->GetOutputPort());
erodeActor->GetProperty()->SetInterpolationTypeToNearest();
vtkNew<vtkImageActor> dilateActor;
dilateActor->GetMapper()->SetInputConnection(dilate->GetOutputPort());
dilateActor->GetProperty()->SetInterpolationTypeToNearest();
vtkNew<vtkImageActor> openingActor;
openingActor->GetMapper()->SetInputConnection(dilate2->GetOutputPort());
openingActor->GetProperty()->SetInterpolationTypeToNearest();
vtkNew<vtkImageActor> closingActor;
closingActor->GetMapper()->SetInputConnection(erode1->GetOutputPort());
closingActor->GetProperty()->SetInterpolationTypeToNearest();
// Setup renderers
vtkNew<vtkRenderer> originalRenderer;
originalRenderer->AddActor(originalActor);
vtkNew<vtkRenderer> connectedRenderer;
connectedRenderer->AddActor(connectedActor);
vtkNew<vtkRenderer> dilateRenderer;
dilateRenderer->AddActor(dilateActor);
vtkNew<vtkRenderer> erodeRenderer;
erodeRenderer->AddActor(erodeActor);
vtkNew<vtkRenderer> closingRenderer;
closingRenderer->AddActor(closingActor);
vtkNew<vtkRenderer> openingRenderer;
openingRenderer->AddActor(openingActor);
std::vector<vtkSmartPointer<vtkRenderer>> renderers;
renderers.push_back(originalRenderer);
renderers.push_back(connectedRenderer);
renderers.push_back(erodeRenderer);
renderers.push_back(dilateRenderer);
renderers.push_back(openingRenderer);
renderers.push_back(closingRenderer);
// Setup viewports for the renderers.
int rendererSize = 300;
unsigned int xGridDimensions = 2;
unsigned int yGridDimensions = 3;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(rendererSize * xGridDimensions,
rendererSize * yGridDimensions);
for (int row = 0; row < static_cast<int>(yGridDimensions); row++)
{
for (int col = 0; col < static_cast<int>(xGridDimensions); col++)
{
int index = row * xGridDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] = {
static_cast<double>(col) / xGridDimensions,
static_cast<double>(yGridDimensions - (row + 1)) / yGridDimensions,
static_cast<double>(col + 1) / xGridDimensions,
static_cast<double>(yGridDimensions - row) / yGridDimensions};
renderers[index]->SetViewport(viewport);
renderWindow->AddRenderer(renderers[index]);
}
}
renderWindow->SetWindowName("MorphologyComparison");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
// Renderers share one camera.
renderWindow->Render();
renderers[0]->GetActiveCamera()->Dolly(1.5);
renderers[0]->ResetCameraClippingRange();
for (size_t r = 1; r < renderers.size(); ++r)
{
renderers[r]->SetActiveCamera(renderers[0]->GetActiveCamera());
}
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MorphologyComparison)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
IOImage
ImagingMorphological
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "MorphologyComparison: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(MorphologyComparison MACOSX_BUNDLE MorphologyComparison.cxx )
target_link_libraries(MorphologyComparison PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS MorphologyComparison
MODULES ${VTK_LIBRARIES}
)
Download and Build MorphologyComparison¶
Click here to download MorphologyComparison and its CMakeLists.txt file. Once the tarball MorphologyComparison.tar has been downloaded and extracted,
cd MorphologyComparison/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./MorphologyComparison
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.