新的ITK,VTK。读取tiff文件并将它们作为卷或3D切片进行翻录。错误

时间:2016-10-25 13:47:05

标签: image 3d vtk itk

请帮我阅读一系列tiff图像并将其显示为体积或切片,以查看我使用ITK,VTK读取的内容。以下代码一直没有工作到最后。仅适用于读取所有图像和格式连接器,但显示它不起作用。该怎么办?

#include <vtkSmartPointer.h>
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkTIFFReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkImageActor.h"
#include "vtkVersion.h"
#include "vtkImageViewer.h"
#include "vtkImageMapper3D.h"
#include "vtkInteractorStyleImage.h"
#include <vtkRenderer.h>
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkTIFFImageIO.h"
#include "itkImageSeriesReader.h"
#include "itkNumericSeriesFileNames.h"
#include "itkImageFileWriter.h"
#include "itkImageToVTKImageFilter.h"


int main(int argc, char* argv[])
{
    // Verify the number of parameters in the command line
    if (argc < 4)
    {
        std::cerr << "Usage: " <<   std::endl;
        std::cerr << argv[0]
            << " pattern firstSliceValue lastSliceValue"   
            << std::endl;
        return EXIT_FAILURE;
    }

    typedef unsigned short PixelType;
    typedef itk::Image< PixelType, 3 > ImageStackType;
    typedef itk::ImageSeriesReader< ImageStackType > ReaderType;
    typedef itk::NumericSeriesFileNames NameGeneratorType;

    const unsigned int first = atoi(argv[2]);
    const unsigned int last = atoi(argv[3]);

    //Generate Numerical File Names
    NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
    nameGenerator->SetSeriesFormat(argv[1]);
    nameGenerator->SetStartIndex(first);
    nameGenerator->SetEndIndex(last);
    nameGenerator->SetIncrementIndex(1);

    std::vector<std::string> names = nameGenerator->GetFileNames();

    // List the files
    //
    std::vector<std::string>::iterator nit;
    for (nit = names.begin();
        nit != names.end();
        nit++)
    {
        std::cout << "File: " << (*nit).c_str() << std::endl;
    }


    //Read Stack as TIFFs
    ReaderType::Pointer stackReader = ReaderType::New();
    stackReader->SetImageIO(itk::TIFFImageIO::New());
    stackReader->SetFileNames(names);
    stackReader->Update();

    try
    {
        stackReader->Update();
    }
    catch (itk::ExceptionObject & err)
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
    }

    typedef itk::ImageToVTKImageFilter< ImageStackType >  StackConnectorType;

    ImageStackType::SizeType size = stackReader->GetOutput()-  >GetBufferedRegion().GetSize();
    int *extent = new int[6];
    extent[0]   = 0;
    extent[1]   = size[0];
    extent[2]   = 0;
    extent[3]   = size[1];
    extent[4]   = 0;
    extent[5]   = size[2];

    //Export to VTK
    StackConnectorType::Pointer stackConnector = StackConnectorType::New();
    stackConnector->SetInput(stackReader->GetOutput());
    stackConnector->Update();
    stackConnector->GetOutput()->GetExtent(extent);
    cout << "Size: " << extent[1] << "x" << extent[3] << "x" << extent[5] <<   endl;

    // Visualize
    vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputConnection(stackReader->GetOutputPort());
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =   vtkSmartPointer<vtkRenderWindowInteractor>::New();  
    imageViewer->SetupInteractor(renderWindowInteractor);
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->Render();

    return EXIT_SUCCESS;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

你非常接近。稍作修改的工作代码如下。顺便说一句,如果图像的扩展名为.tif,则不需要将ImageIO设置为TIFF。我给出了以下命令行参数"./IMG%03d.png" 0 432

#include <vtkSmartPointer.h>
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkTIFFReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkImageActor.h"
#include "vtkVersion.h"
#include "vtkImageViewer.h"
#include "vtkImageMapper3D.h"
#include "vtkInteractorStyleImage.h"
#include <vtkRenderer.h>
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkTIFFImageIO.h"
#include "itkImageSeriesReader.h"
#include "itkNumericSeriesFileNames.h"
#include "itkImageFileWriter.h"
#include "itkImageToVTKImageFilter.h"


int main(int argc, char* argv[])
{
    // Verify the number of parameters in the command line
    if (argc < 4)
    {
        std::cerr << "Usage: " << std::endl;
        std::cerr << argv[0]
            << " pattern firstSliceValue lastSliceValue"
            << std::endl;
        return EXIT_FAILURE;
    }

    typedef unsigned short PixelType;
    typedef itk::Image< PixelType, 3 > ImageStackType;
    typedef itk::ImageSeriesReader< ImageStackType > ReaderType;
    typedef itk::NumericSeriesFileNames NameGeneratorType;

    const unsigned int first = atoi(argv[2]);
    const unsigned int last = atoi(argv[3]);

    //Generate Numerical File Names
    NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
    nameGenerator->SetSeriesFormat(argv[1]);
    nameGenerator->SetStartIndex(first);
    nameGenerator->SetEndIndex(last);
    nameGenerator->SetIncrementIndex(1);

    std::vector<std::string> names = nameGenerator->GetFileNames();

    // List the files
    //
    std::vector<std::string>::iterator nit;
    for (nit = names.begin();
        nit != names.end();
        nit++)
    {
        std::cout << "File: " << (*nit).c_str() << std::endl;
    }


    //Read Stack as TIFFs
    ReaderType::Pointer stackReader = ReaderType::New();
    //stackReader->SetImageIO(itk::TIFFImageIO::New());
    stackReader->SetFileNames(names);
    stackReader->Update();

    try
    {
        stackReader->Update();
    }
    catch (itk::ExceptionObject & err)
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
    }

    typedef itk::ImageToVTKImageFilter< ImageStackType >  StackConnectorType;

    ImageStackType::SizeType size = stackReader->GetOutput()->GetBufferedRegion().GetSize();
    int *extent = new int[6];
    extent[0] = 0;
    extent[1] = size[0];
    extent[2] = 0;
    extent[3] = size[1];
    extent[4] = 0;
    extent[5] = size[2];

    //Export to VTK
    StackConnectorType::Pointer stackConnector = StackConnectorType::New();
    stackConnector->SetInput(stackReader->GetOutput());
    stackConnector->Update();
    stackConnector->GetOutput()->GetExtent(extent);
    cout << "Size: " << extent[1] << "x" << extent[3] << "x" << extent[5] << endl;

    // Visualize
    vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputData(stackConnector->GetOutput());
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    imageViewer->SetupInteractor(renderWindowInteractor);
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    //imageViewer->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}