[ITK] [ITK-users] White Background on rotated image

Matias Montroull matimontg at gmail.com
Fri Nov 14 10:56:53 EST 2014


I figured how to rotate a DICOM Series in a directory, here's my code,
however, I get white images as a result, I'm not sure what I'm missing?

#include "itkImage.h"
#include "itkResampleImageFilter.h"
#include "itkAffineTransform.h"
#include "itkGDCMImageIO.h"

//Para las series
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkNumericSeriesFileNames.h"

int main( int argc, char * argv[] )
{
if( argc < 4 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " Directorio_Entrada Directorio_Salida
Grados_Rotacion" << std::endl;
return EXIT_FAILURE;
}

const unsigned int InputDimension = 3;
const unsigned int OutputDimension = 2;
typedef signed short PixelType;
typedef itk::Image< PixelType, InputDimension >    InputImageType;
typedef itk::Image< PixelType, OutputDimension >    OutputImageType;
typedef itk::ImageSeriesReader< InputImageType >    ReaderType;
typedef itk::GDCMImageIO    ImageIOType;
typedef itk::GDCMSeriesFileNames    InputNamesGeneratorType;
typedef itk::NumericSeriesFileNames    OutputNamesGeneratorType;
typedef itk::ImageSeriesWriter< InputImageType, OutputImageType >
 SeriesWriterType;
typedef itk::ResampleImageFilter< InputImageType, InputImageType >
ResampleFilterType;
typedef itk::LinearInterpolateImageFunction<InputImageType, double >
InterpolatorType;

ImageIOType::Pointer gdcmIO = ImageIOType::New();
InputNamesGeneratorType::Pointer inputNames =
InputNamesGeneratorType::New();
inputNames->SetInputDirectory( argv[1] );
const ReaderType::FileNamesContainer & filenames =
inputNames->GetInputFileNames();

ReaderType::Pointer reader = ReaderType::New();

reader->SetImageIO( gdcmIO );
reader->SetFileNames( filenames );
try
{
reader->Update();
}
catch (itk::ExceptionObject &excp)
{
std::cerr << "Exception thrown while reading the series" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}


const double angleInDegrees = atof( argv[3] );

typedef itk::AffineTransform< double, InputDimension > TransformType;
//usado para mapear el espacio de entrada con el espacio de salida
TransformType::Pointer transform = TransformType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();


const InputImageType::PointType & origin = reader->GetOutput()->GetOrigin();
const InputImageType::SpacingType& inputSpacing =
reader->GetOutput()->GetSpacing();
const InputImageType::RegionType& inputRegion =
reader->GetOutput()->GetLargestPossibleRegion();
const InputImageType::SizeType& inputSize =
inputRegion.GetSize();

std::cout << "The input series in directory " << argv[1]
<< " has " << filenames.size() << " files with spacing "
<< inputSpacing
<< std::endl;

transform->SetIdentity();

ResampleFilterType::Pointer filter = ResampleFilterType::New();
filter->SetInput( reader->GetOutput() );
filter->SetInterpolator( interpolator );
filter->SetOutputOrigin( reader->GetOutput()->GetOrigin() );
filter->SetOutputSpacing( inputSpacing );
filter->SetOutputDirection( reader->GetOutput()->GetDirection() );
filter->SetSize( inputSize );
filter->SetDefaultPixelValue(100); //Pixel por defecto de lo que queda
fuera de la imagen cuando se rota
//filter->Update();

TransformType::OutputVectorType translation1;
const double imageCenterX = origin[0] + inputSpacing[0] * inputSize[0] /
2.0;
const double imageCenterY = origin[1] + inputSpacing[1] * inputSize[1] /
2.0;
translation1[0] = -imageCenterX;
translation1[1] = -imageCenterY;
transform->Translate( translation1 );
// Software Guide : EndCodeSnippet
std::cout << "imageCenterX = " << imageCenterX << std::endl;
std::cout << "imageCenterY = " << imageCenterY << std::endl;

const double degreesToRadians = std::atan(1.0) / 45.0;
const double angle = angleInDegrees * degreesToRadians;
transform->Rotate2D( -angle, false );

TransformType::OutputVectorType translation2;
translation2[0] = imageCenterX;
translation2[1] = imageCenterY;
transform->Translate( translation2, false );
filter->SetTransform( transform );
filter->Update();


// Generate the file names
itksys::SystemTools::MakeDirectory( argv[2] );
OutputNamesGeneratorType::Pointer outputNames =
OutputNamesGeneratorType::New();
std::string seriesFormat(argv[2]);
seriesFormat = seriesFormat + "/" + "IM%d.dcm";
outputNames->SetSeriesFormat (seriesFormat.c_str());
outputNames->SetStartIndex (1);
outputNames->SetEndIndex (230); //Rectificar

SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
seriesWriter->SetInput( filter->GetOutput() );
seriesWriter->SetImageIO( gdcmIO );
seriesWriter->SetFileNames( outputNames->GetFileNames() );
//seriesWriter->SetMetaDataDictionaryArray( &outputArray );
try
{
seriesWriter->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception thrown while writing the series " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}

On Fri, Nov 14, 2014 at 10:28 AM, Matias Montroull <matimontg at gmail.com>
wrote:

> I'm having hard times following the code in the example. RIght now I have
> been able to rotate a dcm file one at a time, now I would like to input a
> directory and output another directory with  the rotated images, I would
> need some guidance for this task.
> Can I just set the input as directory instead of a file for example?
>
> On Thu, Nov 13, 2014 at 3:26 PM, Matias Montroull <matimontg at gmail.com>
> wrote:
>
>> Thanks Matt, I'll give it a try
>>
>> On Thu, Nov 13, 2014 at 2:37 PM, Matt McCormick <
>> matt.mccormick at kitware.com> wrote:
>>
>>> Hi Matias,
>>>
>>> This example may have a hint on how to write a series [1].
>>>
>>> HTH,
>>> Matt
>>>
>>> [1]
>>> http://itk.org/Doxygen/html/IO_2ImageReadDicomSeriesWrite_8cxx-example.html
>>>
>>> On Thu, Nov 13, 2014 at 12:18 PM, Matias Montroull <matimontg at gmail.com>
>>> wrote:
>>> > Now I have another question. This example is just for one file, is
>>> there a
>>> > way to do it for a directory containing a set of DICOM files?
>>> >
>>> > On Thu, Nov 13, 2014 at 2:16 PM, Matias Montroull <matimontg at gmail.com
>>> >
>>> > wrote:
>>> >>
>>> >> never mind, it was my bad. The InputPixelType was unsigned char and
>>> should
>>> >> have been unsigned short
>>> >>
>>> >> On Thu, Nov 13, 2014 at 2:14 PM, Matias Montroull <
>>> matimontg at gmail.com>
>>> >> wrote:
>>> >>>
>>> >>> Bradley, as soon as I enter this line of code:
>>> >>>
>>> >>> typedef itk::GDCMImageIO ImageIOType;
>>> >>> ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
>>> >>> writer->SetImageIO(gdcmImageIO);
>>> >>>
>>> >>> I get a bad image as result.. any ideas what could it be?
>>> >>>
>>> >>> On Thu, Nov 13, 2014 at 11:36 AM, Bradley Lowekamp
>>> >>> <blowekamp at mail.nih.gov> wrote:
>>> >>>>
>>> >>>> Hello again,
>>> >>>>
>>> >>>> This is a non-trivial task where caution in needed to determine
>>> which
>>> >>>> tags should be copied. But here is a basic example here:
>>> >>>> http://itk.org/Wiki/ITK/Examples/DICOM/ResampleDICOM
>>> >>>>
>>> >>>> Brad
>>> >>>>
>>> >>>> On Nov 13, 2014, at 9:30 AM, Matias Montroull <matimontg at gmail.com>
>>> >>>> wrote:
>>> >>>>
>>> >>>> I have one more question, it seems the writer method removes lot of
>>> tags
>>> >>>> from the image, is there an option to keep the original tags such
>>> as patient
>>> >>>> name, model, manufacture etc?
>>> >>>>
>>> >>>> On Thu, Nov 13, 2014 at 11:27 AM, Matias Montroull <
>>> matimontg at gmail.com>
>>> >>>> wrote:
>>> >>>>>
>>> >>>>> Bradley, I must say you're the man! that did the trick, thanks a
>>> lot!
>>> >>>>>
>>> >>>>> On Thu, Nov 13, 2014 at 10:59 AM, Bradley Lowekamp
>>> >>>>> <blowekamp at mail.nih.gov> wrote:
>>> >>>>>>
>>> >>>>>> Hello,
>>> >>>>>>
>>> >>>>>> Check out the ImageToImageFilter::SetDefaultPixelValue method [1].
>>> >>>>>>
>>> >>>>>> Brad
>>> >>>>>>
>>> >>>>>> [1]
>>> >>>>>>
>>> http://www.itk.org/Doxygen/html/classitk_1_1ResampleImageFilter.html#a227397f3786a0ec4d3107cb73033b4be
>>> >>>>>>
>>> >>>>>> On Nov 13, 2014, at 8:56 AM, Matias Montroull <
>>> matimontg at gmail.com>
>>> >>>>>> wrote:
>>> >>>>>>
>>> >>>>>> Hi,
>>> >>>>>>
>>> >>>>>> I'm running this example:
>>> >>>>>>
>>> >>>>>>
>>> http://www.itk.org/Doxygen/html/Filtering_2ResampleImageFilter3_8cxx-example.html
>>> >>>>>>
>>> >>>>>> and I'm getting this result which is fine but I need the white
>>> >>>>>> sections left due to the rotation to be black. how can I achieve
>>> this?
>>> >>>>>>
>>> >>>>>> <Capture.JPG>
>>> >>>>>>
>>> >>>>>> _____________________________________
>>> >>>>>> Powered by www.kitware.com
>>> >>>>>>
>>> >>>>>> Visit other Kitware open-source projects at
>>> >>>>>> http://www.kitware.com/opensource/opensource.html
>>> >>>>>>
>>> >>>>>> Kitware offers ITK Training Courses, for more information visit:
>>> >>>>>> http://www.kitware.com/products/protraining.php
>>> >>>>>>
>>> >>>>>> Please keep messages on-topic and check the ITK FAQ at:
>>> >>>>>> http://www.itk.org/Wiki/ITK_FAQ
>>> >>>>>>
>>> >>>>>> Follow this link to subscribe/unsubscribe:
>>> >>>>>> http://public.kitware.com/mailman/listinfo/insight-users
>>> >>>>>>
>>> >>>>>>
>>> >>>>>
>>> >>>>
>>> >>>>
>>> >>>
>>> >>
>>> >
>>> >
>>> > _____________________________________
>>> > Powered by www.kitware.com
>>> >
>>> > Visit other Kitware open-source projects at
>>> > http://www.kitware.com/opensource/opensource.html
>>> >
>>> > Kitware offers ITK Training Courses, for more information visit:
>>> > http://www.kitware.com/products/protraining.php
>>> >
>>> > Please keep messages on-topic and check the ITK FAQ at:
>>> > http://www.itk.org/Wiki/ITK_FAQ
>>> >
>>> > Follow this link to subscribe/unsubscribe:
>>> > http://public.kitware.com/mailman/listinfo/insight-users
>>> >
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20141114/08d245aa/attachment-0001.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list