#include <iostream>
using InputPixelType = unsigned short;
ImageType::PixelType, RGBImageType::PixelType>;
static void CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap);
int main( int argc, char * argv[] )
{
if( argc != 3 )
{
std::cerr << "Usage: " << argv[0]
<< " inputImageFile pixelSize"
<< std::endl;
return EXIT_FAILURE;
}
double spacing = atof(argv[2]);
ReaderType::Pointer reader =
ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();
TransformType::Pointer transform =
TransformType::New();
transform->SetIdentity();
GaussianInterpolatorType::Pointer gaussianInterpolator =
GaussianInterpolatorType::New();
gaussianInterpolator->SetSigma(1.0);
gaussianInterpolator->SetAlpha(3.0);
NearestNeighborInterpolatorType::Pointer nearestNeighborInterpolator =
NearestNeighborInterpolatorType::New();
ResampleFilterType::Pointer resizeFilter1 =
ResampleFilterType::New();
resizeFilter1->SetTransform(transform);
resizeFilter1->SetInterpolator(gaussianInterpolator);
ResampleFilterType::Pointer resizeFilter2 =
ResampleFilterType::New();
resizeFilter2->SetTransform(transform);
resizeFilter2->SetInterpolator(nearestNeighborInterpolator);
const ImageType::SpacingType& inputSpacing =
reader->GetOutput()->GetSpacing();
double outputSpacing[2];
outputSpacing[0] = spacing;
outputSpacing[1] = spacing;
reader->GetOutput()->GetLargestPossibleRegion();
unsigned int oldWidth = inputSize[0];
unsigned int oldHeight = inputSize[1];
unsigned int newWidth = (double) oldWidth * inputSpacing[0] / spacing;
unsigned int newHeight = (double) oldHeight * inputSpacing[1] / spacing;
resizeFilter1->SetOutputSpacing(outputSpacing);
resizeFilter2->SetOutputSpacing(outputSpacing);
resizeFilter1->
SetSize(outputSize);
resizeFilter2->SetSize(outputSize);
resizeFilter1->SetInput(reader->GetOutput());
resizeFilter2->SetInput(reader->GetOutput());
ColormapFilterType::Pointer colormapFilter1 =
ColormapFilterType::New();
ColormapType::Pointer colormap
= ColormapType::New();
CreateRandomColormap(4096, colormap);
colormapFilter1->SetInput (reader->GetOutput());
colormapFilter1->SetColormap(colormap);
ColormapFilterType::Pointer colormapFilter2 =
ColormapFilterType::New();
colormapFilter2->SetInput (resizeFilter1->GetOutput());
colormapFilter2->SetColormap(colormap);
ColormapFilterType::Pointer colormapFilter3 =
ColormapFilterType::New();
colormapFilter3->SetInput (resizeFilter2->GetOutput());
colormapFilter3->SetColormap(colormap);
std::stringstream desc;
desc << itksys::SystemTools::GetFilenameName(argv[1]) << ": " << oldWidth << ", " << oldHeight;
colormapFilter1->GetOutput(),
true,
desc.str());
std::stringstream desc2;
desc2 << "Gaussian Interpolation: " << newWidth << ", " << newHeight;
colormapFilter2->GetOutput(),
true,
desc2.str());
std::stringstream desc3;
desc3 << "Nearest Neighbor Interpolation: " << newWidth << ", " << newHeight;
colormapFilter3->GetOutput(),
true,
desc3.str());
return EXIT_SUCCESS;
}
void CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap)
{
#define LOW .3
ColormapType::ChannelType redChannel;
ColormapType::ChannelType greenChannel;
ColormapType::ChannelType blueChannel;
random->SetSeed ( 8775070 );
redChannel.push_back(LOW);
greenChannel.push_back(LOW);
blueChannel.push_back(LOW);
for (unsigned int i = 1; i < size; ++i)
{
redChannel.push_back(static_cast<ColormapType::RealType>
(random->GetUniformVariate(LOW, 1.0)));
greenChannel.push_back(static_cast<ColormapType::RealType>
(random->GetUniformVariate(LOW, 1.0)));
blueChannel.push_back(static_cast<ColormapType::RealType>
(random->GetUniformVariate(LOW, 1.0)));
}
colormap->SetRedChannel(redChannel);
colormap->SetGreenChannel(greenChannel);
colormap->SetBlueChannel(blueChannel);
}