Convert BMP Image to DICOM Image

450 views
Skip to first unread message

harshitha

unread,
May 12, 2021, 1:13:19 AM5/12/21
to dcm4che
Hello All,

NEED URGENT HELP!!!!

DCM4CHE only provides support to convert a JPEG image to DICOM using a JPEG Parser. Is there any way to convert a BMP image directly to a DICOM image?

Todd Jensen

unread,
May 12, 2021, 9:47:01 AM5/12/21
to dcm4che
If using the command line, may be easier to convert the BMP to JPEG first using something like ImageMagick:

yeddula harshitha

unread,
May 12, 2021, 10:34:50 AM5/12/21
to dcm...@googlegroups.com
I don't want to convert BMP to any intermediate type. Is there any way for direct conversion?

--
You received this message because you are subscribed to a topic in the Google Groups "dcm4che" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dcm4che/bPC5S-J3pGg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dcm4che+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dcm4che/d8c428c8-322c-4b97-951e-1d92083c339cn%40googlegroups.com.

Tatsunidas

unread,
May 12, 2021, 7:45:24 PM5/12/21
to dcm...@googlegroups.com
1.
A simple and easy way is to use the 3D slicer.
It can convert general images to dicom secondary capture with some primary attributes.

2.
If you would contribute/try programming,
for example (This procedure is not strict.),
//get image pixels as byte[]
BufferedImage bmp = ImageIO.read(new File("your.bmp"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bmp, "bmp", baos );
byte[] pixels = baos.toByteArray();
baos.flush();
baos.close();
//get dicom attr reference to copy
DicomInputStream dis = new DicomInputStream(new FIle(path2ReferenceDcm));
Attributes refDcm4AttrCopy = dis.readDataset();
dis.close();
//set pixel
refDcm4AttrCopy.setValue(Tag.PixelData, VR.OW, new BulkData(pixels)); //should check VR.OB or OW.
//set attr values related to pixel
refDcm4AttrCopy.setInt(Tag.Rows, VR.US, bmp.getHeight());
refDcm4AttrCopy.setInt(Tag.Columns, VR.US, bmp.getWidth());
refDcm4AttrCopy.setInt(Tag.BitsAllocated, ...)
refDcm4AttrCopy.setInt(Tag.BitsStored, ...)//BitsStored - 1
refDcm4AttrCopy.setInt(Tag.HighBit, ...)
refDcm4AttrCopy.setInt(Tag.PixelRepresentation, ...)
refDcm4AttrCopy.setInt(Tag.SamplesPerPixel, ...)
refDcm4AttrCopy.setInt(Tag.PhotometricInterpretation, ...) 
// set secondary capture sop class uid
refDcm4AttrCopy.setString(Tag.SOPClassUID, ...)
//set uids
refDcm4AttrCopy.setString(Tag.StudyInstanceUID...);
refDcm4AttrCopy.setString(Tag.SeriesInstanceUID...);
refDcm4AttrCopy.setString(Tag.SOPInstanceUID...);
//create file meta info
Attributes fmi = refDcm4AttrCopy.createFileMetaInfomation(Tag.SOPClassUID);
//output
DicomOutputStream dos = new DicomOutputStream(new File("path2BmpAsDicom.dcm"));
dos.writeFileMetaInformation(fmi);
attribs.writeTo(dos);
dos.close();

You can keep in mind,
- Is that bmp compressed or not ? if compressed, decompress it first, then use it as ExplicitLittleEndian for TSUID.
- Is multiframe ? (movies ?)
- gray or rgb ? if rgb, what is order ? (which rrr...ggg...bbb... or rgbrgbrgb...), if grascale, has look up tables ?
- bit-depth ?  (8 bit or 16 bit or more grayscale?,  or 24 bit , 32 bit rgb ?)


I hope this helps.
(Notice, I am not dcm4che developer)

2021年5月12日(水) 14:13 harshitha <mail2h...@gmail.com>:
Hello All,

NEED URGENT HELP!!!!

DCM4CHE only provides support to convert a JPEG image to DICOM using a JPEG Parser. Is there any way to convert a BMP image directly to a DICOM image?

--
You received this message because you are subscribed to the Google Groups "dcm4che" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dcm4che+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dcm4che/34411433-f779-48c9-af05-4acdf7d89bd6n%40googlegroups.com.


--
==========================================
Tatsuaki KOBAYASHI
TEL:080-1274-6433
Good Luck
==========================================

Tatsunidas

unread,
May 12, 2021, 7:49:14 PM5/12/21
to dcm...@googlegroups.com
Sorry, this is my typo,

//create file meta info
//wrong
Attributes fmi = refDcm4AttrCopy.createFileMetaInfomation(Tag.SOPClassUID);
//correct
Attributes fmi = refDcm4AttrCopy.createFileMetaInfomation(TransferSyntaxUIDOnYourNeed);

2021年5月13日(木) 8:45 Tatsunidas <chok...@gmail.com>:

harshitha

unread,
May 13, 2021, 2:05:19 AM5/13/21
to dcm4che
NEED URGENT HELP!!!!!

When I did this..
refDcm4AttrCopy.setValue(Tag.PixelData, VR.OW, new BulkData(pixels)); //should check VR.OB or OW.
I was not able to open the DCM image..

FileInputStream fis = new FileInputStream(input_file);  
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));

         byte[] buffer = new byte[8162];  
         int read;
         while ((read = dis.read(buffer)) > 0) {
             dos.write(buffer, 0, read);
         }
But the final image is empty as attached....
Capture.PNG

Tatsunidas

unread,
May 13, 2021, 3:24:52 AM5/13/21
to dcm...@googlegroups.com
Umm, sorry, BulkData needs only DicomInputStream.

try it,
//refDcm4AttrCopy.setValue(Tag.PixelData, VR.OW, new BulkData(pixels)); //should check VR.OB or OW.  
//instead
ByteBuffer byteBuffer = ByteBuffer.allocate(pixels.length);
byteBuffer.order(ByteOrder.nativeOrder()).put(pixels);
refDcm4AttrCopy.setBytes(Tag.PixelData, VR.OW, byteBuffer.array());

2021年5月13日(木) 15:05 harshitha <mail2h...@gmail.com>:

harshitha

unread,
May 13, 2021, 6:47:32 AM5/13/21
to dcm4che
Hi ,
I tried this, there are no errors and when I tried opening the dcm message, I am not able to open it.

Tatsunidas

unread,
May 13, 2021, 8:37:31 AM5/13/21
to dcm...@googlegroups.com
Could you attach your sample bmp ?


2021年5月13日(木) 19:47 harshitha <mail2h...@gmail.com>:

yeddula harshitha

unread,
May 13, 2021, 8:55:14 AM5/13/21
to dcm...@googlegroups.com
Please find the attached BMP file

You received this message because you are subscribed to a topic in the Google Groups "dcm4che" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dcm4che/bPC5S-J3pGg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dcm4che+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dcm4che/CADsnaZeVUxYSQjyKCCPXqbB%3D-htNWEHryyrxOK%2B3znPQ6771Hw%40mail.gmail.com.
sampleBmpImage.bmp

Tatsunidas

unread,
May 13, 2021, 8:22:02 PM5/13/21
to dcm...@googlegroups.com
 simple example, how about this ?

2021年5月13日(木) 21:55 yeddula harshitha <mail2h...@gmail.com>:
sample_bmp_rgb.dcm

yeddula harshitha

unread,
May 13, 2021, 11:01:55 PM5/13/21
to dcm...@googlegroups.com
Hi,
I am able to view the DICOM image and also the DICOM tags. Can you please share the code, so that I can verify my mistake?



harshitha

unread,
May 25, 2021, 12:35:51 AM5/25/21
to dcm4che
  Hi,
I am able to view the DICOM image and also the DICOM tags. Can you please share the code, so that I can verify my mistake?


harshitha

unread,
May 28, 2021, 10:56:58 PM5/28/21
to dcm4che
Hi,

//retrieve the pixel and header information
BufferedImage bmp = ImageIO.read(new File("your.bmp"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bmp, "bmp", baos );
byte[] pixels = baos.toByteArray();

BMP Image has a header component included. So the variable, pixels will contain header data along with the pixel data.
//following code retrieves only the pixel data
pixels = ((DataBufferByte)bmpImage.getData().getDataBuffer()).getData();

BMP image scanning is different as compared to other image scanning. Refer to https://medium.com/sysf/bits-to-bitmaps-a-simple-walkthrough-of-bmp-image-format-765dc6857393 for more information.
The pixel information retrieved is in the (B,G,R) format. It needs to be modified to (R,G,B)
//converting (B,G,R) to (R,G,B), B & R needs to be swapped
byte temp;
for(int i=0; i< pixels.length ; i=i+3) {
     tmp = pixels[i];
     pixels[i] = pixels[i+2];
     pixels[i+2] = tmp;
 }

//pixel converted to ByteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate(pixels.length);
byteBuffer = byteBuffer.order(ByteOrder.nativeOrder()).put(pixels);

//setting the pixel information to the Pixel data Tag
Attributes dataset = new Attributes();
dataset.setBytes(Tag.PixelData, VR.OB,  byteBuffer .array());
// set other tags based on the requirement

//create dicom file
try(DicomOutputStream dos = new DicomOutputStream
(new File(dicomFilePath))
{
dos.writeFileMetaInformation(
    fileMetadata.createFileMetaInformation("1.2.840.10008.1.2.1")); // Explicit VR little Endian, transfer syntax is used
dataset.writeTo(dos); //write dataset to dicomOutputStream
        dos.flush();
    
} catch (IOException e) {
log.error("Error while writing to DICOM File!!");
e.printStackTrace();
}




Tatsunidas

unread,
May 28, 2021, 11:51:19 PM5/28/21
to dcm...@googlegroups.com
Awesome.

2021年5月29日(土) 11:57 harshitha <mail2h...@gmail.com>:


--
==========================================
Tatsuaki KOBAYASHI
==========================================
Reply all
Reply to author
Forward
0 new messages