Skip to content

Commit be1ec47

Browse files
Ben Wilsonhjmjohnson
authored andcommitted
Add AdditiveGaussianNoiseImageFilter Example
1 parent 4733798 commit be1ec47

File tree

8 files changed

+193
-1
lines changed

8 files changed

+193
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
3+
project( AdditiveGaussianNoiseImageFilter )
4+
5+
find_package( ITK REQUIRED )
6+
include( ${ITK_USE_FILE} )
7+
8+
add_executable( AdditiveGaussianNoiseImageFilter Code.cxx )
9+
target_link_libraries( AdditiveGaussianNoiseImageFilter ${ITK_LIBRARIES} )
10+
11+
install( TARGETS AdditiveGaussianNoiseImageFilter
12+
DESTINATION bin/ITKExamples/Filtering/ImageFeature
13+
COMPONENT Runtime
14+
)
15+
16+
install( FILES Code.cxx CMakeLists.txt
17+
DESTINATION share/ITKExamples/Code/Filtering/ImageFeature/AdditiveGaussianNoiseImageFilter
18+
COMPONENT Code
19+
)
20+
21+
enable_testing()
22+
add_test( NAME AdditiveGaussianNoiseImageFilterTest
23+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AdditiveGaussianNoiseImageFilter
24+
${CMAKE_CURRENT_BINARY_DIR}/Gourds8.png
25+
Output.mha
26+
)
27+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*=========================================================================
2+
*
3+
* Copyright Insight Software Consortium
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+
* http://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+
#include "itkImage.h"
19+
#include "itkImageFileReader.h"
20+
#include "itkImageFileWriter.h"
21+
#include "itkAdditiveGaussianNoiseImageFilter.h"
22+
23+
int main( int argc, char* argv[] )
24+
{
25+
// Check for proper arguments, if not, explain usage.
26+
if( argc != 5 )
27+
{
28+
std::cerr << "Usage: "<< std::endl;
29+
std::cerr << argv[0];
30+
std::cerr << " <InputFileName> <OutputFileName> [Mean] [Standard Deviation]";
31+
std::cerr << std::endl;
32+
return EXIT_FAILURE;
33+
}
34+
// Initialize and assign user provided variables
35+
const std::string * inputImage = argv[1];
36+
const std::string * outputImage = argv[2];
37+
// get floating point numbers for the Mean and Standard Deviation to perform the algorithm
38+
const double mean = std::stod(argv[3]);
39+
const double deviation = std::stod(argv[4]);
40+
41+
constexpr unsigned int Dimension = 2;
42+
// Use unsigned char so file will save to .png
43+
using PixelType = unsigned char;
44+
using ImageType = itk::Image< PixelType, Dimension >;
45+
46+
// read the old file to be converted
47+
using ReaderType = itk::ImageFileReader< ImageType >;
48+
ReaderType::Pointer reader = ReaderType::New();
49+
reader->SetFileName( inputImage );
50+
51+
using ImageType = itk::Image< PixelType, Dimension >;
52+
53+
// Create the filter and apply the algorithm to the image
54+
using FilterType = itk::AdditiveGaussianNoiseImageFilter< ImageType, ImageType >;
55+
FilterType::Pointer filter = FilterType::New();
56+
filter->SetInput( reader->GetOutput() );
57+
filter->SetMean( mean ); // set the mean
58+
filter->SetStandardDeviation( deviation ); // Set the standard deviation
59+
60+
// Set the writer to save file
61+
using WriterType = itk::ImageFileWriter< ImageType >;
62+
WriterType::Pointer writer = WriterType::New();
63+
writer->SetFileName( outputImage );
64+
writer->SetInput( filter->GetOutput() );
65+
66+
//Write the output image
67+
try
68+
{
69+
writer->Update();
70+
}
71+
catch( itk::ExceptionObject & error )
72+
{
73+
std::cerr << "Error: " << error << std::endl;
74+
return EXIT_FAILURE;
75+
}
76+
77+
return EXIT_SUCCESS;
78+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
import itk
3+
import sys
4+
5+
if len(sys.argv) != 5:
6+
print("Usage: " + sys.argv[0] + "< Input Image > < Output Image > [Mean] [Standard Deviation]")
7+
sys.exit(1)
8+
9+
inputImage = sys.argv[1]
10+
outputImage = sys.argv[2]
11+
mean = float(sys.argv[3])
12+
deviation = float(sys.argv[4])
13+
14+
# Unsigned Char for png usage
15+
InputPixelType = itk.UC
16+
OutputPixelType = itk.UC
17+
Dimension = 2
18+
19+
InputImageType = itk.Image[InputPixelType, Dimension]
20+
OutputImageType = itk.Image[OutputPixelType, Dimension]
21+
22+
ReaderType = itk.ImageFileReader[InputImageType]
23+
reader = ReaderType.New()
24+
reader.SetFileName(inputImage)
25+
26+
FilterType = itk.AdditiveGaussianNoiseImageFilter[InputImageType, InputImageType]
27+
AdditiveFilter = FilterType.New()
28+
AdditiveFilter.SetInput(reader.GetOutput())
29+
AdditiveFilter.SetMean(mean)
30+
AdditiveFilter.SetStandardDeviation(deviation)
31+
32+
33+
34+
WriterType = itk.ImageFileWriter[OutputImageType]
35+
writer = WriterType.New()
36+
writer.SetFileName(outputImage)
37+
writer.SetInput(AdditiveFilter.GetOutput())
38+
39+
writer.Update()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Add Gaussian Noise to an Image
2+
=====================================================
3+
4+
.. index::
5+
single: DerivativeImageFilter
6+
pair: image; random
7+
8+
Synopsis
9+
--------
10+
11+
Adds Gaussian Noise to a particular image
12+
13+
14+
Results
15+
-------
16+
.. figure:: Gourds8.png
17+
:alt: Input Image
18+
Input Image
19+
.. figure:: GourdsFilter.png
20+
:alt: Output Image
21+
Output Image
22+
23+
24+
Code
25+
----
26+
27+
C++
28+
...
29+
30+
.. literalinclude:: Code.cxx
31+
:lines: 18-
32+
Python
33+
......
34+
35+
.. literalinclude:: Code.py
36+
:language: python
37+
:lines: 1, 18-
38+
39+
Classes demonstrated
40+
--------------------
41+
42+
.. breathelink:: itk::AdditiveGaussianNoiseImageFilter
Loading
Loading

src/Filtering/ImageFeature/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ compare_to_baseline(EXAMPLE_NAME BilateralFilterAnImage
3434
add_example(FindZeroCrossings)
3535
#===================================
3636

37-
endif()
37+
endif()
38+
39+
add_example(AdditiveGaussianNoiseImageFilter)
40+
compare_to_baseline(EXAMPLE_NAME AdditiveGaussianNoiseImageFilter
41+
BASELINE_PREFIX OutputBaseline
42+
)

src/Filtering/ImageFeature/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ ImageFeature
1616
ZeroCrossingBasedEdgeDecor/Documentation.rst
1717
BilateralFilterAnImage/Documentation.rst
1818
ExtractContoursFromImage/Documentation.rst
19+
AdditiveGaussianNoiseImageFilter/Documentation.rst

0 commit comments

Comments
 (0)