如何使用bmp格式的另一个图像覆盖dicom数据文件中的图像 [英] How do I overwrite an image in dicom datafile with another one in which is in bmp format

查看:151
本文介绍了如何使用bmp格式的另一个图像覆盖dicom数据文件中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图覆盖dicom图像数据集的像素数据,并将其替换为bmp格式的另一个灰度图像。我希望所有其他标记保持不变。

我尝试了两种方法: -



方法1-

我已成功将图像复制到新的dicom文件但不是标签。我使用的代码是:

Hi,
I am trying to overwrite the pixel data of the dicom image dataset and replace it with another grayimage which is in bmp format.I want all the other tags to remain as such.
I have tried two methods:-

Method 1-
I have successfully copied the image into a new dicom file but not the tags. The code I used is :

OFString pixDataFile, outputFile, tempStr;
	// Main class for controlling conversion
	Image2Dcm i2d;
	// Output plugin to use (ie. SOP class to write)
	I2DOutputPlug *outPlug = NULL;
	// Input plugin to use (ie. file format to read)
	I2DImgSource *inputPlug = NULL;
	// Group length encoding mode for output DICOM file
	E_GrpLenEncoding grpLengthEnc = EGL_recalcGL;
	// Item and Sequence encoding mode for output DICOM file
	E_EncodingType lengthEnc = EET_ExplicitLength;
	// Padding mode for output DICOM file
	E_PaddingEncoding padEnc = EPD_noChange;
	// File pad length for output DICOM file
	OFCmdUnsignedInt filepad = 0;
	// Item pad length for output DICOM file
	OFCmdUnsignedInt itempad = 0;
	// Write only pure dataset, i.e. without meta header
	E_FileWriteMode writeMode = EWM_dataset;
	// Override keys are applied at the very end of the conversion "pipeline"
	OFList<OFString> overrideKeys;
	// The transfersytanx proposed to be written by output plugin
	E_TransferSyntax writeXfer;

	pixDataFile = in;
	outputFile = "pai.dcm";
	inputPlug = new I2DBmpSource();
	outPlug = new I2DOutputPlugSC();
	grpLengthEnc = EGL_recalcGL;
	lengthEnc = EET_ExplicitLength;

	/*OFCmdUnsignedInt opt_filepad;
	OFCmdUnsignedInt opt_itempad;

	itempad = opt_itempad;
	filepad = opt_filepad;*/


	I2DImgSource *bmpSource = OFstatic_cast(I2DImgSource*, inputPlug);
	inputPlug->setImageFile(pixDataFile);

	OFCondition result = i2d.convert(inputPlug, outPlug, dataset, writeXfer);

	// Saving output DICOM image 
	if (result.good())
	{
		DcmFileFormat dcmff(dataset);
		result = dcmff.saveFile(outputFile.c_str(), writeXfer, lengthEnc, grpLengthEnc, padEnc, filepad, itempad, writeMode);

	}
	// Cleanup and return
	outPlug = NULL;
	inputPlug = NULL;
	dataset = NULL;



这里输入和输出显示输入和输出图像



方法2: -



我已成功将所有标签复制到新的dicom图像中,但无法替换像素数据。我想知道如何在openCV中以我可以使用的格式提取灰度图像的像素数据: -


Here the in and out shows the input and output images

Method 2 :-

I have successfully copied all the tags into a new dicom image but was not able to replace the pixel data. I would like to know how to extract the pixel data of grayscale image in openCV in a format that I could use for this :-

dataset_dst = dst_fileformat.getDataset();
		// Use DcmDataset's assignment operator to copy the complete dataset
		*dataset_dst = *dataset_src;///1st one is the dataset of the new dicom while the secon one is the dataset of the source dicom

		dataset_dst->putAndInsertUint8Array(DCM_PixelData, pixeldata);//how to get pixeldata from bmp grayscale??
		dst_fileformat.saveFile("new.dcm");





这是我尝试过的最新消息: -



This is the latest I tried:-

dataset_dst = dst_fileformat.getDataset();
     *dataset_dst = *dataset_src;// dataset_src is the dataset of the original dicom file
                 Mat input= imread("image.bmp");
      Uint16 * pixel;
     const unsigned long  size = input.size().height* input.size().width ;
     pixel = ( Uint16 *)malloc(size);

     for (int i = 0; i < input.rows; i++)
     {
        for (int j = 0; j < input.cols; j++)
        {
           *pixel = (Uint16)input.at<uchar>(i, j);
           cout << *pixel;
        }
     }
     dataset_dst->putAndInsertUint16Array(DCM_PixelData, pixel,size, true);/// line for replacement
     dst_fileformat.saveFile("new.dcm");





但是像@KarstenK警告我的那样,我得到了垃圾数据。我应该如何以及在标题和元数据中做出哪些更改才能获得正确的图像?



我尝试了什么:



我尝试的方法如上所示。请仔细研究并帮助



But like @KarstenK warned me, I get garbage data. How and what changes should I make in the header and metadata to get a proper image?

What I have tried:

Al the methods I have tried is shown above. Kindly look into it and help

推荐答案

首先:编写一个新文件,最好使用带有%file%_格式.bmp的文件名。 。这是最佳实践,因为您不会覆盖输入数据以供以后使用。



在第二个写入转换后的数据时,您必须重新计算标题数据并将其写入正确的格式。



为了更深入地了解图像转换和其他所需的细节,我建议杰出的 CxImage文章 OpenCV样本



提示:如果您编写文件,请使用始终是完整路径而不仅仅是文件名。
At first: write a new file, at best with a filename with the patren "%file%_"format".bmp". It is best practice because you dont overwrite your input data for later use.

At the second to write the converted data you must recompute the header data and write it in the correct format.

For deeper insights in image transformation and other needed details I advise the outstanding CxImage article and the OpenCV samples.

Tip: if you write files use always a full path and not only the file name.


这篇关于如何使用bmp格式的另一个图像覆盖dicom数据文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆