Skip to content

Commit afed4d5

Browse files
committed
ENH: Migrate example itkGrayscaleFunctionDilateImageFilter
Migrate the itkGrayscaleFunctionDilateImageFilter.cxx example from `ITK/Examples/Filtering` to `ITKSphinxExamples/src/Filtering/MathematicalMorphology/DilateAGrayscaleImage`. New baseline output image added as `OutputBaseline.png` as it didn't exist in ITK before.
1 parent 3bca12f commit afed4d5

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

src/Filtering/MathematicalMorphology/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ add_example(ErodeBinaryImageUsingFlatStruct)
3232
compare_to_baseline(EXAMPLE_NAME ErodeBinaryImageUsingFlatStruct
3333
BASELINE_PREFIX ErodeBinaryImageUsingFlatStruct
3434
)
35+
36+
add_example(DilateUsingFunctionalGrayscale)
37+
compare_to_baseline(EXAMPLE_NAME DilateUsingFunctionalGrayscale
38+
BASELINE_PREFIX OutputBaseline
39+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
cmake_minimum_required(VERSION 3.16.3)
2+
3+
project( DilateUsingFunctionalGrayscale )
4+
5+
find_package( ITK REQUIRED
6+
COMPONENTS
7+
ITKCommon
8+
ITKIOImageBase
9+
ITKMathematicalMorphology
10+
ITKIOPNG
11+
)
12+
include( ${ITK_USE_FILE} )
13+
14+
add_executable( DilateUsingFunctionalGrayscale Code.cxx )
15+
target_link_libraries( DilateUsingFunctionalGrayscale ${ITK_LIBRARIES} )
16+
17+
install( TARGETS DilateUsingFunctionalGrayscale
18+
DESTINATION bin/ITKSphinxExamples/Filtering/MathematicalMorphology
19+
COMPONENT Runtime
20+
)
21+
22+
install( FILES Code.cxx CMakeLists.txt
23+
DESTINATION share/ITKSphinxExamples/Code/Filtering/MathematicalMorphology/DilateUsingFunctionalGrayscale
24+
COMPONENT Code
25+
)
26+
27+
enable_testing()
28+
add_test( NAME DilateAGrayscaleImageTest
29+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/DilateUsingFunctionalGrayscale
30+
${CMAKE_CURRENT_BINARY_DIR}/cthead1.png
31+
Output.png
32+
5
33+
)
34+
35+
#if( ITK_WRAP_PYTHON )
36+
# add_test( NAME DilateAGrayscaleImageTestPython
37+
# COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
38+
# ${CMAKE_CURRENT_BINARY_DIR}/cthead1.png
39+
# OutputPython.png
40+
# 5
41+
# )
42+
#endif()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
#include "itkGrayscaleFunctionDilateImageFilter.h"
20+
#include "itkBinaryBallStructuringElement.h"
21+
#include "itkImageFileReader.h"
22+
#include "itkImageFileWriter.h"
23+
#include "itkImage.h"
24+
25+
int
26+
main(int argc, char * argv[])
27+
{
28+
if (argc < 3)
29+
{
30+
std::cerr << "Usage: " << std::endl;
31+
std::cerr << argv[0] << " inputImageFile outputImageFile" << std::endl;
32+
return EXIT_FAILURE;
33+
}
34+
35+
constexpr unsigned int Dimension = 2;
36+
using PixelType = unsigned char;
37+
38+
using ImageType = itk::Image<PixelType, Dimension>;
39+
40+
using ReaderType = itk::ImageFileReader<ImageType>;
41+
using WriterType = itk::ImageFileWriter<ImageType>;
42+
43+
auto reader = ReaderType::New();
44+
auto writer = WriterType::New();
45+
46+
reader->SetFileName(argv[1]);
47+
writer->SetFileName(argv[2]);
48+
49+
50+
using KernelType = itk::BinaryBallStructuringElement<PixelType, Dimension>;
51+
52+
using FilterType =
53+
itk::GrayscaleFunctionDilateImageFilter<ImageType, ImageType, KernelType>;
54+
55+
auto filter = FilterType::New();
56+
57+
KernelType ball;
58+
KernelType::SizeType ballSize;
59+
ballSize[0] = 1;
60+
ballSize[1] = 4;
61+
ball.SetRadius(ballSize);
62+
ball.CreateStructuringElement();
63+
64+
filter->SetKernel(ball);
65+
66+
filter->SetInput(reader->GetOutput());
67+
writer->SetInput(filter->GetOutput());
68+
69+
try
70+
{
71+
writer->Update();
72+
}
73+
catch (const itk::ExceptionObject & excp)
74+
{
75+
std::cerr << excp << std::endl;
76+
return EXIT_FAILURE;
77+
}
78+
79+
return EXIT_SUCCESS;
80+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
:name: DilateUsingFunctionalGrayscale
2+
3+
Dilate a grayscale image using a functional kernel
4+
========================
5+
6+
.. index::
7+
single: BinaryBallStructuringElement
8+
single: GrayscaleFunctionDilateImageFilter
9+
pair: mathematical morphology; dilation
10+
11+
.. seealso:: dilation; erosion
12+
13+
14+
Synopsis
15+
--------
16+
17+
Dilate an image using functional grayscale morphology.
18+
Function dilation takes the maximum of all the pixels identified by the structuring element plus the structuring element value.
19+
In this example, the white regions are enlarged.
20+
21+
22+
23+
Results
24+
-------
25+
26+
.. figure:: cthead1.png
27+
:scale: 50%
28+
:alt: Input ct head image.
29+
30+
Input grayscale image.
31+
32+
.. figure:: OutputBaseline.png
33+
:scale: 50%
34+
:alt: Dilated output.
35+
36+
Dilated output.
37+
38+
39+
Code
40+
----
41+
42+
C++
43+
...
44+
45+
.. literalinclude:: Code.cxx
46+
:lines: 18-
47+
48+
49+
Classes demonstrated
50+
--------------------
51+
52+
.. breathelink:: itk::GrayscaleFunctionDilateImageFilter itk::BinaryBallStructuringElement
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1b0df436d9ff4ef9baa8a3122e6066da1266add37a0192171481c950082d32c8a6c24dd2d11ad4ef1019ef4f9557d692223c9576052db94bcc7ed4f5c800e559
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
32d5c12cd02f537c901ee48ad2f438fcf5399f7292bf9c0abfb8da91b74fc2e0c4983e9650283db3462d22f05f05a8637a6c3b8188159e4bbd6dc68804a84870

0 commit comments

Comments
 (0)