ITK  4.8.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Filtering/Smoothing/MedianFilteringOfAnRGBImage/Code.cxx
#include "itkRGBPixel.h"
#include "itkImage.h"
namespace itk
{
template< typename TComponent = unsigned short >
class myRGBPixel : public RGBPixel<TComponent>
{
public:
typedef myRGBPixel Self;
typedef RGBPixel<TComponent> Superclass;
using RGBPixel<TComponent>::operator=;
bool operator<=(const Self & r) const
{
return (this->GetLuminance() <= r.GetLuminance());
}
bool operator<(const Self & r) const
{
return (this->GetLuminance() < r.GetLuminance());
}
};
}
int main(int argc, char * argv[])
{
// Verify command line arguments
if( argc != 4 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " <InputImageFile> <OutputImageFile> <radius>"
<< std::endl;
return EXIT_FAILURE;
}
// Setup types
const unsigned int Dimension = 2;
typedef itk::myRGBPixel< unsigned char > MyPixelType;
// Create and setup a reader
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
// Create and setup a median filter
FilterType::Pointer medianFilter = FilterType::New();
FilterType::InputSizeType radius;
radius.Fill( atoi( argv[3] ) );
medianFilter->SetRadius( radius );
medianFilter->SetInput( reader->GetOutput() );
// Cast the custom myRBGPixel's to RGBPixel's
typedef itk::RGBPixel< unsigned char > RGBPixelType;
CastType::Pointer cast = CastType::New();
cast->SetInput( medianFilter->GetOutput() );
WriterType::Pointer writer = WriterType::New();
writer->SetInput( cast->GetOutput() );
writer->SetFileName( argv[2] );
try
{
writer->Update();
}
catch( itk::ExceptionObject & error )
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}