# Show color images in Slicer

**URL:** <https://discourse.slicer.org/t/show-color-images-in-slicer/22619>\
**Category:** Support\
**Tags:** python, openigtlink\
**Created:** [March 21, 2022, 1:05pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619 "2022-03-21T13:05:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lb123](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@lb123](https://discourse.slicer.org/u/lb123)\
**Post date:** [March 21, 2022, 1:05pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619/1 "2022-03-21T13:05:59Z")

</div>

Hi,

I’m using pyigtl to create a server to send color images to Slicer via IGT Link.  
I can successfully receive the images in Slicer creating a client using the OpenIGTLinkIF module.  
However I cannot show the received images as color RGB images but I can only show them as 3 separate channels (R, G and B).  
Can you please tell me how to show the image as a color image?

Thank you

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 21, 2022, 1:46pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619/2 "2022-03-21T13:46:48Z")

</div>

A volume node is created automatically when the first image is received. For single-component images a `vtkMRMLScalarVolumeNode` is created, while for multi-component images (such as an RGB color image) a `vtkMRMLVectorVolumeNode` is created. The class of an object cannot be changed, therefore you may end up having a scalar volume node storing multi-component image data if you first send a single-component volume and then a color volume using the same device.

If you need to send both grayscale and color volumes then you can avoid having suboptimal volume node type by using different device names (e.g., `Image-BMode`, `Image-Doppler`). Alternatively, you can implement a custom logic that observes the received image data and if it detects that the image data type is changed then delete and recreate the volume node.

---

<div class="post-metadata">

**Author:** ![lb123](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@lb123](https://discourse.slicer.org/u/lb123)\
**Post date:** [March 21, 2022, 2:57pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619/3 "2022-03-21T14:57:01Z")

</div>

Thank you for your reply.

It seems that the behaviour is not exactly the one you described.

This is my python code:

```auto
import pyigtl # pylint: disable=import-error
from time import sleep
import numpy as np
import cv2

server = pyigtl.OpenIGTLinkServer(port=18944)
cap = cv2.VideoCapture(0)

while True:
    if not server.is_connected():
        # Wait for client to connect
        sleep(0.1)
        continue
    ret,img = cap.read()
    img = np.transpose(img, axes=(2, 1, 0))
    print(img.shape) # it prints (3, 640, 480) so it is multi-component
    # Send image
    image_message = pyigtl.ImageMessage(img, device_name="Image")
    server.send_message(image_message, wait=True)

```

If in Slicer I type:

```auto
a = slicer.util.getNode("Image")
a

```

I get this output:  
`(MRMLCore.vtkMRMLScalarVolumeNode)000001F1864280A8`

So it seems a vtkMRMLScalarVolumeNode is created instead of a vtkMRMLVectorVolumeNode

What do you think about it?

Thank you

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 21, 2022, 3:05pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619/4 "2022-03-21T15:05:21Z")

</div>

> [@lb123](#):
>
> `print(img.shape) # it prints (3, 640, 480) so it is multi-component`

This defines a 3D scalar volume (with 3 slices), not a multi-component image. See the axes definition here:

> <https://github.com/lassoan/pyigtl/blob/f14d582f6fb3f406a706a0b5e928ed509300428d/pyigtl/messages.py#L255-L257>

Therefore, the shape of an RGB image array should be `(1, 480, 640, 3)`.

---

<div class="post-metadata">

**Author:** ![lb123](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@lb123](https://discourse.slicer.org/u/lb123)\
**Post date:** [March 21, 2022, 3:46pm UTC](https://discourse.slicer.org/t/show-color-images-in-slicer/22619/5 "2022-03-21T15:46:20Z")

</div>

Thank you very much! Now it works properly.

```auto

import pyigtl # pylint: disable=import-error
from time import sleep
import numpy as np
import cv2

server = pyigtl.OpenIGTLinkServer(port=18944)
cap = cv2.VideoCapture(0)

while True:
    if not server.is_connected():
        # Wait for client to connect
        sleep(0.1)
        continue
    ret,img = cap.read()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.rotate(img, cv2.ROTATE_180)
    img=np.expand_dims(img, axis=0) # Define multi-component image   
    print(img.shape) # Now it prints (1, 480, 640, 3)
    # Send image
    image_message = pyigtl.ImageMessage(img, device_name="Image")
    server.send_message(image_message, wait=True)

```
