医学软件项目开发(一):使用ITK对DICOM文件进行相关操作

一、预定义

CT值的单位是Hounsfield,简称为Hu,范围是 -1024-3071。用于衡量人体组织对X射线的吸收率,设定水的吸收率为0Hu。

在DICOM图像读取的过程中,我们会发现图像的像素值有可能不是这个范围,通常是0-4096,这个值就是我们常见到的像素值或者灰度值。一般我们所处理的图象是经过量化后的灰度图象,标准的CT 灰度图象为12 位灰度图象。

8bite,16bites,32bites,64bites 对应每个像素值变量类型为char、short、int、long

opencv

using InputPixelType = unsigned char;//无符号八位,一般取signed short,unsigned short得到的是2^16大小的数据-1024,而使用signed short得到的数据才为正常
using OutputPixelType = unsigned char;
using InputImageType = itk::Image<InputPixelType, 2>;//二维
using OutputImageType = itk::Image<OutputPixelType, 2>;
typedef itk::ImageSeriesReader<InputImageType> ReaderType;//ImageFileReader也可
typedef itk::GDCMImageIO ImageIOType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileNames(filenamesOut);
//reader->SetFileName(argv[1]);//调试中的命令参数名字
ImageIOType::Pointer gdcmIO = ImageIOType::New();
reader->SetImageIO(gdcmIO);
reader->Update();
Mat S_OrgImg = itk::OpenCVImageBridge::ITKImageToCVMat< OutputImageType >(reader->GetOutput());//转换为Mat,默认无此函数,需重新配置ITK

重新配置ITK方法

double intercept = gdcmIO->GetRescaleIntercept();//截距
double slope = gdcmIO->GetRescaleSlope();//斜率
S_OrgImg = S_OrgImg + Scalar(-intercept);
S_OrgImg.convertTo(S_OrgImg, -1, 1 / slope, 0);//将Hu值转换为像素值

二、writer(项目中测试直接异常)

using WriterType = itk::ImageFileWriter< InputImageType >;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("current.png");
writer->SetInput(reader->GetOutput());
writer->Update();

三、窗宽窗位(ITK中调整窗宽窗位方法)

//ITK内窗宽窗位调整方法
typedef itk::IntensityWindowingImageFilter< InputImageType, OutputImageType > FilterType;
FilterType::Pointer filter = FilterType::New();
filter->SetWindowMinimum(a);
filter->SetWindowMaximum(b);
filter->SetOutputMinimum(0);
filter->SetOutputMaximum(255);
filter->SetInput(reader->GetOutput());
filter->Update();//转换为Mat时切记使用filter->GetOutput()。另Getpixel得到的直接是Hu值,利用filter调整的直接是Hu值

四、将cv::Mat保存为raw1.mat,然后拖拽到excel查看(改进方式:下载Image Watch插件)

	char filename[] = "D://raw1.mat";
	std::ofstream fout(filename);
	if (!fout) {

		return;
	}

	for (int i = 0; i < m1.rows; i++)
	{
		for (int j = 0; j < m1.cols; j++)
		{
			fout << m1.at<quint16>(i, j) << "\t";
		}
		fout << std::endl;
	}
	fout.close();

五、ITK可能用到的内容(获取信息判断类型)

//获取图像尺寸
InputImageType::SizeType imgSize = inputImage->GetLargestPossibleRegion().GetSize();
//获取gdcmIO图像大小
ImageIOType::SizeType imgsize;
imgsize = gdcmIO->GetImageSizeInPixels();
width,height = reader->GetOutput().GetSize()

标签(0028,0030)代表像素间距pixspacing

六、项目代码

	QString patientName = FindDicomTag("0010|0010", gdcmIO);
	QString patientSex = FindDicomTag("0010|0040", gdcmIO);
	QString patientBirth = FindDicomTag("0010|0030", gdcmIO);
	QString patientAge = FindDicomTag("0010|1010", gdcmIO);

	QString patientID = FindDicomTag("0010|0020", gdcmIO);
	QString studyDate = FindDicomTag("0008|0020", gdcmIO);
	QString patientSize = FindDicomTag("0010|1020", gdcmIO);
	QString patientWeight = FindDicomTag("0010|1030", gdcmIO);
	QString patientArea = FindDicomTag("0010|2160", gdcmIO);

	S_PatientInformation= QString(" Name:%1 Sex:%2 Birth:%3 Age:%4\n patientID:%5 studyDate:%6 Size:%7 Weight:%8 Area:%9\n").arg(patientName).arg(patientSex)
		.arg(patientBirth).arg(patientAge).arg(patientID).arg(studyDate).arg(patientSize)
		.arg(patientWeight).arg(patientArea);

你可能感兴趣的:(医学软件开发,opencv,qt,visual,studio,dicom)