[Insight-users] Can't paste Slice back into 3D Volume

A D callidus.713 at gmail.com
Mon Aug 20 09:08:06 EDT 2012


Dear ITK-Users!

I have problems to paste an extracted and edited slice back into its 3D
Volume. I tried this with the *PasteImageFilter *with the *SetInPlace*(true),
but it doesn't change the actual 3D Volume after reinserting the slice!

Yours Thankfully, AD

Here is the code I have used, have anyone an idea, why it doesn't work?

#include "itkImage.h"
#include "itkImageFileReader.h"

#include "itkExtractImageFilter.h"
#include "itkAnalyzeImageIO.h"

#include "itkSmartPointer.h"

#include "itkPasteImageFilter.h"
#include "itkOrientImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkInPlaceImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"

typedef float        InputPixelType3D;
typedef float        OutputPixelType2D;

typedef itk::Image< InputPixelType3D,  3 >    InputImageType3D;
typedef itk::Image< OutputPixelType2D, 2 >    OutputImageType2D;

typedef itk::ImageFileReader< InputImageType3D  >  ReaderType3D;
typedef itk::CastImageFilter< OutputImageType2D, InputImageType3D >
Cast2Dto3DImageFilter;
typedef itk::ExtractImageFilter< InputImageType3D, OutputImageType2D >
ExtractImageFilter;
typedef itk::InPlaceImageFilter < InputImageType3D, InputImageType3D>
InPlaceImageFilter;
typedef itk::PasteImageFilter < InputImageType3D,  InputImageType3D>
PasteImageFilterType;
typedef itk::BinaryThresholdImageFilter< OutputImageType2D,
OutputImageType2D > BinaryThresholdFilterType;
typedef float   PixelType;

typedef itk::ImageFileReader< OutputImageType2D > ReaderType;

InputImageType3D::IndexType m_start;

OutputImageType2D::Pointer extract2DImageSlice(InputImageType3D::Pointer
volume, int plane, int slice) {
    ExtractImageFilter::Pointer filter = ExtractImageFilter::New();

    InputImageType3D::RegionType inputRegion =
        volume->GetLargestPossibleRegion();

    InputImageType3D::SizeType m_size;
    m_size = inputRegion.GetSize();
    m_size[plane] = 0;

    m_start = inputRegion.GetIndex();

    const unsigned int sliceNumber = slice;
    m_start[plane] = sliceNumber;

    InputImageType3D::RegionType desiredRegion;
    desiredRegion.SetSize(  m_size  );
    desiredRegion.SetIndex( m_start );
    filter->SetExtractionRegion( desiredRegion );

    filter->SetInput( volume );

    filter->Update();

    return filter->GetOutput();
}


InputImageType3D::Pointer ReadAnalyzeFile(const char *path)
{
    itk::AnalyzeImageIO::Pointer io = itk::AnalyzeImageIO::New();
    ReaderType3D::Pointer fileReader = ReaderType3D::New();
    fileReader->SetImageIO(io);
    fileReader->SetFileName(path);
    fileReader->Update();

    itk::OrientImageFilter<InputImageType3D,InputImageType3D>::Pointer
orienter =
    itk::OrientImageFilter<InputImageType3D,InputImageType3D>::New();
    orienter->UseImageDirectionOn();

orienter->SetDesiredCoordinateOrientation(itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_IRA);
    orienter->SetInput(fileReader->GetOutput());
    orienter->Update();

    return orienter->GetOutput();
 }

  OutputImageType2D::Pointer
performBinaryThreshold(OutputImageType2D::Pointer slice, std::vector<float>
filterParameter) {
        BinaryThresholdFilterType::Pointer filter =
BinaryThresholdFilterType::New();

        filter->SetInput( slice );
        filter->SetOutsideValue(0); //outsideValue
        filter->SetInsideValue(255); //insideValue
        filter->SetLowerThreshold(filterParameter[0]); //lowerThreshold*/
        filter->SetUpperThreshold(filterParameter[1]); //upperThreshold
        filter->Update();

        return filter->GetOutput();
    }

void main() {
    InputImageType3D::Pointer volume = ReadAnalyzeFile( "Volume.hdr" );
    OutputImageType2D::Pointer slice = extract2DImageSlice( volume, 0,  1);

    std::vector<float> param;
    param.push_back(200);
    param.push_back(255);

    OutputImageType2D::Pointer binaryMask = performBinaryThreshold(slice,
param);

    Cast2Dto3DImageFilter::Pointer castFilter =
Cast2Dto3DImageFilter::New();
    castFilter->SetInput(binaryMask);
    castFilter->Update();

    PasteImageFilterType::Pointer pasteImageFilter =
PasteImageFilterType::New();

    pasteImageFilter->SetSourceRegion(
castFilter->GetOutput()->GetLargestPossibleRegion().GetSize() );
    pasteImageFilter->SetSourceImage( castFilter->GetOutput() );

    pasteImageFilter->SetDestinationIndex( m_start );
    pasteImageFilter->SetDestinationImage( volume );
    pasteImageFilter->SetInPlace(true);

    pasteImageFilter->Update();

    volume = pasteImageFilter->GetOutput();
    volume->Update();

    std::cout << "Done!";
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120820/ccd799d4/attachment.htm>


More information about the Insight-users mailing list