使用c++版本的itk计算二值三维图像的表面

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkMesh.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkBinaryMask3DMeshSource.h"
#include "itkMeshFileWriter.h"
#include
#include 


// 读取nii数据
template<typename image_type, typename image_pointer>
void readData(const std::string& file_path, image_pointer& out_image) {
    using imageIOType = itk::NiftiImageIO;
    using readerType = itk::ImageFileReader<image_type>;

    auto reader = readerType::New();
    auto nifitiIO = imageIOType::New();

    reader->SetImageIO(nifitiIO);
    reader->SetFileName(file_path);
    reader->Update();

    out_image = reader->GetOutput();
}


// 写入nii数据
template<typename image_type, typename image_pointer>
void writeData(const image_pointer& write_image, const std::string& write_path) {
    using writerType = itk::ImageFileWriter<image_type>;
    auto writer = writerType::New();

    using niiIOType = itk::NiftiImageIO;
    auto niiIO = niiIOType::New();
    writer->SetImageIO(niiIO);

    writer->SetInput(write_image);
    writer->SetFileName(write_path);
    writer->Update();
}



int main()
{
    std::string infilename("C:\\Users\\Administrator\\Downloads\\predict_vc_001\\p20220620_093308_254.nii.gz");
    std::string outfilename("surface.obj");

    constexpr unsigned int Dimension = 3;

    using PixelType = unsigned char;
    using ImageType = itk::Image<PixelType, Dimension>;


    ImageType::Pointer input;
    readData<ImageType, ImageType::Pointer>(infilename, input);

    using MeshType = itk::Mesh<double, Dimension>;

    using FilterType = itk::BinaryMask3DMeshSource<ImageType, MeshType>;
    auto filter = FilterType::New();
    filter->SetInput(input);
    filter->SetObjectValue(1);

    using WriterType = itk::MeshFileWriter<MeshType>;
    auto writer = WriterType::New();
    writer->SetMeshIO(itk::OBJMeshIO::New());
    writer->SetFileName(outfilename);

    writer->SetInput(filter->GetOutput());
    try
    {
        writer->Update();
    }
    catch (const itk::ExceptionObject& error)
    {
        std::cerr << "Error: " << error << std::endl;
        return EXIT_FAILURE;
    }

	return 0;
}

保存的.obj表面文件,可以通过para view打开查看

你可能感兴趣的:(医学图像处理1,c++,开发语言)