小言_互联网的博客

为什么把一个序列的DICOM数据有short型转化成 unsigned char型以后,重建失败

484人阅读  评论(0)

  
  1. #include <vtkAutoInit.h>
  2. VTK_MODULE_INIT(vtkRenderingOpenGL2)
  3. VTK_MODULE_INIT(vtkInteractionStyle)
  4. VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
  5. VTK_MODULE_INIT(vtkRenderingFreeType)
  6. #include "itkImage.h"
  7. #include "itkImageFileReader.h"
  8. #include "itkImageToVTKImageFilter.h"
  9. #include "vtkImageViewer.h"
  10. #include "vtkWin32RenderWindowInteractor.h"
  11. #include "itkResampleImageFilter.h"//采样
  12. #include "itkBinaryThresholdImageFilter.h"//二值化
  13. #include "itkThresholdImageFilter.h"//阈值分割
  14. #include "itkBinaryBallStructuringElement.h"
  15. #include "itkImageFileWriter.h"
  16. #include "itkCastImageFilter.h"
  17. #include "itkGDCMImageIOFactory.h"
  18. #include <iostream>
  19. #include <string>
  20. using namespace std;
  21. int main(int argc, char* argv[])
  22. {
  23. typedef itk::Image< unsigned char, 2> ImageType;
  24. typedef itk::Image< unsigned char, 2> UnsignedCharImageType;
  25. typedef itk::ImageFileReader<ImageType> ReaderType;
  26. typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
  27. std:: string DCM = ".DCM";
  28. itk::GDCMImageIOFactory::RegisterOneFactory();
  29. for ( int num = 1; num< 118; num++)
  30. {
  31. std:: string inputFilename = "E:\\imgdata\\2013-06-04\\1.2.840.113619.2.55.3.2831155457.416.1370299545.455";
  32. std:: string outputFilename = "D:\\IM\\1.2.840.113619.2.55.3.2831155457.416.1370299545.455";
  33. char t[ 256];
  34. string s;
  35. sprintf_s(t, ".%d", num);
  36. s = t;
  37. inputFilename = inputFilename + s + DCM;
  38. outputFilename = outputFilename + s + DCM;
  39. struct stat buffer;
  40. if (stat(inputFilename.c_str(), &buffer) != 0)
  41. continue;
  42. ReaderType::Pointer reader = ReaderType::New();
  43. ConnectorType::Pointer connector = ConnectorType::New();
  44. reader->SetFileName(inputFilename);
  45. //reader->SetImageIO( );
  46. //itk::GDCMImageIOFactory::RegisterOneFactory();
  47. reader->Update();
  48. ImageType::SizeType inputSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
  49. std:: cout << "Original size: " << reader->GetOutput()->GetLargestPossibleRegion().GetSize() << std:: endl;
  50. // Resize
  51. ImageType::SizeType outputSize;
  52. outputSize[ 0] = inputSize[ 0];
  53. outputSize[ 1] = inputSize[ 1];
  54. ImageType::SpacingType outputSpacing;
  55. outputSpacing[ 0] = reader->GetOutput()->GetSpacing()[ 0];
  56. outputSpacing[ 1] = reader->GetOutput()->GetSpacing()[ 1];
  57. typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> FilterType;
  58. FilterType::Pointer Thresholdfilter = FilterType::New();
  59. Thresholdfilter->SetInput(reader->GetOutput());
  60. Thresholdfilter->SetLowerThreshold( 100);
  61. Thresholdfilter->SetUpperThreshold( 180);
  62. //默认设置 SetInsideValue 255 SetOutsideValue 0
  63. Thresholdfilter->Update();
  64. /* typedef itk::BinaryBallStructuringElement<ImageType::PixelType, ImageType::ImageDimension>
  65. StructuringElementType;
  66. StructuringElementType structuringElement;
  67. structuringElement.SetRadius(8);
  68. structuringElement.CreateStructuringElement();
  69. typedef itk::BinaryMorphologicalOpeningImageFilter <ImageType, ImageType, StructuringElementType>
  70. BinaryMorphologicalOpeningImageFilterType;
  71. BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter
  72. = BinaryMorphologicalOpeningImageFilterType::New();
  73. openingFilter->SetInput(Thresholdfilter->GetOutput());
  74. openingFilter->SetKernel(structuringElement);
  75. openingFilter->Update();
  76. */
  77. typedef itk::CastImageFilter<ImageType, UnsignedCharImageType > CastFilterType;
  78. CastFilterType::Pointer castFilter = CastFilterType::New();
  79. castFilter->SetInput(Thresholdfilter->GetOutput());
  80. typedef itk::ImageFileWriter< UnsignedCharImageType > WriterType;
  81. WriterType::Pointer writer = WriterType::New();
  82. writer->SetFileName(outputFilename);
  83. writer->SetInput(castFilter->GetOutput());
  84. writer->Update();
  85. }
  86. return 0;
  87. }

 


转载:https://blog.csdn.net/laziji/article/details/104415896
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场