Where the usual graphcut constructs a graph out of the image's voxels, the label graphcut used irregular regions instead. This reduces memory consumption, accelerates the execution and leads mostly to better results.

Original image Foreground (red) and background (green) marker on original image

Let's assume we want to segment the ventricles in a brain scan (left image). First, we need some markers for foreground (red) and background (green). These can be seen in the right image and are usually either created manually or by another algorithm.

Creating the region image

The label graphcut requires a region image. MedPy supplies the required functionalities. First, we create a gradient magnitude version of our original image with


In [5]:
medpy_gradient.py resources/b0.nii.gz output/gradient.nii.gz

Which we then feed to the watershed algorith


In [6]:
medpy_watershed.py output/gradient.nii.gz output/watershed.nii.gz --mindist 10

Visually, the result process like this

Original image Gradient image Region image Region image (colored)

Performing the graphcut

Now we just have to supply our images to the script


In [7]:
medpy_graphcut_label.py output/gradient.nii.gz output/watershed.nii.gz resources/b0markers.nii.gz output/graphcut_label.nii.gz --boundary=stawiaski

And, after some waiting time, we obtain

, which is a pretty good approximation of the ventricles visible in the brain scan.


In [ ]: