CT Heart landmarking and segmentation (Mimics 22.0)

For this tutorial the Mimics project Heart.mcs from C:\MedData\DemoFiles will be used.

The tutorial shows how to prepare for applying the CT heart segmentation tool. A series of function calls is created and controls the script. One function is created for each operation that will be performed. The main operations are thresholding, landmarking, calculation of CT heart segmentation masks and calculation of 3D parts. Appropriate naming is used for the respective functions.

import mimics

# Import the numpy library that is useful for some operations
try:
	import numpy as np  # First need to install numpy package for Python. Type pip install numpy in your cmd
except ImportError as ie:
	print("================================================================")
	print("=== The 3rd party Python package 'numpy' is not installed! ===")
	print("=== To install it, use 'pip install numpy' in your cmd!    ===")
	print("================================================================")
	raise
# Define a shortcut
md = mimics.data
# Constants declaration
MASK_LEFT = "Left Heart"
MASK_RIGHT = "Right Heart"
LANDMARKS = (
	"RA", "RA",
	"LA", "LA",
	"LV", "LV",
	"RV", "RV",
	"AO", "AO",
	"PA", "PA",
)

SEED_RADIUS = dict(
	RA=10.0,
	LA=10.0,
	LV=10.0,
	RV=10.0,
	AO=8.0,
	PA=8.0,
)

SEED_COLOR = dict(
	RA=(0, 255, 255),
	LA=(255, 0, 255),
	LV=(255, 205, 205),
	RV=(145, 112, 255),
	AO=(255, 0, 0),
	PA=(0, 0, 255),
)

MASKS = ("RA", "LA", "LV", "RV", "AO", "PA")


def activate_thresholding(mask_name: str):
	m = mimics.segment.activate_thresholding()
	m.name = mask_name
	return


def indicate_landmark(pid: int):
	pdef = LANDMARKS[pid]
	try:
		coords = mimics.indicate_coordinate(message="Indicate {} ".format(pdef),
											confirm=False, show_message_box=True)
	except InterruptedError:
		return False

	mimics.view.navigate_to(coords)
	pnt = mimics.analyze.create_sphere_center_radius(coords, SEED_RADIUS[pdef.split()[0]])
	pnt.name = pdef
	pnt.color = tuple(np.array(SEED_COLOR[pdef.split()[0]]) / 255)
	return


def calc_ct_heart():
	left_heart = md.masks.find(MASK_LEFT)
	right_heart = md.masks.find(MASK_RIGHT)
	seeds = []
	for p in md.spheres:
		if p.name in LANDMARKS:
			seeds.append(p)
	mimics.segment.calculate_ct_heart_from_mask(left_heart, right_heart, seed_points=seeds)
	for p in md.masks:
		if p.name in MASKS:
			p.color = tuple(np.array(SEED_COLOR[p.name.split()[0]]) / 255)
	return


def create_3d_parts():
	for p in md.masks:
		if p.name in LANDMARKS:
			par = mimics.segment.calculate_part(p, "Medium")
			par.name = p.name
	return


def main():
	"""Main function."""

	# Open Heart.mcs project
	input_dir = r'C:\MedData\DemoFiles\Heart.mcs'
	mimics.file.open_project(input_dir)

	# Create masks for the left and right parts of the heart
	activate_thresholding(MASK_LEFT)
	activate_thresholding(MASK_RIGHT)

	# Indicate the landmarking
	for l in LANDMARKS:
		indicate_landmark(LANDMARKS.index(l))

	# Perform CT heart segmentation
	calc_ct_heart()

	# Create of the 3D parts
	create_3d_parts()


if __name__ == '__main__':
	main()