最近项目要求解析dicom格式的影像文件,并将解析的图像本地保存jpg格式,将这几天查询的资料和个人实现代码共享给大家。
开发环境 win10 + vtk7.0 + itk4.11 + qt5.5 + vs2010
关于框架的编译以下链接可供参考:
VTK 编译参考: http://qimo601.iteye.com/blog/1553222 http://blog.csdn.net/wanhongluli/article/details/77852843
ITK编译参考: https://wenku.baidu.com/view/99cfdb2559eef8c75fbfb3e3.html
在此默认已经配置好环境
//设置输入类型
typedef itk::Image<unsigned short,2> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
//设置输出类型
typedef unsigned char OutputPixelType;
const unsigned int OutputDimension = 2;
typedef itk::Image<OutputPixelType,OutputDimension> OutputImageType;
//获取DICOM文件读取对象
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
//ITK图像数据向VTK图像数据转换对象
typedef itk::ImageToVTKImageFilter<ImageType> FilterType;
FilterType::Pointer connector = FilterType::New();
//获得DICOM文件名并读取DICOM文件
char* fileName = "1.dcm";
reader->SetFileName(fileName);
reader->SetImageIO(gdcmImageIO);
reader->Update();
//像素灰度值反转
ImageType::Pointer image = reader->GetOutput();
ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
ImageType::IndexType index;
for(int x = 0; x <size[0];x++)
for(int y = 0;y < size[1];y++)
{
index[0] = x;
index[1] = y;
ImageType::PixelType value = image->GetPixel(index);
unsigned short test = 4096 - value;
image->SetPixel(index,test);
}
image->Update();
//设置窗位和窗宽
typedef itk::IntensityWindowingImageFilter <ImageType, ImageType> IntensityWindowingImageFilterType;
IntensityWindowingImageFilterType::Pointer intensityFilter=IntensityWindowingImageFilterType::New();
intensityFilter->SetInput(image);
intensityFilter->SetWindowMinimum(0);
intensityFilter->SetWindowMaximum(4096);
intensityFilter->SetOutputMinimum(0);
intensityFilter->SetOutputMaximum(255);
intensityFilter->Update();
//dcm转JPG
typedef itk::CastImageFilter<ImageType,OutputImageType> ImageCastType;
ImageCastType::Pointer imageCast = ImageCastType::New();
imageCast->SetInput(intensityFilter->GetOutput());
//将JPG格式的图像存储在本地
typedef itk::ImageFileWriter<OutputImageType> Writer1Type;
Writer1Type::Pointer writer1 = Writer1Type::New();
typedef itk::JPEGImageIO jpgType;
jpgType::Pointer jpgIO = jpgType::New();
writer1->SetImageIO(jpgIO);
writer1->SetFileName("tran.jpg");
writer1->SetInput(imageCast->GetOutput());
writer1->Update();
//ITK到VTK转化
connector->SetInput(image);
connector->Update();
//设置Y轴像素反转
vtkSmartPointer<vtkImageFlip> flip = vtkSmartPointer<vtkImageFlip>::New();
flip->SetInputData(connector->GetOutput());
flip->SetFilteredAxis(1);
flip->Update();
//显示图像
vtkImageViewer2 *viewer = vtkImageViewer2::New();
viewer->SetInputData(flip->GetOutput());
viewer->SetColorWindow(4096);
viewer->SetColorLevel(2048);
viewer->SetRenderWindow(ui.qvtkWidget->GetRenderWindow());
viewer->Render();