Show color images in Slicer

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

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.

Thank you for your reply.

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

This is my python code:

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:

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

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

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

Thank you very much! Now it works properly.


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)
1 Like