我从官网copy的itk的区域增长的实例,原始的示例代码链接在这:https://itk.org/Doxygen/html/Examples_2Segmentation_2ConnectedThresholdImageFilter_8cxx-example.html
然后我修改了文件读取的方式,该为对dcm类型的读写,最终我的代码如下:
#include "itkConnectedThresholdImageFilter.h"
#include "itkImage.h"
#include "itkCastImageFilter.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"
int Isocontour2D( int pos[3], float lowerThreshold, float upperThreshold);
int main(int argc, char* argv[])
{
Isocontour2D( new int[]{457,430,1},40,300);
return 0;
}
int Isocontour2D(int pos[3], float lowerThreshold, float upperThreshold) {
using InputPixelType = float;
constexpr unsigned int Dimension = 3;
using InputImageType = itk::Image<InputPixelType, Dimension>;
using OutputPixelType = unsigned char;
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
//创建读取DCM的GDCMIOImage类
using ImageIOType = itk::GDCMImageIO;
ImageIOType::Pointer dcmImageIO = ImageIOType::New();
// 定义图像映射类,将InputImage强制类型转换为OutputInage
using CastingFilterType =
itk::CastImageFilter<InputImageType, OutputImageType>;
CastingFilterType::Pointer caster = CastingFilterType::New();
// 实例化文件读写类
using ReaderType = itk::ImageFileReader<InputImageType>;
using WriterType = itk::ImageFileWriter<OutputImageType>;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName("K:\\dicom\\IMG-0064.dcm");
writer->SetFileName("J:\\result.DCM");
// 设置读写I/O流类型
reader->SetImageIO(dcmImageIO);
writer->SetImageIO(dcmImageIO);
// 在进行区域扩散前对图像进行平滑处理,减小噪声的污染
using CurvatureFlowImageFilterType =
itk::CurvatureFlowImageFilter<InputImageType, InputImageType>;
CurvatureFlowImageFilterType::Pointer smoothing =
CurvatureFlowImageFilterType::New();
// 连通阈值区域扩散类实例化
using ConnectedFilterType =
itk::ConnectedThresholdImageFilter<InputImageType, InputImageType>;
ConnectedFilterType::Pointer connectedThreshold =
ConnectedFilterType::New();
smoothing->SetInput(reader->GetOutput());
connectedThreshold->SetInput(smoothing->GetOutput());
caster->SetInput(connectedThreshold->GetOutput());
writer->SetInput(caster->GetOutput());
smoothing->SetNumberOfIterations(5);
smoothing->SetTimeStep(0.125);
// 设置区域生长的阈值区间
connectedThreshold->SetLower(lowerThreshold);
connectedThreshold->SetUpper(upperThreshold);
connectedThreshold->SetReplaceValue(255);
// 设置种子点坐标
InputImageType::IndexType index;
index[0] = pos[0];
index[1] = pos[1];
index[2] = pos[2];
connectedThreshold->SetSeed(index);
try
{
writer->Update();
}
catch (const itk::ExceptionObject& excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
return EXIT_SUCCESS;
}
然后程序在writer->Update();代码内部直接报错:
有大神能帮我看看问题出在哪嘛。
然后我修改了文件读取的方式,该为对dcm类型的读写,最终我的代码如下:
#include "itkConnectedThresholdImageFilter.h"
#include "itkImage.h"
#include "itkCastImageFilter.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"
int Isocontour2D( int pos[3], float lowerThreshold, float upperThreshold);
int main(int argc, char* argv[])
{
Isocontour2D( new int[]{457,430,1},40,300);
return 0;
}
int Isocontour2D(int pos[3], float lowerThreshold, float upperThreshold) {
using InputPixelType = float;
constexpr unsigned int Dimension = 3;
using InputImageType = itk::Image<InputPixelType, Dimension>;
using OutputPixelType = unsigned char;
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
//创建读取DCM的GDCMIOImage类
using ImageIOType = itk::GDCMImageIO;
ImageIOType::Pointer dcmImageIO = ImageIOType::New();
// 定义图像映射类,将InputImage强制类型转换为OutputInage
using CastingFilterType =
itk::CastImageFilter<InputImageType, OutputImageType>;
CastingFilterType::Pointer caster = CastingFilterType::New();
// 实例化文件读写类
using ReaderType = itk::ImageFileReader<InputImageType>;
using WriterType = itk::ImageFileWriter<OutputImageType>;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName("K:\\dicom\\IMG-0064.dcm");
writer->SetFileName("J:\\result.DCM");
// 设置读写I/O流类型
reader->SetImageIO(dcmImageIO);
writer->SetImageIO(dcmImageIO);
// 在进行区域扩散前对图像进行平滑处理,减小噪声的污染
using CurvatureFlowImageFilterType =
itk::CurvatureFlowImageFilter<InputImageType, InputImageType>;
CurvatureFlowImageFilterType::Pointer smoothing =
CurvatureFlowImageFilterType::New();
// 连通阈值区域扩散类实例化
using ConnectedFilterType =
itk::ConnectedThresholdImageFilter<InputImageType, InputImageType>;
ConnectedFilterType::Pointer connectedThreshold =
ConnectedFilterType::New();
smoothing->SetInput(reader->GetOutput());
connectedThreshold->SetInput(smoothing->GetOutput());
caster->SetInput(connectedThreshold->GetOutput());
writer->SetInput(caster->GetOutput());
smoothing->SetNumberOfIterations(5);
smoothing->SetTimeStep(0.125);
// 设置区域生长的阈值区间
connectedThreshold->SetLower(lowerThreshold);
connectedThreshold->SetUpper(upperThreshold);
connectedThreshold->SetReplaceValue(255);
// 设置种子点坐标
InputImageType::IndexType index;
index[0] = pos[0];
index[1] = pos[1];
index[2] = pos[2];
connectedThreshold->SetSeed(index);
try
{
writer->Update();
}
catch (const itk::ExceptionObject& excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
return EXIT_SUCCESS;
}
然后程序在writer->Update();代码内部直接报错:
有大神能帮我看看问题出在哪嘛。