Skip to content

ENH: Migrate example itkGrayscaleFunctionDilateImageFilter #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Filtering/MathematicalMorphology/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ add_example(ErodeBinaryImageUsingFlatStruct)
compare_to_baseline(EXAMPLE_NAME ErodeBinaryImageUsingFlatStruct
BASELINE_PREFIX ErodeBinaryImageUsingFlatStruct
)

add_example(DilateUsingFunctionalGrayscale)
compare_to_baseline(EXAMPLE_NAME DilateUsingFunctionalGrayscale
BASELINE_PREFIX OutputBaseline
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.16.3)

project( DilateUsingFunctionalGrayscale )

find_package( ITK REQUIRED
COMPONENTS
ITKCommon
ITKIOImageBase
ITKMathematicalMorphology
ITKIOPNG
)
include( ${ITK_USE_FILE} )

add_executable( DilateUsingFunctionalGrayscale Code.cxx )
target_link_libraries( DilateUsingFunctionalGrayscale ${ITK_LIBRARIES} )

install( TARGETS DilateUsingFunctionalGrayscale
DESTINATION bin/ITKSphinxExamples/Filtering/MathematicalMorphology
COMPONENT Runtime
)

install( FILES Code.cxx CMakeLists.txt
DESTINATION share/ITKSphinxExamples/Code/Filtering/MathematicalMorphology/DilateUsingFunctionalGrayscale
COMPONENT Code
)

enable_testing()
add_test( NAME DilateUsingFunctionalGrayscaleTest
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/DilateUsingFunctionalGrayscale
${CMAKE_CURRENT_BINARY_DIR}/cthead1.png
Output.png
5
)

if( ITK_WRAP_PYTHON )
add_test( NAME DilateUsingFunctionalGrayscaleTestPython
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
${CMAKE_CURRENT_BINARY_DIR}/cthead1.png
OutputPython.png
5
)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#include "itkFlatStructuringElement.h"
#include "itkGrayscaleFunctionDilateImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

int
main(int argc, char * argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " <inputImage> <outputImage> <radius>";
std::cerr << std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;

using ImageType = itk::Image<PixelType, Dimension>;

using ReaderType = itk::ImageFileReader<ImageType>;
using WriterType = itk::ImageFileWriter<ImageType>;

auto reader = ReaderType::New();
auto writer = WriterType::New();

const char * inputImage = argv[1];
const char * outputImage = argv[2];
const unsigned int radiusValue = std::stoi(argv[3]);

reader->SetFileName(inputImage);
writer->SetFileName(outputImage);

using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);

using FilterType = itk::GrayscaleFunctionDilateImageFilter<ImageType, ImageType, StructuringElementType>;

auto filter = FilterType::New();

filter->SetKernel(structuringElement);

filter->SetInput(reader->GetOutput());
writer->SetInput(filter->GetOutput());

try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python

# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import itk
import argparse

itk.auto_progress(2)

parser = argparse.ArgumentParser(
description="Dilate an image using functional grayscale morphology."
)
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("radius", type=int)
args = parser.parse_args()

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)

StructuringElementType = itk.FlatStructuringElement[Dimension]
structuringElement = StructuringElementType.Ball(args.radius)

grayscaleFilter = itk.GrayscaleFunctionDilateImageFilter[
ImageType, ImageType, structuringElement
].New()
grayscaleFilter.SetInput(reader.GetOutput())
grayscaleFilter.SetKernel(structuringElement)

writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(grayscaleFilter.GetOutput())

writer.Update()
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
:name: DilateUsingFunctionalGrayscale

Dilate a grayscale image using a functional kernel
===================================================

.. index::
single: BinaryBallStructuringElement
single: GrayscaleFunctionDilateImageFilter
pair: mathematical morphology; dilation

.. seealso:: dilation; erosion


Synopsis
--------

Dilate an image using functional grayscale morphology.
Function dilation takes the maximum of all the pixels identified by the structuring element plus the structuring element value.
In this example, the white regions are enlarged.



Results
-------

.. figure:: cthead1.png
:scale: 50%
:alt: Input ct head image.

Input grayscale image.

.. figure:: OutputBaseline.png
:scale: 50%
:alt: Dilated output.

Dilated output.


Code
----

Python
......

.. literalinclude:: Code.py
:language: python
:lines: 1, 16-


C++
...

.. literalinclude:: Code.cxx
:lines: 18-


Classes demonstrated
--------------------

.. breathelink:: itk::GrayscaleFunctionDilateImageFilter itk::BinaryBallStructuringElement
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bafkreiaqbnt5phaiaiohrwjfis2bwmqinj5eu4jgnumpfsmviy27nsduoe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bafkreieilrgrns7xcna5yle2yw3q2wxkl3wn7lp22k4d4pwjhlhwc3ed4u
1 change: 1 addition & 0 deletions src/Filtering/MathematicalMorphology/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ MathematicalMorphology

CreateABinaryBallStructuringElement/Documentation.rst
DilateAGrayscaleImage/Documentation.rst
DilateUsingFunctionalGrayscale/Documentation.rst
ErodeAGrayscaleImage/Documentation.rst
ErodeBinaryImageUsingFlatStruct/Documentation.rst
GenerateStructureElementsWithAccurateArea/Documentation.rst
Expand Down
Loading