Convert to JPG

286 views
Skip to first unread message

vijendra rao

unread,
May 22, 2013, 7:48:52 AM5/22/13
to ruby-...@googlegroups.com
Sorry if it is already been answered, I tried searching in the forum and github issues, but didn't find a solution. 
I am trying to convert attached sample image downloaded from osirix. But converted jpg image has an overlay or its very dull. I have tried following code snippets, as per your suggestion in issue #39 in github.
image = dcm.image(:level => [35, 350], :remap => true)
image.normalize.write("CT_35_350.jpg")
image = dcm.image(:remap => true)
image.normalize.write("CT.jpg")
But that is also not producing the right image. 

Here is the details from dcm.summary command. It is not compressed image as "Compression:          No"

DICOM Object Properties:
-------------------------------------------
Source:               File (successfully read): IM-0001-0001.dcm
Modality:             CT Image Storage
Meta Header:          Yes
Value Representation: Explicit
Byte Order (File):    Little Endian
Pixel Data:           Yes
Image Size:           512*512
Number of frames:     1
Photometry:           MONOCHROME2
Compression:          No
Bits per Pixel:       16
-------------------------------------------
=> ["Source:               File (successfully read): IM-0001-0001.dcm", "Modality:             CT Image Storage", "Meta Header:          Yes", "Value Representation: Explicit", "Byte Order (File):    Little Endian", "Pixel Data:           Yes", "Image Size:           512*512", "Number of frames:     1", "Photometry:           MONOCHROME2", "Compression:          No", "Bits per Pixel:       16"]
 
Can you please suggest.

IM-0001-0001.tar.gz

Christoffer Lervåg

unread,
May 22, 2013, 9:50:57 AM5/22/13
to ruby-...@googlegroups.com
Hello Vijendra

This dicom file contains something which I haven't seen before: Multiple window center & width values. Probably ruby-dicom doesn't consider this as a possibility and mishandles it. I will investigate that and fix it for the next gem version.

However, you should still be able to extract a valid image, by manually entering these values.
Try the following:

image = dcm.image(:level => [20, 400], :remap => true)
The dicom file gives also an alternative suggested pair of values, which you could also try:
image = dcm.image(:level => [600, 2500], :remap => true)
Best regards,
Chris

Kamil Łęczycki

unread,
May 22, 2013, 11:06:06 AM5/22/13
to ruby-...@googlegroups.com
I think it should generally take window width and center tags values by default to image level option, however if sth goes wrong and it can't do it this it should manually calculate the histogram and take those values from it.

-- 
Kamil Łęczycki
Sent with Sparrow

--
You received this message because you are subscribed to the Google Groups "ruby-dicom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-dicom+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

vijendra rao

unread,
May 23, 2013, 2:07:44 AM5/23/13
to ruby-...@googlegroups.com
Hello Chris,

Thank you very much for your help. But unfortunately, both of the below commands produces complete black image. If I use normalize, it produces two white patches, but even that is not a valid image. Please check the attached images for reference. Tried like below.

dcm = DICOM::DObject.read('IM-0001-0001.dcm')
image = dcm.image(:level => [20, 400], :remap => true)
image.display
image.normalize.display

Am I doing some thing wrong? Or is it some thing to do with my image magick installation? 
IM-0001-0001-1.tar.gz

Christoffer Lervåg

unread,
May 23, 2013, 2:10:50 AM5/23/13
to ruby-...@googlegroups.com
Ok Vijendra,

I will check this out myself. There might be some bug which also affects the use of manual width/center specification in this case. Hopefully I can test it by the end of the day.

Regards,
Chris


Christoffer Lervåg

unread,
May 23, 2013, 9:09:11 AM5/23/13
to ruby-...@googlegroups.com
Vijendra,

I have tried this on my system, using the latest ruby-dicom gem (version 0.9.5), and I get a completely reasonable image when extracting the image, in agreement with your 'desktop viewer' screenshot.

I can only speculate that you are using an old version of ruby-dicom, or that you are not actually normalizing the image before you write it to jpg?

Here is my tested code:

  file = './IM-0001-0001.dcm'
  dcm = DObject.read(file)
  image1 = dcm.image(:level => [20, 400], :remap => true)
  image1.normalize.write("CT_20_400.jpg")
  image2 = dcm.image(:remap => true)
  image2.normalize.write("CT_default_remap.jpg")

Regards,
Chris

vijendra rao

unread,
May 24, 2013, 7:17:33 AM5/24/13
to ruby-...@googlegroups.com
Hello Chris,

Sorry to bother you again on this. I have checked. My ruby-dicom version is 0.9.5, ImageMagick version is 6.7.7-10 on Ubuntu 12.10.
And even after using normalize it is not coming properly.  I have tried the 'convert' command of ImageMagick, as you suggested in github issue #49 to some one else. That is also not coming correctly. So will it be some thing to do with ImageMagick? Any help/suggestions are much appreciated. 
IM-0001-0001-2.tar.gz.tar.gz

Christoffer Lervåg

unread,
May 24, 2013, 7:37:26 AM5/24/13
to ruby-...@googlegroups.com
Ok Vijendra,

Just to make sure that I know exactly what you are doing, and exactly what your problem is, I want you to do the following:

1. Create and attach in your response, two screenshots (jpg images - ideally uncompressed, for simplicity). The one giving the 'expected image', and the one you get with ruby-dicom.
2. Explain in words what you think is wrong with the image produced by ruby-dicom.
3. Post the exact code you are using in ruby-dicom to produce the jpg image.

Since I can't reproduce this myself, we need to make sure that there is no misunderstanding, and rule out any mistakes in your actual code.

Regards,
Chris

vijendra rao

unread,
May 25, 2013, 5:04:41 AM5/25/13
to ruby-...@googlegroups.com
OK. Here are the details.

1. I am expecting some thing like attached "expected_image.png".  And the images, I am getting as per my below code (# 3) are attached, with same names.

2. I am expecting to see those two white spots(guess its ankle) in the black background, as in the expected_image.png. But, this is what I am getting.
    Try1.  Just dcm.image without any option and normalize, I will get to see the image but kind of overlay exists ( "image_try1.jpg"). 
    Try2.  level and remap options with normalize. Complete black bg with two white patches. ( "image_try2.jpg"). 
    Try3.  only remap option with normalize. Textured  bg with two white patches. ( "image_try3.jpg"). 
    Try4.  Only normalize without any option. Complete white image. Nothing visible. ( "image_try4.jpg"). 

3. Here is the code.

require 'dicom'
require 'RMagick'
include Magick
include DICOM

dcm = DObject.read("IM-0001-0001.dcm")

#Try1: defualt image, without any option
image_try1 = dcm.image
image_try1.write("image_try1.jpg")

#Try2: level and remap option with normalize
image_try2 = dcm.image(:level => [20, 400], :remap => true)
image_try2.normalize.write("image_try2.jpg")

#Try3: only remap option with normalize
image_try3 = dcm.image(:remap => true)
image_try3.normalize.write("image_try3.jpg")

#Try4: defualt image, without any option normalized
image_try4 = dcm.image
image_try4.normalize.write("image_try4.jpg")

System specs.
ruby-dicom  V 0.9.5, ImageMagick V 6.7.7-10, Ruby V 1.9.3p385  on Ubuntu 12.10.

Regards,
Vijendra
expected_image.png
image_try1.jpg
image_try2.jpg
image_try3.jpg
image_try4.jpg

Christoffer Lervåg

unread,
May 27, 2013, 3:46:36 AM5/27/13
to ruby-...@googlegroups.com
Thank you for this detailed response Vijendra.

I have now tried to execute the exact same script on my work computer, and the images I get are different from yours. I honestly don't know what goes wrong for you, but I can only speculate that it is related to ImageMagick.

Regards,
ChrisInline image 1Inline image 4Inline image 3Inline image 2

My specs:
Windows XP SP3 32 bit
ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
ruby-dicom 0.9.5
RMagick 2.13.2
ImageMagick-6.7.6-Q16

image.jpeg
image.jpeg
image.jpeg
image.jpeg

Christoffer Lervåg

unread,
May 27, 2013, 4:42:26 AM5/27/13
to ruby-...@googlegroups.com
Perhaps it wasn't clear in my response which image was which, but anyway, try2 seems to give the expected image result for me.

Do you know if your image magick installation is Q8? (Mine is Q16).


vijendra rao

unread,
May 27, 2013, 9:12:16 AM5/27/13
to ruby-...@googlegroups.com
Thank you very much for checking. 

Even I have  image magick Q16. And yes it is related to image magick.

Following thread have some insights.    
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=19748&hilit=DICOM

Following convert commands are giving nearly valid images.
convert -define dcm:display-range=reset IM-0001-0001.dcm -auto-level IM-0001-0001.jpg
convert -define dcm:display-range=reset IM-0001-0001.dcm  -brightness-contrast -5x25 -normalize IM-0001-0001.jpg

But now I have to find out, the way to give proper -brightness-contrast values for each images. 
Any option in ruby-dicom to give custom options. Or I guess, I have to run convert command with options directly from ruby.

Christoffer Lervåg

unread,
May 28, 2013, 3:47:13 AM5/28/13
to ruby-...@googlegroups.com
Vijendra,

I have now verified that I get the correct/expected result as in my reply above also on a separate window system with Ruby 2.0.0.p0 and ImageMagick 6.8.4-Q16 (Rmagick 2.13.2). Since it works just fine at all my tested systems Im running out of suggestions.

Feel free to pursue this yourself, e.g. look at the ruby-dicom code, play with some histogram options as per Kamil's suggestion, test other Ubuntu images with other versions of ImageMagick or reach out to the ImageMagick forum.

Best regards,
Chris

vijendra rao

unread,
May 28, 2013, 6:19:58 AM5/28/13
to ruby-...@googlegroups.com
Thank you Chris and sorry for wasting your time. 

Then I guess it is a problem with Ubuntu image magick installation (or my system's). I have used convert commands mentioned in my last reply with paperclip. I have written custom paperclip processor as per this suggestion. Just pasting code here.

module Paperclip
  class DcmToJpg < Processor

    def initialize file, options = {}, attachment = nil
      super
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

     def make
       src = @file
       dst = Tempfile.new(['temp_file', '.jpg'])
       dst.binmode
       begin
         parameters = ["-define dcm:display-range=reset"]
         parameters << ":source"
         parameters << "-auto-level -normalize"
         parameters << ":dest"

         parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

         success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
       rescue
         raise PaperclipError, "There was an error converting dicom to jpeg for #{@basename}"
       end

       dst
     end

  end
end
Reply all
Reply to author
Forward
0 new messages