[Insight-users] Using ScaleTransform to stretch an image
Dan Mueller
dan.muel at gmail.com
Tue Nov 8 15:51:46 EST 2011
Hi David,
Aha. After closer inspection I see that you are forgetting the concept
of spacing. You need to think carefully about what happens to spacing
after a scale.
Please find code below which I think does what you want. In fact,
because you want to change the output size, you don't even need to use
a ScaleTransform -- an IdentityTransform works just fine.
#include "itkImage.h"
#include "itkIdentityTransform.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkResampleImageFilter.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
typedef itk::Image<unsigned char, 2> ImageType;
typedef ImageType::SizeType SizeType;
typedef ImageType::SpacingType SpacingType;
typedef itk::ImageFileWriter<ImageType> WriterType;
typedef itk::IdentityTransform<double, 2> TransformType;
typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleImageFilterType;
typedef itk::NearestNeighborInterpolateImageFunction<ImageType>
InterpolatorType;
static void CreateImage(ImageType::Pointer image);
int main(int argc, char *argv[])
{
// Create input image
ImageType::Pointer input = ImageType::New();
CreateImage(input);
SizeType inputSize = input->GetLargestPossibleRegion().GetSize();
SpacingType inputSpacing = input->GetSpacing();
std::cout << "Input size: " << inputSize << std::endl;
std::cout << "Writing input... " << std::endl;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("C:/Temp/input.png");
writer->SetInput(input);
writer->Update();
input->DisconnectPipeline();
// Resize
std::cout << "Resizing... " << std::endl;
SizeType outputSize;
outputSize.Fill(200);
SpacingType outputSpacing;
for (unsigned int d=0; d<ImageType::ImageDimension; d++)
{
outputSpacing[d] = input->GetSpacing()[d] *
((double)inputSize[d] / (double)outputSize[d]);
}
ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
resample->SetInput(input);
resample->SetSize(outputSize);
resample->SetOutputSpacing(outputSpacing);
resample->SetTransform(TransformType::New());
resample->SetInterpolator(InterpolatorType::New());
resample->UpdateLargestPossibleRegion();
ImageType::Pointer output = resample->GetOutput();
output->DisconnectPipeline();
std::cout << "Output size: " <<
output->GetLargestPossibleRegion().GetSize() << std::endl;
std::cout << "Writing output... " << std::endl;
WriterType::Pointer outputWriter = WriterType::New();
outputWriter->SetFileName("C:/Temp/output.mha");
outputWriter->SetInput(output);
outputWriter->Update();
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image)
{
// Allocate empty image
itk::Index<2> start; start.Fill(0);
itk::Size<2> size; size.Fill(100);
ImageType::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
// Make a white square
for(unsigned int r = 40; r < 60; r++)
{
for(unsigned int c = 40; c < 60; c++)
{
ImageType::IndexType pixelIndex;
pixelIndex[0] = r;
pixelIndex[1] = c;
image->SetPixel(pixelIndex, 255);
}
}
}
HTH
Cheers, Dan
On 9 November 2011 06:04, David Doria <daviddoria at gmail.com> wrote:
> On Tue, Nov 8, 2011 at 2:43 PM, Dan Mueller <dan.muel at gmail.com> wrote:
>> Hi David,
>>
>> I think you forgot to set the center of the transform. Try something
>> like the following:
>>
>> typedef itk::ScaleTransform<double, 2> TransformType;
>> TransformType::Pointer scaleTransform = TransformType::New();
>> itk::FixedArray<float, 2> scale;
>> scale[0] = 1.5;
>> scale[1] = 1.5;
>> scaleTransform->SetScale(scale);
>> TransformType::PointType center;
>> // NOTE: The below code assumes no offset/origin and unity spacing
>> center[0] = image->GetLargestPossibleRegion().GetSize()[0] / 2;
>> center[1] = image->GetLargestPossibleRegion().GetSize()[1] / 2;
>> scaleTransform->SetCenter(center);
>>
>> HTH
>>
>> Cheers, Dan
>
> Hm, that makes sense, but it didn't fix it.
>
> Updated code: http://www.itk.org/Wiki/ITK/Examples/WishList/ImageProcessing/ScaleTransform
>
> David
>
More information about the Insight-users
mailing list