[vtkusers] Set window/level and convert image DICOM

David Gobbi david.gobbi at gmail.com
Mon Jan 25 17:27:30 EST 2016


Hi Pedro,

In your code, I suspect the vtkImageShiftScale is redundant. You can remove
it and get the same result.

 - David

On Mon, Jan 25, 2016 at 2:31 PM, Pedro Paulo <pedropaulobmjr at hotmail.com>
wrote:

> I would like to thank David Gobbi for support and to all who have read but
> couldn't somehow help, I managed to apply the window/level and convert the
> image correctly, below is my code, which may be useful to someone in the
> future, and I believe it will:
>
>
> from vtk import *
>
> reader = vtkDICOMImageReader()
> reader.SetFileName("input.dcm")
> reader.Update()
> image = reader.GetOutput()
>
>
> windowlevel = vtkImageMapToWindowLevelColors()
> windowlevel.SetInput(reader.GetOutput())
>
> windowlevel.SetWindow(2500)
> windowlevel.SetLevel(200)
> windowlevel.Update()
>
>
> shiftScaleFilter = vtkImageShiftScale()
> shiftScaleFilter.SetOutputScalarTypeToUnsignedChar()
> shiftScaleFilter.SetInputConnection(windowlevel.GetOutputPort())
>
> shiftScaleFilter.SetShift(-1.0*windowlevel.GetOutput().GetScalarRange()[0])
> oldRange = windowlevel.GetOutput().GetScalarRange()[1] -
> windowlevel.GetOutput().GetScalarRange()[0]
> newRange = 255
>
> shiftScaleFilter.SetScale(newRange/oldRange)
> shiftScaleFilter.Update()
>
>
> writer = vtkPNGWriter()
> writer.SetFileName("out.png")
> writer.SetInputConnection(windowlevel.GetOutputPort())
> writer.Write()
>
>
> ------------------------------
> *De:* Pedro Paulo <pedropaulobmjr at hotmail.com>
> *Enviado:* domingo, 24 de janeiro de 2016 07:39
>
> *Para:* Pedro Paulo
> *Cc:* vtkusers at vtk.org
> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>
>
> Now I'm beginning to understand, I was not understanding some things about
> DICOM, now my question is, do you know how I can access the value of each
> pixel in the image? And if possible, change these values, for now, only
> access is sufficient.
>
>
> ------------------------------
> *De:* Pedro Paulo <pedropaulobmjr at hotmail.com>
> *Enviado:* sábado, 23 de janeiro de 2016 06:58
> *Para:* David Gobbi
> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>
>
> That's the point, I did not understand the steps I need to take to make
> this calculation of w/l and convert the image to png. I do not ask you to
> do the implementation, only tell me the steps I need to do, because I did
> not understand completely, the rest I try to do.
>
>
> ------------------------------
> *De:* David Gobbi <david.gobbi at gmail.com>
> *Enviado:* sexta-feira, 22 de janeiro de 2016 21:53
> *Para:* Pedro Paulo
> *Cc:* vtkusers at vtk.org
> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>
> Hi Pedro,
>
> I've given you all of the information that you need.  At the very most,
> all you have left to do is a little bit of mathematics.
>
>  - David
>
>
> On Fri, Jan 22, 2016 at 2:44 PM, Pedro Paulo <pedropaulobmjr at hotmail.com>
> wrote:
>
>> Yeah, I used with version 6 and functioned normally, thank you, do you
>> know how I could do these operations of window/level using my code?
>> It is the last feature of my software and I have no idea how to do it.
>>
>>
>>
>> ------------------------------
>> *De:* David Gobbi <david.gobbi at gmail.com>
>> *Enviado:* sexta-feira, 22 de janeiro de 2016 19:22
>>
>> *Para:* Pedro Paulo
>> *Cc:* vtkusers at vtk.org
>> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>>
>> Hi Pedro,
>>
>> If you get that error with vtkScalarsToColors, then your VTK is very old!
>> To use vtkScalarsToColors like this, you must use vtk 5.10, vtk 6, or vtk
>> 7.
>>
>> Maybe you should keep your original code, with vtkImageShiftScale.
>> The math to convert from "range" to "shift/scale" is shown on this page:
>> http://www.cognitive-antics.net/mw/index.php?title=Data_range_remapping
>>
>>  - David
>>
>>
>>
>> On Fri, Jan 22, 2016 at 12:11 PM, Pedro Paulo <pedropaulobmjr at hotmail.com
>> > wrote:
>>
>>> I would like to set the WindowCenter and WindowWidth with which the
>>> image is converted to png, when I do the conversion using the program I
>>> sent, it makes the window and level automatically, which I do not want
>>> it to do. For your second question, yes, I want to calculate my own window
>>> and level when I convert the image.
>>> For example, I want to make a conversion with the values ww=2000, wl=250
>>> and at another time to make a conversion into the same image with the
>>> values ww=3000, wl=150.
>>>
>>>
>>> I tried using your code like this:
>>>
>>>
>>> from vtk import *
>>>
>>> reader = vtkDICOMImageReader()
>>> reader.SetFileName("img.dcm")
>>> reader.Update()
>>> image = reader.GetOutput()
>>>
>>> ###########################################################
>>> ww = 2500
>>> wl = 200
>>>
>>> greymap = vtkScalarsToColors()
>>> greymap.SetRange(wl-0.5*ww, wl+0.5*ww)
>>>
>>> applymap = vtkImageMapToColors()
>>> applymap.SetInputConnection(reader.GetOutputPort())
>>> applymap.SetLookupTable(greymap)
>>>
>>> applymap.SetOutputFormatToRGB()
>>> applymap.Update()
>>> ###########################################################
>>>
>>> and I got the following error:
>>>
>>> Traceback (most recent call last):
>>>   File "ff.py", line 12, in <module>
>>>     greymap = vtkScalarsToColors ()
>>> TypeError: this is an abstract class and can not be instantiated
>>>
>>>
>>>
>>> ------------------------------
>>> *De:* David Gobbi <david.gobbi at gmail.com>
>>> *Enviado:* sexta-feira, 22 de janeiro de 2016 18:40
>>>
>>> *Para:* Pedro Paulo
>>> *Cc:* vtkusers at vtk.org
>>> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>>>
>>> Hi Pedro,
>>>
>>> I think that I misunderstood your question.  Do you want to apply the
>>> preset
>>> WindowCenter and WindowWidth that are stored in the DICOM meta data?
>>> Or do you want to compute your own window and level?
>>>
>>>  - David
>>>
>>>
>>> On Thu, Jan 21, 2016 at 11:46 PM, Pedro Paulo <
>>> pedropaulobmjr at hotmail.com> wrote:
>>>
>>>> I'm using python to do this, my code is below, how do I add your code
>>>> in mine to give right?
>>>>
>>>> Does your vtk-dicom package set the parameters ww/wl and do the
>>>> conversion?
>>>>
>>>> Sorry my bad english. [image: &#X1f60a]
>>>>
>>>>
>>>>
>>>> from vtk import *
>>>>
>>>> reader = vtkDICOMImageReader()
>>>> reader.SetFileName('image.dcm')
>>>> reader.Update()
>>>> image = reader.GetOutput()
>>>>
>>>> shiftScaleFilter = vtkImageShiftScale()
>>>> shiftScaleFilter.SetOutputScalarTypeToUnsignedChar()
>>>> shiftScaleFilter.SetInputConnection(reader.GetOutputPort())
>>>>
>>>> shiftScaleFilter.SetShift(-1.0*image.GetScalarRange()[0])
>>>> oldRange = image.GetScalarRange()[1] - image.GetScalarRange()[0]
>>>> newRange = 255
>>>>
>>>> shiftScaleFilter.SetScale(newRange/oldRange)
>>>> shiftScaleFilter.Update()
>>>>
>>>> writer = vtkPNGWriter()
>>>> writer.SetFileName('output.jpg')
>>>> writer.SetInputConnection(shiftScaleFilter.GetOutputPort())
>>>> writer.Write()
>>>>
>>>>
>>>>
>>>> ------------------------------
>>>> *De:* David Gobbi <david.gobbi at gmail.com>
>>>> *Enviado:* quinta-feira, 21 de janeiro de 2016 22:00
>>>> *Para:* Pedro Paulo
>>>> *Cc:* vtkusers at vtk.org
>>>> *Assunto:* Re: [vtkusers] Set window/level and convert image DICOM
>>>>
>>>> Just to clarify my previous email, all CT and PET DICOM images have
>>>> RescaleIntercept/RescaleSlope, but for CT, it is not uncommon to have
>>>> RescaleIntercept = 0 and RescaleSlope = 1.  In any case, these fields
>>>> cannot be ignored if you plan to use WindowCenter and WindowWidth.
>>>>
>>>> On Thu, Jan 21, 2016 at 2:47 PM, David Gobbi <david.gobbi at gmail.com>
>>>> wrote:
>>>>
>>>>> Hi Pedro,
>>>>>
>>>>> I recommend using vtkImageMapToColors to apply a greyscale lookup
>>>>> table.
>>>>> In python, for example, it would look like this:
>>>>>
>>>>> from vtk import *
>>>>>
>>>>> greymap = vtkScalarsToColors()
>>>>> greymap.SetRange(wl - 0.5*ww, wl + 0.5*ww)
>>>>>
>>>>> applymap = vtkImageMapToColors()
>>>>> applymap.SetInputConnection(...)
>>>>> applymap.SetLookupTable(greymap)
>>>>> applymap.SetOutputFormatToRGB() # or use SetOutputFormatToLuminance()
>>>>> applymap.Update()
>>>>>
>>>>> Be careful, though, because according to the DICOM standard the
>>>>> VOI LUT (i.e. window/level) should only be applied after the Modality
>>>>> LUT
>>>>> (i.e. the RescaleIntercept/RescaleSlope).  Probably around half of the
>>>>> CT images and nearly all PET images have RescaleIntercept/RescaleSlope,
>>>>> so if you do not take these into account, you'll get an incorrect
>>>>> result.  You'd
>>>>> be safe with MRI, which does not have RescaleIntercept/Slope.
>>>>>
>>>>> So, of course, I'd suggest that you use my vtk-dicom package ;-)
>>>>> https://github.com/dgobbi/vtk-dicom
>>>>> It has a filter called vtkDICOMApplyRescale that will correctly apply
>>>>> the
>>>>> rescale information.
>>>>>
>>>>>  - David
>>>>>
>>>>>
>>>>> On Thu, Jan 21, 2016 at 2:22 PM, Pedro Paulo <
>>>>> pedropaulobmjr at hotmail.com> wrote:
>>>>>
>>>>>> Hi people, I want to convert a DICOM image to a png, but, before
>>>>>> converting, I would like to set the parameters of Window Width and Window
>>>>>> Center, to that the png image looks like if the parameters of ww/wl were
>>>>>> seen in a viewer, does anyone know how could I do this?
>>>>>> Thanks!
>>>>>>
>>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160125/fd5a8a9e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OutlookEmoji-&#X1f60a.png
Type: image/png
Size: 488 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160125/fd5a8a9e/attachment.png>


More information about the vtkusers mailing list