Fellow Oak DICOM - changing image window level
Asked Answered
P

2

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.

Postulate answered 22/10, 2012 at 23:15 Comment(4)
can you be a little more clear about what DICOM library you're using?Paterfamilias
I am using Fellow Oak DICOM for .NET, thanks.Postulate
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.Conchology
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.Postulate
E
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.

Excoriate answered 27/10, 2012 at 2:23 Comment(10)
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<decimal>().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.Postulate
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?Excoriate
The images are grayscale. Here is the link to my workaround to fix the problem: groups.google.com/forum/?fromgroups=#!topic/fo-dicom/…. ThanksPostulate
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.Excoriate
Thanks, I got this error: Dicom.DicomDataset' does not contain a definition for 'Set'Postulate
Sorry, that should be .Add, not .SetExcoriate
let us continue this discussion in chatPostulate
Changed the types to strings. Try again.Excoriate
That library is so broken... another try to force it to work.Excoriate
Ok. Fixed now. Fun facts: I don't know what "DICOM", "WindowCenter", Dataset etc is. Also it's 9:48 AM.Excoriate
C
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));
Cephalothorax answered 27/10, 2012 at 15:49 Comment(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.Postulate
E
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.

Excoriate answered 27/10, 2012 at 2:23 Comment(10)
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<decimal>().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.Postulate
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?Excoriate
The images are grayscale. Here is the link to my workaround to fix the problem: groups.google.com/forum/?fromgroups=#!topic/fo-dicom/…. ThanksPostulate
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.Excoriate
Thanks, I got this error: Dicom.DicomDataset' does not contain a definition for 'Set'Postulate
Sorry, that should be .Add, not .SetExcoriate
let us continue this discussion in chatPostulate
Changed the types to strings. Try again.Excoriate
That library is so broken... another try to force it to work.Excoriate
Ok. Fixed now. Fun facts: I don't know what "DICOM", "WindowCenter", Dataset etc is. Also it's 9:48 AM.Excoriate

© 2022 - 2024 — McMap. All rights reserved.