Beginner Tutorial Data Objects 2D Contours CSO Label

Contour Example 6: Adding Labels to Contours

Introduction

In this example, we are adding a label to a contour. The label provides information about measurements and about the contour itself. The label remains connected to the contour and can be moved via mouse interactions.

Steps to do

Develop your network

Add a LocalImage and a View2D module to your workspace and connect them as shown below. Load the file ProbandT1.dcm from MeVisLab demo data. In order to create contours (CSOs), we need a SoView2DCSOExtensibleEditor module. It manages attached CSO editors, renderers and offers an optional default renderer for all types of CSOs.

The first CSO we want to create is a distance line. Add a SoCSODistanceLineEditor to the SoView2DCSOExtensibleEditor. It renders and interactively generates CSOs that consist of a single line segment. The line segment can be rendered as an arrow; it can be used to measure distances.

We are going to add some more editors later. In order to have the same look and feel for all types of CSOs, add a SoCSOVisualizationSettings module as seen below. The module is used to adjust visual parameters like color and line style for CSOs. Also add a CSOManager module to organize CSOs and CSOGroups within a network.

Initial Network

Initial Network

We are now able to create lines in the View2D. You can also modify the lines by dragging the seed points to a different location.

SoCSODistanceLineEditor

SoCSODistanceLineEditor

The created lines do neither provide any details about the length of your measurement, nor a unique ID to identify it in case of multiple CSOs.

Add a CSOLabelRenderer module to your network and connect it to a SoGroup. Also connect your SoCSODistanceLineEditor to the SoGroup as seen below. The ID of each CSO appears next to your distance lines. Moving the ID also shows the name of the contour.

CSOLabelRenderer

CSOLabelRenderer

We now want to customize the details to be shown for each distance line. Open the panel of the CSOLabelRenderer. You can see the two parameters labelString and labelName. The labelString is set to the ID of the CSO. The labelName is set to a static text and the label property of the CSO. The label can be defined in the module CSOManager. You can do this, but we are not defining a name for each contour in this example.

Enter the following to the panel of the CSOLabelRenderer module:

CSOLabelRenderer

labelString = f"Length {cso.getLength()}"  
labelName = f"Distance: {cso.getId()}"
deviceOffsetX = 0
deviceOffsetY = 0

We are setting the labelName to a static text showing the type of the CSO and the unique ID of the contour. We also define the labelString to the static description of the measurement and the length parameter of the CSO.

labelString and labelName

labelString and labelName

You can also round the length by using:

CSOLabelRenderer

labelString = f'Length: {cso.getLength():.2f} mm'

In order to see all possible parameters of a CSO, add a CSOInfo module to your network and connect it to the CSOManager. The geometric informations of the selected CSO from CSOManager can be seen there.

CSOInfo

CSOInfo

For labels shown on greyscale images, it makes sense to add a shadow. Open the panel of the SoCSOVisualizationSettings module and on tab Misc check the option Should render shadow. This increases the readability of your labels.

Ex6_NoShadow

Ex6_NoShadow

Ex6_Shadow

Ex6_Shadow

If you want to define your static text as a parameter in multiple labels, you can open the panel of the CSOLabelRenderer module and define text as User Data. The values can then be used in Python via userData.

User Data

User Data

You can also add multiple CSO editors to see the different options. Add the SoCSORectangleEditor module to your workspace and connect it to the SoGroup module. As we now have two different editors, we need to tell the CSOLabelRenderer which CSO is to be rendered. Open the panel of the SoCSODistanceLineEditor. You can see the field Extension Id set to distanceLine. Open the panel of the SoCSORectangleEditor. You can see the field Extension Id set to rectangle.

Extension ID

Extension ID

We currently defined the labelName and labelString for the distance line. If we want to define different labels for different types of CSOs, we have to change the CSOLabelRenderer Python script. Open the panel of the CSOLabelRenderer and change the Python code to the following:

CSOLabelRenderer

if cso.getSubType() == 'distanceLine':
  labelString =  f'{userData0} {cso.getLength():.2f} mm'
  labelName = userData1
  labelName += str(cso.getId())
elif cso.getSubType() == 'rectangle':
  labelString = f'{userData0} {cso.getLength():.2f} mm\n'
  labelString += f'{userData2} {cso.getArea():.2f} mm^2'
  labelName = userData3
  labelName += str(cso.getId())
deviceOffsetX = 0
deviceOffsetY = 0

SoCSORectangleEditor

SoCSORectangleEditor

If you now draw new CSOs, you will notice that you still always create distance lines. Open the panel of the SoView2DCSOExtensibleEditor. You can see that the Creator Extension Id is set to __default. By default, the first found eligible editor is used to create a new CSO. In our case this is the SoCSODistanceLineEditor.

SoCSORectangleEditor

SoCSORectangleEditor

Change Creator Extension Id to rectangle.

SoCSORectangleEditor & SoView2DCSOExtensibleEditor

SoCSORectangleEditor & SoView2DCSOExtensibleEditor

Newly created CSOs are now rectangles. The label values are shown as defined in the CSOLabelRenderer and show the length and the area of the rectangle.

Labeled Rectangle in View2D

Labeled Rectangle in View2D

You will find a lot more information in the CSOInfo module for your rectangles. The exact meaning of the values for each type of CSO is explained in the table below.

CSOInfo

CSOInfo

Parameters and meanings for all CSO types

CSO EditorPCA X Ext.PCA Y Ext.PCA Z Ext.LengthArea
SoCSOPointEditorn.a.n.a.n.a.n.a.n.a.
SoCSOAngleEditor
SoCSOArrowEditor
SoCSODistanceLineEditorLength (in mm)
SoCSODistancePolylineEditorLength of all lines (in mm)
SoCSOEllipseEditorPerimeter (in mm)Area (in mm2)
SoCSORectangleEditorLength of all sides (in mm)Area (in mm2)
SoCSOSplineEditor
SoCSOPolygonEditorLength of all lines (in mm)
SoCSOIsoEditor
SoCSOLiveWireEditor

Summary

  • Custom labels can be added to contours using the CSOLabelRenderer module.
  • Python scripting is used within the CSOLabelRenderer module to customize label content based on CSO types.
  • Visual properties can be adjusted within the CSOLabelRenderer and the SoCSOVisualizationSettings modules to improve label visibility and appearance.