ITK阈值分割nrrd图像

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkNrrdImageIOFactory.h"
#include "itkThresholdImageFilter.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkSmartPointer.h"
#include "vtkImageFlip.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);

int main(int argc, char ** argv) {
    typedef itk::Image ImageType;
    typedef itk::ThresholdImageFilter< ImageType > FilterType;
    typedef itk::ImageFileReader ReaderType;
    typedef itk::ImageFileWriter WriterType;
    typedef itk::ImageToVTKImageFilter ConnectorType;  //connenct itk and vtk

    itk::NrrdImageIOFactory::RegisterOneFactory();

    FilterType::Pointer filter = FilterType::New();
    ReaderType::Pointer reader = ReaderType::New();
    WriterType::Pointer writer = WriterType::New();
    ConnectorType::Pointer connector = ConnectorType::New();

    reader->SetFileName("1.nrrd");    //要读取的文件名
    writer->SetFileName("2.nrrd");    //写入的文件名

    filter->SetInput(reader->GetOutput());
    filter->SetOutsideValue(0);
    filter->ThresholdBelow(2604);
    filter->ThresholdAbove(3573);
    filter->Update();
    writer->SetInput(filter->GetOutput());
    try {
        writer->Update();
    }
    catch (itk::ExceptionObject &err) {
        std::cerr << "ExceptionObject Caught" << std::endl;
        std::cerr << err << std::endl;
        system("pause");
        return EXIT_FAILURE;
    }

    connector->SetInput(reader->GetOutput());
    connector->Update();

    vtkImageViewer *viewer = vtkImageViewer::New();
    vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
    viewer->SetInputData(connector->GetOutput());
    viewer->SetupInteractor(interactor);
    viewer->GetRenderWindow()->SetSize(500, 500);//set window size
    viewer->SetColorWindow(255); //set window color
    viewer->SetColorLevel(128);   //set the level of window
    viewer->Render();

    interactor->Initialize();
    interactor->Start();

    return EXIT_SUCCESS;
}

 

你可能感兴趣的:(ITK)