pydicom / pydicom

Read, modify and write DICOM files with python code

Home Page:https://pydicom.github.io/pydicom/dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

convert jpeg to dcm(c++)

reality-h opened this issue · comments

this is my code:

void MainWin::convertJpegToDcm()
{
const QString& jpegFilePath = "E:\vs\CannonTestNew\CannonCameraTest\jpeg2dcm\cat.jpeg";
const QString& dicomFilePath = "E:\vs\CannonTestNew\CannonCameraTest\jpeg2dcm\cat.dcm";
QImage jpegImage(jpegFilePath);
if (jpegImage.isNull())
{
return;
}
int width = jpegImage.width();
int height = jpegImage.height();

DcmFileFormat fileFormat;
OFCondition status;

DcmDataset* dataset = fileFormat.getDataset();

char uid[100];
//dcmGenerateUniqueIdentifier(uid);

//插入必要的DICOM标准字段
dataset->putAndInsertString(DCM_SOPClassUID, UID_SecondaryCaptureImageStorage);
dataset->putAndInsertString(DCM_SOPInstanceUID, dcmGenerateUniqueIdentifier(uid));
dataset->putAndInsertUint16(DCM_SamplesPerPixel, 3);
dataset->putAndInsertString(DCM_PhotometricInterpretation, "YBR_FULL_422");


dataset->putAndInsertUint16(DCM_Rows, height);
dataset->putAndInsertUint16(DCM_Columns, width);
dataset->putAndInsertUint16(DCM_BitsAllocated,16);
dataset->putAndInsertUint16(DCM_BitsStored,16);
dataset->putAndInsertUint16(DCM_HighBit, 15);
dataset->putAndInsertUint16(DCM_PixelRepresentation, 0);
dataset->putAndInsertUint16(DCM_PlanarConfiguration, 0);
if (dataset) {
    dataset->putAndInsertString(DCM_PatientName, "John Doe");
    dataset->putAndInsertString(DCM_PatientID, "12345");
    dataset->putAndInsertString(DCM_SeriesDescription, "DICOM Series");

    // Set image pixel data (assuming 8-bit grayscale image)
    //const unsigned char* pixelData = jpegImage.bits();
    //Uint16* pixelData = (Uint16*)jpegImage.getOutputData(16);
    Uint16* pixelData = (Uint16*)jpegImage.bits();
    dataset->putAndInsertUint16Array(DCM_PixelData, pixelData, width * height);
}

status = fileFormat.saveFile(dicomFilePath.toStdString().c_str(), EXS_LittleEndianExplicit);
if (!status.good()) {
    std::cout << "Failed to save DICOM file:" << status.text() << endl;

}

}
The original image is:
image
The image saved by the aforementioned code is as follows:
image
However, changing the code from dataset->putAndInsertUint16Array(DCM_PixelData, pixelData, width * height); to dataset->putAndInsertUint16Array(DCM_PixelData, pixelData, width * height * 2); results in the image shown below:
image
What problem has occurred here, and how can I get a proper image?

This belongs to dcmtk, not to pydicom - please ask that in their repo.