[vtkusers] How to map negative grayscale to color?

Liu_tj tjlp at netease.com
Wed Mar 9 10:21:01 EST 2016


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/20160309/5d4ed359/attachment.html>


More information about the vtkusers mailing list