方法一:itk::image 转buffer,然后调用torch::from_blob
步骤如下:
1,读取图像到itk::Image
2,将itk::Image 的数据写入连续的buffer中
3,调用libtorch的from_blob函数将buffer的内容转入tensor
// read image with itk
const char* im_path = "H:\\tmp\\a\\src.mhd";
using ImageType = itk::Image<float,3>;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(im_path);
ImageType::Pointer image = reader->GetOutput();
reader->Update();
// print some point for debug
std::cout<<"For itk Image3d:"<<std::endl;
std::cout<<"point[224,297,10] must be 2086, now is "
<<image->GetPixel({
224,297,10})<<std::endl;
std::cout<<"point[113,285,10] must be 167.3, now is "
<<image->GetPixel({
113,285,10})<<std::endl;
std::cout<<"point[301,371,5] must be 1086, now is "
<<image->GetPixel({
301,371,5})<<std::endl