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;
Image2Dcm i2d;
I2DOutputPlug *outPlug = NULL;
I2DImgSource *inputPlug = NULL;
E_GrpLenEncoding grpLengthEnc = EGL_recalcGL;
E_EncodingType lengthEnc = EET_ExplicitLength;
E_PaddingEncoding padEnc = EPD_noChange;
OFCmdUnsignedInt filepad = 0;
OFCmdUnsignedInt itempad = 0;
E_FileWriteMode writeMode = EWM_dataset;
OFList<OFString> overrideKeys;
E_TransferSyntax writeXfer;
pixDataFile = in;
outputFile = "pai.dcm";
inputPlug = new I2DBmpSource();
outPlug = new I2DOutputPlugSC();
grpLengthEnc = EGL_recalcGL;
lengthEnc = EET_ExplicitLength;
I2DImgSource *bmpSource = OFstatic_cast(I2DImgSource*, inputPlug);
inputPlug->setImageFile(pixDataFile);
OFCondition result = i2d.convert(inputPlug, outPlug, dataset, writeXfer);
if (result.good())
{
DcmFileFormat dcmff(dataset);
result = dcmff.saveFile(outputFile.c_str(), writeXfer, lengthEnc, grpLengthEnc, padEnc, filepad, itempad, writeMode);
}
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();
*dataset_dst = *dataset_src;
dataset_dst->putAndInsertUint8Array(DCM_PixelData, pixeldata); dst_fileformat.saveFile("new.dcm");
This is the latest I tried:-
dataset_dst = dst_fileformat.getDataset();
*dataset_dst = *dataset_src; 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); 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