|
| 1 | +/*============================================================================= |
| 2 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + you may not use this file except in compliance with the License. |
| 4 | + You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +=============================================================================*/ |
| 14 | +#include "itkImage.h" |
| 15 | +#include "itkImageFileReader.h" |
| 16 | +#include "itkImageFileWriter.h" |
| 17 | +#include "itkMedianImageFilter.h" |
| 18 | +#include "MedianFilterCLP.h" |
| 19 | +#include "stdlib.h" |
| 20 | +#include "itkImageIOBase.h" |
| 21 | + |
| 22 | +void GetImageType ( std::string fileName , itk::ImageIOBase::IOComponentType &componentType ) |
| 23 | +{ |
| 24 | + typedef itk::Image< unsigned char, 3 > ImageType ; |
| 25 | + itk::ImageFileReader<ImageType>::Pointer imageReader = itk::ImageFileReader< ImageType >::New() ; |
| 26 | + imageReader->SetFileName( fileName.c_str() ) ; |
| 27 | + imageReader->UpdateOutputInformation() ; |
| 28 | + componentType = imageReader->GetImageIO()->GetComponentType() ; |
| 29 | +} |
| 30 | + |
| 31 | +template< class TImage > |
| 32 | +int ReadImage( const char* fileName , typename TImage::Pointer& image ) |
| 33 | +{ |
| 34 | + typedef TImage ImageType ; |
| 35 | + typedef itk::ImageFileReader< ImageType > ImageReaderType ; |
| 36 | + typename ImageReaderType::Pointer reader = ImageReaderType::New() ; |
| 37 | + reader->SetFileName( fileName ) ; |
| 38 | + try |
| 39 | + { |
| 40 | + reader->Update() ; |
| 41 | + } |
| 42 | + catch( itk::ExceptionObject& e ) |
| 43 | + { |
| 44 | + std::cerr << e.what() << std::endl ; |
| 45 | + return 1 ; |
| 46 | + } |
| 47 | + image = reader->GetOutput() ; |
| 48 | + return 0 ; |
| 49 | +} |
| 50 | +/* Creates the Median Filter and the output image with the input type. */ |
| 51 | +template< class ComponentType > |
| 52 | +int MedianFilter( std::string inputFileName , std::string outputFileName , int radius ) |
| 53 | +{ |
| 54 | + typedef itk::Image< ComponentType , 3 > ImageType ; |
| 55 | + typedef itk::MedianImageFilter < ImageType , ImageType > FilterType ; |
| 56 | + typename FilterType::Pointer medianFilter = FilterType::New() ; |
| 57 | + typename FilterType::InputSizeType filterRadius ; |
| 58 | + typename ImageType::Pointer image = ImageType::New() ; |
| 59 | + if( radius < 1 ) |
| 60 | + { |
| 61 | + std::cout << "Radius value must be non-zero positive integer." << std::endl ; |
| 62 | + return EXIT_FAILURE ; |
| 63 | + } |
| 64 | + if( ReadImage< ImageType >( inputFileName.c_str() , image ) ) |
| 65 | + { |
| 66 | + std::cout << "Image could not be read." << std::endl ; |
| 67 | + return EXIT_FAILURE ; |
| 68 | + } |
| 69 | + filterRadius.Fill( radius ) ; |
| 70 | + medianFilter->SetRadius( filterRadius ) ; |
| 71 | + medianFilter->SetInput( image ) ; |
| 72 | + typedef itk::ImageFileWriter< ImageType > WriterType ; |
| 73 | + typename WriterType::Pointer writer = WriterType::New() ; |
| 74 | + writer->SetInput( medianFilter->GetOutput() ) ; |
| 75 | + writer->SetFileName( outputFileName ) ; |
| 76 | + writer->Update() ; |
| 77 | + return EXIT_SUCCESS ; |
| 78 | +} |
| 79 | + |
| 80 | +int main( int argc , char *argv[] ) |
| 81 | +{ |
| 82 | + PARSE_ARGS ; |
| 83 | + itk::ImageIOBase::IOPixelType pixelType ; |
| 84 | + itk::ImageIOBase::IOComponentType componentType ; |
| 85 | + try |
| 86 | + { |
| 87 | + GetImageType( input , componentType ) ; |
| 88 | +/* Apply the Median Filter according to the type of the input file */ |
| 89 | + switch( componentType ) |
| 90 | + { |
| 91 | + case itk::ImageIOBase::UCHAR: |
| 92 | + { |
| 93 | + return MedianFilter< unsigned char >( input , output , radius ) ; |
| 94 | + break ; |
| 95 | + } |
| 96 | + case itk::ImageIOBase::CHAR: |
| 97 | + { |
| 98 | + return MedianFilter< char >( input , output , radius ) ; |
| 99 | + break ; |
| 100 | + } |
| 101 | + case itk::ImageIOBase::USHORT: |
| 102 | + { |
| 103 | + return MedianFilter< unsigned short >( input , output , radius ) ; |
| 104 | + break ; |
| 105 | + } |
| 106 | + case itk::ImageIOBase::SHORT: |
| 107 | + { |
| 108 | + return MedianFilter< short >( input , output , radius ) ; |
| 109 | + break ; |
| 110 | + } |
| 111 | + case itk::ImageIOBase::UINT: |
| 112 | + { |
| 113 | + return MedianFilter< unsigned int >( input , output , radius ) ; |
| 114 | + break ; |
| 115 | + } |
| 116 | + case itk::ImageIOBase::INT: |
| 117 | + { |
| 118 | + return MedianFilter< int >( input , output , radius ) ; |
| 119 | + break ; |
| 120 | + } |
| 121 | + case itk::ImageIOBase::ULONG: |
| 122 | + { |
| 123 | + return MedianFilter< unsigned long >( input , output , radius ) ; |
| 124 | + break ; |
| 125 | + } |
| 126 | + case itk::ImageIOBase::LONG: |
| 127 | + { |
| 128 | + return MedianFilter< long >( input , output , radius ) ; |
| 129 | + break ; |
| 130 | + } |
| 131 | + case itk::ImageIOBase::FLOAT: |
| 132 | + { |
| 133 | + return MedianFilter< float >( input , output , radius ) ; |
| 134 | + break ; |
| 135 | + } |
| 136 | + case itk::ImageIOBase::DOUBLE: |
| 137 | + { |
| 138 | + return MedianFilter< double >( input , output , radius ) ; |
| 139 | + break ; |
| 140 | + } |
| 141 | + default: |
| 142 | + case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE: |
| 143 | + { |
| 144 | + std::cerr << "Unknown and unsupported component type!" << std::endl ; |
| 145 | + return EXIT_FAILURE ; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + catch( itk::ExceptionObject &excep ) |
| 150 | + { |
| 151 | + std::cerr << argv[0] << ": exception caught !" << std::endl ; |
| 152 | + std::cerr << excep << std::endl ; |
| 153 | + return EXIT_FAILURE ; |
| 154 | + } |
| 155 | +return EXIT_SUCCESS ; |
| 156 | +} |
0 commit comments