DCMTK读取dcm图片+opencv显示图片

读取DCM格式图片中的一些基本信息

DcmFileFormat是最基本的文件对象

OFCondition是每一次操作的返回值,用来判断操作是否成功

所有的数据都存在DcmDataSet对象中,用getDataSet方法得到

void LoadDcmFile()
{
    std::string file_path = "IM00001";	//dcm文件

	DcmFileFormat fileformat;
	OFCondition oc = fileformat.loadFile(file_path.c_str());	//读取Dicom图像
	if (!oc.good())		//判断Dicom文件是否读取成功
	{
		std::cout << "file Load error" << std::endl;
		return;
	}
	DcmDataset *dataset = fileformat.getDataset();	//得到Dicom的数据集,所有的数据都存储在数据集当中
	E_TransferSyntax xfer = dataset->getOriginalXfer();	//得到传输语法

	OFString patientname;
	dataset->findAndGetOFString(DCM_PatientName, patientname);	//获取病人姓名     //dataset->findAndGetOFString/->findAndGetUint16
	cout << "patientName :" << patientname << endl;

	unsigned short bit_count(0);
	dataset->findAndGetUint16(DCM_BitsStored, bit_count);	//获取像素的位数 bit
	cout << "bit_count :" << bit_count << endl;

	OFString isRGB;
	dataset->findAndGetOFString(DCM_PhotometricInterpretation, isRGB);//DCM图片的图像模式
	cout << "isrgb :" << isRGB << endl;

	unsigned short img_bits(0);
	dataset->findAndGetUint16(DCM_SamplesPerPixel, img_bits);	//单个像素占用多少byte
	cout << "img_bits :" << img_bits << endl;
	//DicomImage* img_xfer = new DicomImage(xfer, 0, 0, 1);		//由传输语法得到图像的帧

	unsigned short m_width;		//获取图像的窗宽高
	unsigned short m_height;
	dataset->findAndGetUint16(DCM_Rows, m_height);
	dataset->findAndGetUint16(DCM_Columns, m_width);
	cout << "width :" << m_width << endl;
	cout << "height " << m_height << endl;

        unsigned short center, width;  //获取源图像中的窗位和窗宽
	dataset->findAndGetUint16(DCM_WindowCenter, center);
	dataset->findAndGetUint16(DCM_WindowWidth, width);
    
        DcmElement* element = NULL;    //读取dcm中的像素值
	OFCondition result = dataset->findAndGetElement(DCM_PixelData, element);
	if (result.bad() || element == NULL)
}

 

使用Dcmtk读取dcm图片并且使用opencv显示,这边读取方式和上面有一些区别,上面用的是DcmFileFormat,这边用的是DicomImage,其中设置窗宽和窗位的步骤是十分必要的,否则会出现图像一片黑的情况。

void LoadDcmFile4()
{
	DicomImage *img = new DicomImage("E:\\CT\\ST00001\\SE00001\\IM00020");
	int nWidth = img->getWidth();			//获得图像宽度
	int nHeight = img->getHeight();			//获得图像高度
	img->setWindow(100, 400);  //肝脏一般取窗宽为450HU,窗位为45HU,这边的窗宽和窗位要自己设定
	Uint16 *pixelData = (Uint16*)(img->getOutputData(16));
	std::cout << nWidth << ", " << nHeight << std::endl;
	if (pixelData != NULL)
	{
		//cv::Mat dst2(nWidth, nHeight, CV_16UC1, cv::Scalar::all(0));
		//unsigned short* data = nullptr;
		//for (int i = 0; i < nHeight; i++)
		//{
		//	data = dst2.ptr(i);	//取得每一行的头指针 也可使用dst2.at(i, j)
		//	for (int j = 0; j < nWidth; j++)
		//	{
		//		*data++ = pixelData[i*nWidth + j];
		//		 //cout << *data << " ";
		//		//cout << dst2.at(i, j) << " ";
		//	}
		//}
		Mat dst(nWidth, nHeight, CV_16UC1, pixelData);
		cv::imshow("image2", dst);
		cv::waitKey(0);
	}
}

 

你可能感兴趣的:(DCMTK)