[vtkusers] How to map negative grayscale to color?

Liu_tj tjlp at netease.com
Sat Apr 2 01:12:28 EDT 2016


Hi,David,


As you help me on this thread, I would like to continue on it. I don't clearly understand your words "I didn't think it would be necessary, because I thought that if you passed an RGB (or RGBA) image to vtkImageViewer2 ....". How to pass an RGB image to the vtkImageViewer2? 


My requirement is that the pixels in some greyscale threshold displayed at some specific color and other pixels are not changed. At that time I had some misunderstanding and I thought it worked.Actually I don't work it out. My code is below. I just change the lookup table of the vtkImageViewer2's vtkImageMapToWindowLevelColors. The result is not I need. 


It just directly change the image of vtkImageViewer2 and the whole image are changed. See the attachment image 1. 
If I use a new vtkImageMapToColors instead of the vtkImageViewer2's vtkImageMapToWindowLevelColors, then nothing happen. Seems the vtkImageMapToColors shows no effect. See the attachment image 2.


My DICOM series is 512*512*190. The scalar range is [0, 3140]. In my test, the threshold value is [0,400].


private void GrayToColors( Segament_Threshold threshold)
        {
            vtkLookupTable lookupTable = vtkLookupTable.New();


            double validmin = System.Math.Max(m_scalarrange[0], threshold.Min_Threshold);
            double validmax = System.Math.Min(m_scalarrange[1], threshold.Max_Threshold);
            int number = (int)(m_scalarrange[1] - m_scalarrange[0] + 1);
            lookupTable.SetRange(m_scalarrange[0], m_scalarrange[1]);
            lookupTable.SetNumberOfTableValues(number);
            lookupTable.Build();


            for (int i = (int)(m_scalarrange[0]); i < (int)(validmin); i++)
            {
                lookupTable.SetTableValue(i - (int)m_scalarrange[0], 0.0, 0.0, 0.0, 0.0);
            }
            for (int i = (int)(validmin); i <= (int)(validmax); i++)
            {
                lookupTable.SetTableValue(i - (int)m_scalarrange[0], 0x66/0xff, 1.0, 1.0, 1.0);
            }
            for (int i = (int)(threshold.Max_Threshold) + 1; i < (int)(m_scalarrange[1]); i++)
            {
                lookupTable.SetTableValue(i - (int)m_scalarrange[0], 0.0, 0.0, 0.0, 0.0);
            }


            vtkImageMapToWindowLevelColors colormapper = m_imageViewer.GetWindowLevel();
            colormapper.SetOutputFormatToRGBA();
            colormapper.PassAlphaToOutputOn();
            colormapper.SetLookupTable(lookupTable);
            colormapper.Update();


            if (m_colormapActor == null)
            {
                m_colormapActor = vtkImageActor.New();
                m_colormapActor.GetMapper().SetInputConnection(colormapper.GetOutputPort());
                m_renderer.AddViewProp(m_colormapActor);
            }
            m_renderer.ResetCamera();
            m_renderer.Modified();
            m_imageViewer.SetRenderer(m_renderer);
            m_imageViewer.Render();
        }


Any thing wrong?


Thanks
Liu Peng


在2016-03-10,"David Gobbi" <david.gobbi at gmail.com> 写道: -----原始邮件-----
发件人:"David Gobbi" <david.gobbi at gmail.com>
发送时间:2016年03月10日 星期四
收件人:"Liu_tj" <tjlp at netease.com>
抄送:"vtkusers" <vtkusers at vtk.org>
主题:Re: Re: Re: Re: Re: [vtkusers] How to map negative grayscale to color?


Hi Liu Peng,


I'm glad to hear it worked for you.  I didn't think it would be necessary, because I thought that if you passed an RGB (or RGBA) image to vtkImageViewer2 it would simply display it as-is, without needing any tweaks to internal parts of the Viewer (note: I hardly ever use vtkImageViewer or vtkimageViewer2 myself, I prefer to use the image mappers directly instead).


 - David


On Wed, Mar 9, 2016 at 8:21 AM, Liu_tj <tjlp at netease.com> wrote:
Hi, David,

I try it again. The conversion from gray scale to RGB works after I set the LookupTable to vtkImageMapToWindowLevelColors returned by vtkImageViewer2::GetWindowLevels().




在2016-03-09,"David Gobbi" <david.gobbi at gmail.com> 写道: -----原始邮件-----
发件人:"David Gobbi" <david.gobbi at gmail.com>
发送时间:2016年03月9日 星期三
收件人:"Liu_tj" <tjlp at netease.com>
抄送:"vtkusers" <vtkusers at vtk.org>
主题:Re: Re: Re: Re: [vtkusers] How to map negative grayscale to color?



No, that isn't necessary.



On Tue, Mar 8, 2016 at 9:29 PM, Liu_tj <tjlp at netease.com> wrote:

Hi,David,

Do I need to set the LookupTable to vtkImageMapToWindowLevelColors which is returned by vtkImageViewer2::GetWindowLevels() instead of create a new vtkImageMapToColors?

BR,
Thanks



在2016-03-09,"David Gobbi" <david.gobbi at gmail.com> 写道: -----原始邮件-----
发件人:"David Gobbi" <david.gobbi at gmail.com>
发送时间:2016年03月9日 星期三
收件人:"Liu_tj" <tjlp at netease.com>
主题:Re: Re: Re: [vtkusers] How to map negative grayscale to color?



Hi Liu Peng,


Do you want to show a gray image underneath, and the color image
on top?  I'm not sure if vtkImageViewer works property if you add
more than one image.


You should add a "print" statement of some kind to your code to
print the values of scalarrange, to be sure they are correct.


Don't call colormap.Build() after setting your own values for the table,
or it might overwrite the values that you have set.  If you call it at all,
call it immediately after SetNumberOfTableValues(), before you set
any of your own values.


The range on the final for() loop should be the following:
for (int i = (int)(threshhold[1]) + 1; i <= (int)(scalarrange[1]); i++)


Also, if you want the beyond-threshold values to be transparent
instead of black, then set the alpha component to zero:


for (int i = (int)(scalarrange[0]); i < (int)(threshhold[0]); i++)

{
    colormap.SetTableValue(i - (int)scalarrange[0], 0.0, 0.0, 0.0, 0.0);
}


 - David




On Tue, Mar 8, 2016 at 9:40 AM, Liu_tj <tjlp at netease.com> wrote:
Hi, David,

I follow your post and it doesn't work. The DICOM image shows no color, just gray image. Could you have a look at my code below? I use a vtkImageView2 to display the DICOM image. The code is c# by Activiz .NET. The scalar range of test image is [-1024,3071]. I want to show the gray scale at [1000,3000] to green. The function GrayToColors() maps gray scale to color, it is called by Display().

private void Display(vtkRenderWindow aRenderWindow, vtkAlgorithmOutput aData)
        {
            if (aData == null)
                return;

            m_imageViewer = vtkImageViewer2.New();
            m_imageViewer.SetInputConnection(aData);
            switch (m_sliceDirection)
            {
                case Direction.AXIAL:
                    m_imageViewer.SetSliceOrientationToXY();
                    break;
                case Direction.CORONAL:
                    m_imageViewer.SetSliceOrientationToYZ();
                    break;
                case Direction.SAGITTAL:
                    m_imageViewer.SetSliceOrientationToXZ();
                    break;
                default:
                    break;
            }

            m_imageViewer.GetSliceRange(ref m_minSliceNumber, ref m_maxSliceNumber);
            m_interactorStyle = vtkInteractorStyleImage.New();
            m_interactorStyle.MouseWheelForwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelForwardEvt);
            m_interactorStyle.MouseWheelBackwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelBackwardEvt);

            aRenderWindow.GetInteractor().SetInteractorStyle(m_interactorStyle);
            aRenderWindow.GetRenderers().InitTraversal();

            m_renderer = aRenderWindow.GetRenderers().GetFirstRenderer();
            m_renderer.SetBackground(0.0, 0.0, 0.0);

            m_imageViewer.SetRenderWindow(aRenderWindow);
            m_currentSlice = m_minSliceNumber + (m_maxSliceNumber - m_minSliceNumber) / 2;
            m_imageViewer.SetSlice(m_currentSlice);

            double[] scalarrange = new double[2];
            scalarrange = m_imageViewer.GetInput().GetScalarRange();
            double[] threshhold = new double[2];
            threshhold[0] = 1000;
            threshhold[1] = 3000;
            m_imageViewer.Render();
            GrayToColors(m_renderer, aData, scalarrange, threshhold);

            m_imageViewer.Render();
        }

        private void GrayToColors(vtkRenderer renderer, vtkAlgorithmOutput data, double[] scalarrange, double[] threshhold)
        {
            vtkLookupTable colormap = vtkLookupTable.New();
            int number = (int)(scalarrange[1] - scalarrange[0] + 1);
            colormap.SetRange(scalarrange[0], scalarrange[1]);
            colormap.SetNumberOfTableValues(number);
           
            for (int i = (int)(scalarrange[0]); i < (int)(threshhold[0]); i++)
            {
                colormap.SetTableValue(i - (int)scalarrange[0], 0.0, 0.0, 0.0, 1.0);
            }
            for (int i = (int)(threshhold[0]); i <= (int)(threshhold[1]); i++)
            {
                colormap.SetTableValue(i - (int)scalarrange[0], 0.0, 1.0, 0.0, 1.0);
            }
            for (int i = (int)(threshhold[1]); i < (int)(scalarrange[1]); i++)
            {
                colormap.SetTableValue(i - (int)scalarrange[0], 0.0, 0.0, 0.0, 1.0);
            }
            colormap.Build();

            vtkImageMapToColors colormapper = vtkImageMapToColors.New();
            colormapper.SetInputConnection(data);
            colormapper.SetLookupTable(colormap);
            colormapper.Update();

            vtkImageActor colormapActor = vtkImageActor.New();
            colormapActor.SetInput(colormapper.GetOutput());
            renderer.AddActor(colormapActor);
            renderer.ResetCamera();
            renderer.Modified();
        }


Thanks


在2016-03-08,"David Gobbi" <david.gobbi at gmail.com> 写道: -----原始邮件-----
发件人:"David Gobbi" <david.gobbi at gmail.com>
发送时间:2016年03月8日 星期二
收件人:"Liu_tj" <tjlp at netease.com>
抄送:"vtkusers" <vtkusers at vtk.org>
主题:Re: Re: [vtkusers] How to map negative grayscale to color?



Hi Liu Pen,


That's because I forgot to include the index argument in the call to SetTableValue:


table->SetRange(-1024, 4095);
table->SetNumberOfTableValues(4095 + 1024 + 1);
for (int i = -1024; i < -10; i++) {
    table->SetTableValue(i + 1024, 0.0, 0.0, 0.0, 1.0);
}


The table index must start at zero.  The call to SetRange(-1024, 4095) causes the scalar value -1024 to map to table index 0.  In the "for" loop, i is the scalar value.


 - David




On Tue, Mar 8, 2016 at 7:27 AM, Liu_tj <tjlp at netease.com> wrote:
Hi, David,


When I try, a lot of errors are popup: "Can't set the table value for negative index".

Seems the negative grayscale needs some transformation.

Thanks
Liu Peng



在2016-03-08,"David Gobbi" <david.gobbi at gmail.com> 写道: -----原始邮件-----
发件人:"David Gobbi" <david.gobbi at gmail.com>
发送时间:2016年03月8日 星期二
收件人:"Liu_tj" <tjlp at netease.com>
抄送:"vtkusers" <vtkusers at vtk.org>
主题:Re: [vtkusers] How to map negative grayscale to color?


Hi Liu Peng,


You can set individual values in the lookup table,
in order to precisely map certain scalar values to
specific colors:


table->SetRange(-1024, 4095);
table->SetNumberOfTableValues(4095 + 1024 + 1);
for (int i = -1024; i < -10; i++) {
    table->SetTableValue(0.0, 0.0, 0.0, 1.0);
}
for (int i = -10; i <= 200; i++) {
    table->SetTableValue(0.0, 1.0, 0.0, 1.0);
}
etcetera.


 - David




On Mon, Mar 7, 2016 at 9:31 AM, Liu_tj <tjlp at netease.com> wrote:

Hi,

I try to use the vtkLookupTable to convert the grayscale to rgb color. The image scalar range is [-1024,3071] returned by vtkImageData::GetScalarRange(). Now I want to convert specific grayscale range, for example, [-10,200] to one color, such as green (RGB is 0,1,0). How to do that by vtkLookupTable? I have no idea what method should call: SetNumberOfTableValues(), SetRange(), SetHueRange(), SetValueRange(). What do "Hue" and "Value" stand for?

BR,
Liu Peng


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160402/d6da2e78/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ImageMapToColors_1.jpg
Type: image/jpeg
Size: 40242 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160402/d6da2e78/attachment-0002.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ImageMapToColors_2.jpg
Type: image/jpeg
Size: 35185 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160402/d6da2e78/attachment-0003.jpg>


More information about the vtkusers mailing list