# vtkImageMapToColors display a wrong color

**URL:** https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360
**Category:** Support
**Created:** [January 2, 2023, 1:21pm UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360 "2023-01-02T13:21:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 2, 2023, 1:21pm UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360/1 "2023-01-02T13:21:17Z")

</div>

I am using `vtkImageMapToColors` to map gray image to color image. My image has only 0/1 value:

```auto
img = vtk.vtkImageData()
img.SetDimensions(256, 256, 1)
img.AllocateScalars(vtk.VTK_INT, 1)

for i in range(256):
    for j in range(256):

        if i > 128 and i<192 and j>128 and j<192:
            img.SetScalarComponentFromFloat(i, j, 0, 0, 1)
        else:
            img.SetScalarComponentFromFloat(i, j, 0, 0, 0)

```

If the color table is given as:

```auto
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(2)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.Build()

```

The shown result is what I expect:

![image](https://discourse.vtk.org/uploads/default/original/2X/4/4d94ac553541a7a72cfb10e85e1afd8733a0dd4d.png)

But, if the color table is given as:

```auto
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)
colorTable.Build()

```

The shown result is:

![image](https://discourse.vtk.org/uploads/default/original/2X/5/5e5a3ad5af3014335b56e8c44d31555cf8c5909b.png)

I hope the color part is red, because the value in image is 1, and `colorTable.SetTableValue(1, 1, 0, 0, 1)`. I don’t know why the color part is yellow.

The complete code to reproduce my problem is:

```auto
import vtkmodules.all as vtk

img = vtk.vtkImageData()
img.SetDimensions(256, 256, 1)
img.AllocateScalars(vtk.VTK_INT, 1)

for i in range(256):
    for j in range(256):

        if i > 128 and i<192 and j>128 and j<192:
            img.SetScalarComponentFromFloat(i, j, 0, 0, 1)
        else:
            img.SetScalarComponentFromFloat(i, j, 0, 0, 0)

colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)
colorTable.Build()

mapToColor = vtk.vtkImageMapToColors()
mapToColor.SetInputData(img)
mapToColor.SetLookupTable(colorTable)

imgMapper = vtk.vtkImageSliceMapper()
imgMapper.SetInputConnection(mapToColor.GetOutputPort())
imgSlice = vtk.vtkImageSlice()
imgSlice.SetMapper(imgMapper)

render = vtk.vtkRenderer()
render.AddActor(imgSlice)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 2, 2023, 4:44pm UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360/2 "2023-01-02T16:44:11Z")

</div>

By default, `vtkLookupTable` will map an input value of zero to the first color, and an input value of one to the last color. In other words, by default the lookup table assumes that the input data is a set of floating-point values between 0.0 and 1.0.

You can set the Range of the table to make it map integers directly to colors:

```python
colorTable.SetRange(0, colorTable.GetNumberOfTableValues() - 1)

```

For example, if the table has 3 values, this sets the Range to `[0, 2]`.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 3, 2023, 12:07am UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360/3 "2023-01-03T00:07:19Z")

</div>

Thanks for kindly reply. I have another question. When I modify the setting as:

```auto
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.IndexedLookupOn() ################# only categorical data is considered
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableRange(0, 2)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)
colorTable.Build()

```

In this setting, I add `colorTable.IndexedLookupOn()`, and the result is:

![image](https://discourse.vtk.org/uploads/default/original/2X/9/9bfebe04f6be02a79f959f10eab86d4bf43ab13e.png)

How to use `IndexedLookupOn`?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 3, 2023, 5:44am UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360/4 "2023-01-03T05:44:15Z")

</div>

For `IndexedLookupOn()` to work, you must add annotations to the lookup table. I’ve never used it, so I don’t have anything else to say about it.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 3, 2023, 6:13am UTC](https://discourse.vtk.org/t/vtkimagemaptocolors-display-a-wrong-color/10360/5 "2023-01-03T06:13:54Z")

</div>

@dgobbi Thanks for kindly reply. `IndexedLookupOn()` works with the following setting:

```auto
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.IndexedLookupOn()
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)

colorTable.SetAnnotation(0, 'white')
colorTable.SetAnnotation(1, 'red')
colorTable.SetAnnotation(2, 'yellow')

colorTable.Build()

```
