typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory("E:/imgdata/2017-07-05");
const ReaderType::FileNamesContainer filenames = namesGenerator->GetInputFileNames();
std::size_t numberOfFileNames = filenames.size();
std::cout << numberOfFileNames << std::endl;
for (unsigned int fni = 0; fni < numberOfFileNames; ++fni)
{
std::cout << filenames[fni] << std::endl;
//ITK读取数据
typedef itk::ImageFileReader<ImageType> ReaderType;
//读取指针
ReaderType::Pointer reader = ReaderType::New();
//reader->SetFileName(filenames[fni].c_str());
reader->SetFileName(filenames[fni]);
//创建读取DCM的GDCMIOImage类
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetImageIO(dicomIO);
try
{
reader->Update();
}
catch (itk::ExceptionObject& e)
{
std::cerr << "exception in file reader" << std::endl;
std::cerr << e << std::endl;
}
typedef itk::MetaDataDictionary DictionaryType;
const DictionaryType& dict1 = dicomIO->GetMetaDataDictionary();
m_pCastFilter->SetInput(reader->GetOutput());
//1.高斯去噪
//2.灰度拉伸
m_rescaleFliter->SetInput(m_pCastFilter->GetOutput());
m_rescaleFliter->SetOutputMaximum(255);
m_rescaleFliter->SetOutputMinimum(0);
m_rescaleFliter->Update();
//3.阈值分割
m_pBinaryThresholdImageFilter->SetInput(m_rescaleFliter->GetOutput());
m_pBinaryThresholdImageFilter->SetOutsideValue(0);
m_pBinaryThresholdImageFilter->SetInsideValue(255);
m_pBinaryThresholdImageFilter->SetLowerThreshold(180);
m_pBinaryThresholdImageFilter->SetUpperThreshold(255);
//4.显示处理后的图片
//数据转换为signed short类型
m_pCast2Short->SetInput(m_pBinaryThresholdImageFilter->GetOutput());//m_pOtsuThresholdImageFilter->GetOutput()
m_pCast2Short->Update();
//链接滤波器,转换为VTK类型
m_pConnector->SetInput(m_pCast2Short->GetOutput());
try
{
m_pConnector->Update();
}
catch (itk::ExceptionObject& e)
{
std::cerr << "exception in file reader" << std::endl;
std::cerr << e << std::endl;
}
//显示图像
vtkSmartPointer<vtkImageActor> actor = vtkImageActor::New();
actor->SetInputData(m_pConnector->GetOutput());
actor->InterpolateOff();
actor->Update();
m_pRender2D->AddActor(actor);
m_pImageViewer->GetImageActor()->InterpolateOff();
m_pRender2D->ResetCamera();
m_pRender2D->DrawOn();
ui.m_qvtk2DWidget->GetRenderWindow()->Render();
Sleep(200);
m_pRender2D->RemoveActor2D(actor);
//把文件写入文件夹
using OutputPixelType = signed short;
constexpr unsigned int OutputDimension = 3;
typedef itk::Image< OutputPixelType, OutputDimension > WriteImageType;
typedef itk::ImageFileWriter< WriteImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
using Image2DType = itk::Image<OutputPixelType, OutputDimension>;
writer->SetInput(m_pCast2Short->GetOutput());
char drive[_MAX_DRIVE] = { 0 };
char dir[_MAX_DIR] = { 0 };
char fname[_MAX_FNAME] = { 0 };
char ext[_MAX_EXT] = { 0 };
_splitpath(filenames[fni].c_str(), drive, dir, fname, ext);
std::string filename = "E:/imgdata/2020-10-09/";
filename += fname; filename += ext;
writer->SetFileName(filename);
writer->SetImageIO(dicomIO);
writer->UseInputMetaDataDictionaryOff();
dicomIO->KeepOriginalUIDOn();//此句非常重要,没有此句话,图像序列被破坏
// writer->SetUseInputMetaDataDictionary(m_pCast2Short->GetMetaDataDictionary());
writer->SetMetaDataDictionary(dict1);
try
{
writer->Update();
}
catch (itk::ExceptionObject& e)
{
std::cerr << "exception in file reader" << std::endl;
std::cerr << e << std::endl;
}
}