| 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 |
#include "itkImage.h" |
| 19 |
#include "itkImageFileReader.h" |
| 20 |
#include "itkMedianImageFilter.h" |
| 21 |
#include "itkSubtractImageFilter.h" |
| 22 |
|
| 23 |
#include "itksys/SystemTools.hxx" |
| 24 |
|
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
#include "QuickView.h" |
| 28 |
#include "itkTestingMacros.h" |
| 29 |
|
| 30 |
int |
| 31 |
itkVtkMedianFilterTest(int argc, char * argv[]) |
| 32 |
{ |
| 33 |
if (argc < 2) |
| 34 |
{ |
| 35 |
std::cerr << "Missing parameters." << std::endl; |
| 36 |
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv); |
| 37 |
std::cerr << " InputImageFile [radius]" << std::endl; |
| 38 |
return EXIT_FAILURE; |
| 39 |
} |
| 40 |
std::string inputFilename = argv[1]; |
| 41 |
|
| 42 |
// Setup types |
| 43 |
using ImageType = itk::Image<float, 2>; |
| 44 |
using ReaderType = itk::ImageFileReader<ImageType>; |
| 45 |
using FilterType = itk::MedianImageFilter<ImageType, ImageType>; |
| 46 |
using SubtractType = itk::SubtractImageFilter<ImageType>; |
| 47 |
|
| 48 |
// Create and setup a reader |
| 49 |
auto reader = ReaderType::New(); |
| 50 |
reader->SetFileName(inputFilename); |
| 51 |
|
| 52 |
// Create and setup a median filter |
| 53 |
auto medianFilter = FilterType::New(); |
| 54 |
FilterType::InputSizeType radius; |
| 55 |
radius.Fill(2); |
| 56 |
if (argc > 2) |
| 57 |
{ |
| 58 |
radius.Fill(std::stoi(argv[2])); |
| 59 |
} |
| 60 |
|
| 61 |
medianFilter->SetRadius(radius); |
| 62 |
medianFilter->SetInput(reader->GetOutput()); |
| 63 |
|
| 64 |
auto diff = SubtractType::New(); |
| 65 |
diff->SetInput1(reader->GetOutput()); |
| 66 |
diff->SetInput2(medianFilter->GetOutput()); |
| 67 |
|
| 68 |
QuickView viewer; |
| 69 |
viewer.AddImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(inputFilename)); |
| 70 |
|
| 71 |
std::stringstream desc; |
| 72 |
desc << "MedianImageFilter, radius = " << radius; |
| 73 |
viewer.AddImage(medianFilter->GetOutput(), true, desc.str()); |
| 74 |
|
| 75 |
std::stringstream desc2; |
| 76 |
desc2 << "Original - Median"; |
| 77 |
viewer.AddImage(diff->GetOutput(), true, desc2.str()); |
| 78 |
|
| 79 |
// For testing, turn off interaction |
| 80 |
viewer.Visualize(false); |
| 81 |
|
| 82 |
|
| 83 |
std::cout << "Test finished." << std::endl; |
| 84 |
return EXIT_SUCCESS; |
| 85 |
} |