Click here to Skip to main content
14,383,854 members
Rate this:
Please Sign up or sign in to vote.
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;

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");


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
Posted
Updated 21-Jun-16 5:00am
v2
Comments
KarstenK 15-Jun-16 12:34pm
   
That isnt a good idea. The header data and file extension are used to identify the data type. If your bits are misinterpreted you earn garbage instead of a picture :-(
AvPai 16-Jun-16 0:15am
   
So what do you propose? How do I overwrite the pixel data?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Comments
AvPai 22-Jun-16 1:56am
   
Which header data tags should I recalculate?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100