Creating an encapsulated multi frame dicom file with ruby-dicom

195 views
Skip to first unread message

Christoffer Lervåg

unread,
Nov 30, 2012, 7:09:22 AM11/30/12
to ruby-...@googlegroups.com
Hi All

A user recently asked me how he could create an encapsulated multi fram dicom file using ruby-dicom, as this hasn't been demonstrated in any of the tutorials or in this group before. After a little trial and error, we managed to work out a solution which produces a DICOM file that is viewable. As it turns out, we don't have a convenience method for this kind of thing in ruby-dicom (perhaps we should have - an idea for future implementation perhaps?!) Anyway, in case anyone else should ever need to do this, I will post our solution here for reference. Note that this was performed with ruby-dicom version 0.9.4.

Best regards,
Chris


# Load needed libraries:
require 'dicom'
require 'RMagick'
puts DICOM::VERSION
include DICOM

# Source image files:
files = Array.new
images = Array.new
files << './frame1.jpg'
files << './frame2.jpg'
files << './frame3.jpg'
files.each do |file|
  images << Magick::ImageList.new(file)
end

# Create DICOM object and tags:
dcm = DObject.new
dcm.transfer_syntax = '1.2.840.10008.1.2.4.50'
dcm.sop_class_uid = '1.2.840.10008.5.1.4.1.1.3.1'
dcm.sop_instance_uid = DICOM.generate_uid
dcm.study_date = Time.now.strftime('%Y.%m.%d')
dcm.study_time = Time.now.strftime('%H:%M:%S')
dcm.modality = 'US'
dcm.manufacturer = 'ruby-dicom'
dcm.patients_name = 'Test'
dcm.patient_id = '12345'
dcm.study_instance_uid = DICOM.generate_uid
dcm.series_instance_uid = DICOM.generate_uid
dcm.study_id = '1'
dcm.series_number = '32'
dcm.instance_number = '142'
dcm.image_position_patient = "0.0\\0.0\\0.0"
dcm.image_orientation_patient = "1\\0\\0\\0\\1\\0"
dcm.frame_of_reference_uid = DICOM.generate_uid
dcm.samples_per_pixel = 1
dcm.photometric_interpretation = 'MONOCHROME1'
dcm.number_of_frames = images.length
dcm.rows = 400
dcm.columns = 400
dcm.series_instance_uid = "1.0\\1.0"
dcm.bits_allocated = 8
dcm.bits_stored = 8
dcm.high_bit = 7
dcm.pixel_representation = 0

# Create pixel tags/items:
pixel_data_seq = Sequence.new('7FE0,0010', :parent => dcm, :vr => 'OB')
basic_offset_table_item = Item.new(:length => 0, :parent => pixel_data_seq)
images.each do |image|
  frame_item = Item.new(:bin => image.to_blob, :parent => basic_offset_table_item)
end

# Write DICOM object to file:
dcm.write('multi_frame.dcm')

Christoffer Lervåg

unread,
Apr 12, 2013, 4:10:29 AM4/12/13
to ruby-...@googlegroups.com
I was presented with a problem by another user which is quite similar to the one given above, so I thought I'd post the solution here as well, to the benefit of everyone. The problem is this: How to create a single frame dicom file from a color jpg image? A working script is given below (this was tested with ruby-dicom 0.9.5 and RMagick 2.13.2).

Regards,
Chris


require 'dicom'
require 'RMagick'

include DICOM
include Magick

image = Image.read('color.jpg').first


# Create DICOM object and tags:
dcm = DObject.new
dcm.transfer_syntax = '1.2.840.10008.1.2.4.50'
dcm.sop_class_uid = '1.2.840.10008.5.1.4.1.1.3.1'
dcm.sop_instance_uid = DICOM.generate_uid
dcm.study_date = Time.now.strftime('%Y.%m.%d')
dcm.study_time = Time.now.strftime('%H:%M:%S')
dcm.modality = 'US'
dcm.manufacturer = 'ruby-dicom'
dcm.patients_name = 'Test'
dcm.patient_id = '12345'
dcm.study_instance_uid = DICOM.generate_uid
dcm.series_instance_uid = DICOM.generate_uid
dcm.study_id = '1'
dcm.series_number = '32'
dcm.instance_number = '142'
dcm.image_position_patient = "0.0\\0.0\\0.0"
dcm.image_orientation_patient = "1\\0\\0\\0\\1\\0"
dcm.frame_of_reference_uid = DICOM.generate_uid
dcm.samples_per_pixel = 3
dcm.photometric_interpretation = 'YBR_FULL_422'
dcm.number_of_frames = 1
dcm.rows = image.rows
dcm.columns = image.columns
dcm.pixel_spacing = "1.0\\1.0"

dcm.bits_allocated = 8
dcm.bits_stored = 8
dcm.high_bit = 7
dcm.pixel_representation = 0

# Create pixel tags/items:
pixel_data_seq = Sequence.new('7FE0,0010', :parent => dcm, :vr => 'OB')
basic_offset_table_item = Item.new(:length => 0, :parent => pixel_data_seq)
frame_item = Item.new(:bin => image.to_blob, :parent => basic_offset_table_item)

dcm.write('color.dcm')
Reply all
Reply to author
Forward
0 new messages