6

I am not an experienced programmer, just need to add a DICOM viewer to my VS2010 project. I can display the image in Windows Forms, however can't figure out how to change the window center and width. Here is my script:

DicomImage image = new DicomImage(_filename);
            int maxV = image.NumberOfFrames;
            sbSlice.Maximum = maxV - 1;
            image.WindowCenter = 7.0;
            double wc = image.WindowCenter;
            double ww = image.WindowWidth;

            Image result = image.RenderImage(0);
            DisplayImage(result);

It did not work. I don't know if this is the right approach.

hncl
  • 2,295
  • 7
  • 63
  • 129
  • can you be a little more clear about what DICOM library you're using? – Anatoly G Oct 23 '12 at 19:53
  • I am using Fellow Oak DICOM for .NET, thanks. – hncl Oct 23 '12 at 20:21
  • 1
    Noticed that you posted the same question on the *fo-dicom* discussion forum and got the response that there is a window level bug in the software version you are running. For consistency in *Stackoverflow*, it could be a good idea to provide your own answer here based on the forum response, and also to indicate in the question that you are running *fo-dicom* version 1.0.26. – Anders Gustafsson Oct 24 '12 at 05:42
  • Good point, I will post the answer once I get it working, the bug was fixed in the lattest Git; however image.WindowCenter = 7.0; didn't work, as I mentioned I am not experienced programmer and was hoping to find an example or documentation on how to change the window level. – hncl Oct 24 '12 at 05:50

2 Answers2

6

The DicomImage class was not created with the intention of it being used to implement an image viewer. It was created to render preview images in the DICOM Dump utility and to test the image compression/decompression codecs. Maybe it was a mistake to include it in the library at all?

It is difficult for me to find fault in the code as being buggy when it is being used for something far beyond its intended functionality.

That said, I have taken some time to modify the code so that the WindowCenter/WindowWidth properties apply to the rendered image. You can find these modifications in the Git repo.

var img = new DicomImage(fileName);
img.WindowCenter = 2048.0;
img.WindowWidth = 4096.0;
DisplayImage(img.RenderImage(0));
  • 1
    Thanks Colby, very much appreciated. You are right, I was trying to use the library for extended function; no doubt this is due to my limited experience with DICOM. Thanks again for your effort. – hncl Oct 27 '12 at 18:07
2

I looked at the code and it looked extremely buggy. https://github.com/rcd/fo-dicom/blob/master/DICOM/Imaging/DicomImage.cs

In the current buggy implementation setting the WindowCenter or WindowWidth properties has no effect unless Dataset.Get(DicomTag.PhotometricInterpretation) is either Monochrome1 or Monochrome2 during Load(). This is already ridiculous, but it still cannot be used because the _renderOptions variable is only set in a single place and is immediately used for the _pipeline creation (not giving you chance to change it using the WindowCenter property). Your only chance is the grayscale _renderOptions initialization: _renderOptions = GrayscaleRenderOptions.FromDataset(Dataset);.

The current solution: Your dataset should have

  • DicomTag.WindowCenter set appropriately
  • DicomTag.WindowWidth != 0.0
  • DicomTag.PhotometricInterpretation == Monochrome1 or Monochrome2

The following code accomplishes that:

DicomDataset dataset = DicomFile.Open(fileName).Dataset;
//dataset.Set(DicomTag.WindowWidth, 200.0); //the WindowWidth must be non-zero
dataset.Add(DicomTag.WindowCenter, "100.0");
//dataset.Add(DicomTag.PhotometricInterpretation, "MONOCHROME1"); //ValueRepresentations tag is broken
dataset.Add(new DicomCodeString(DicomTag.PhotometricInterpretation, "MONOCHROME1"));
DicomImage image = new DicomImage(dataset);
image.RenderImage();

The best solution: Wait while this buggy library is fixed.

Ark-kun
  • 6,358
  • 2
  • 34
  • 70
  • Thank you for taking the time, we narrowed the problem in DicomDataset class in the following code: if (typeof(T) == typeof(decimal)) return Add(new DicomDecimalString(tag, values.Cast().ToArray())); the error is "Unable to create DICOM element of type DS with values of type System.Int32" I posted that on the google group. Overall I found fo-Dicom to be one of the better libraries, I have tired other toolkits without much scucess. – hncl Oct 27 '12 at 02:40
  • Why do you think this is the source of the problem? Do you have decimals somewhere? If the code posted in the question works, I think I can write the code to make WindowsCenter work. P.S. Are your images grayscale? – Ark-kun Oct 27 '12 at 02:49
  • The images are grayscale. Here is the link to my workaround to fix the problem: https://groups.google.com/forum/?fromgroups=#!topic/fo-dicom/ycnB25bAvq4. Thanks – hncl Oct 27 '12 at 03:20
  • 1) This won't work in the current library implementation. 2) The type of WindowCenter should be double, not ushort double. 3) I'll update my answer and post the draft code soon. – Ark-kun Oct 27 '12 at 03:35
  • Thanks, I got this error: Dicom.DicomDataset' does not contain a definition for 'Set' – hncl Oct 27 '12 at 04:06
  • Sorry, that should be .Add, not .Set – Ark-kun Oct 27 '12 at 04:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18650/discussion-between-user373721-and-ark-kun) – hncl Oct 27 '12 at 04:32
  • Changed the types to strings. Try again. – Ark-kun Oct 27 '12 at 05:08
  • That library is so broken... another try to force it to work. – Ark-kun Oct 27 '12 at 05:38
  • Ok. Fixed now. Fun facts: I don't know what "DICOM", "WindowCenter", Dataset etc is. Also it's 9:48 AM. – Ark-kun Oct 27 '12 at 05:49