# Perform graph cut

**URL:** https://discourse.itk.org/t/perform-graph-cut/3038
**Category:** Uncategorized
**Created:** [May 6, 2020, 5:28pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038 "2020-05-06T17:28:44Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 6, 2020, 5:28pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/1 "2020-05-06T17:28:44Z")

</div>

I intend to use graph cut segmentation on a VTK volume, and I saw few examples on github. But all these examples are using images (background and foreground images) as masking for graph cut … what should I use for VTK volume, because in this case I don’t have “images” as mask …

Do you know any place from where I can inspire for this task ?

Thank you.

P.S. Or, what is the best approach to cut a little piece from a vtk volume ?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 6, 2020, 5:39pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/2 "2020-05-06T17:39:47Z")

</div>

Having “foreground” and “background” seeds is the essence of graph-cut methods. They can be somewhat adapted, e.g. to only have the user place one seed point in [2D](https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0031064) or [3D](https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0093389).

If you don’t have seeds, you need to construct them somehow. Or just don’t use graph cuts.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 6, 2020, 5:43pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/3 "2020-05-06T17:43:19Z")

</div>

> [@flaviu2](#):
>
> best approach to cut a little piece from a vtk volume

You mean 3D volume? If you have a VTK image you need to [convert it to ITK](https://itk.org/ITKExamples/src/Bridge/VtkGlue/ConvertvtkImageDataToAnitkImage/Documentation.html) in order to apply ITK filters.

We might have advice if you show us example input image and desired result (you can do that manually). Or at least describe it more thoroughly.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 7, 2020, 8:59am UTC](https://discourse.itk.org/t/perform-graph-cut/3038/4 "2020-05-07T08:59:55Z")

</div>

I have a **vtkVolume** , and I have methods that convert **vtkVolume** into **SimpleITK** (_sitk::Image_) not ITK !!! because working with SimpleITK is much faster than ITK 🙂 .

I can take any point from 3d **vtkVolume** through **vtkVolumePicker** or **vtkCellPicker** , whatever … now, I don’t know how to use them into graph cut algorithm, because I’ve found on github some code from here I can inspire, but that code uses images as foreground and background, but not points…

Here is my model: [https://github.com/daviddoria/ImageGraphCutSegmentation](https://github.com/daviddoria/ImageGraphCutSegmentation)

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 7, 2020, 9:15am UTC](https://discourse.itk.org/t/perform-graph-cut/3038/5 "2020-05-07T09:15:04Z")

</div>

Here is my _vtkVolume_ sample (a _CBCT_):

![image](https://discourse.itk.org/uploads/default/original/2X/f/f238f488a161771c275e87d0dd2ff530fb9a7fb5.jpeg)

I can pick a 3D point as I said before with **vtkCellPicker** or **vtkVolumePicker** , but I don’t know how to use them to cut a tooth from there … I was thinking to use graph cut …

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 7, 2020, 1:59pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/6 "2020-05-07T13:59:51Z")

</div>

Get 3D coordinates of the point from VTK picking, and then you should be able to use them in ITK as physical coordinates. If you are using VTK 8.2 or earlier, you will need to take [special care](https://vtk.org/Wiki/index.php?title=ITK/Examples/WishList/IO/itkVtkImageConvertDICOM&oldid=49125) if direction matrix is non-identity.

Use [TransformPhysicalPointToIndex](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/DataRepresentation/Image/Image4.cxx) if you want to get ITK index. Then put a seed there or around a small neighborhood of that voxel.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [May 8, 2020, 12:09am UTC](https://discourse.itk.org/t/perform-graph-cut/3038/7 "2020-05-08T00:09:10Z")

</div>

@flaviu2 before you start implementing anything, I would recommend to try existing tools in various open-source software. Once you confirmed what algorithm fulfills your needs, you can start thinking about the final implementation.

3D Slicer has many segmentation tools (automatic, semi-automatic, and manual) in its Segment Editor module that you can easily try. For example, its “Grow from seeds” effect uses a variant of grow-cut algorithm that is optimized for 3D medical image segmentation. It is expected to work better than a generic graph cut implementation. See tutorials of this and the many other segmentation tools [here](https://www.slicer.org/wiki/Documentation/Nightly/Training#Segmentation). You can also easily make new segmentation tools out of SimpleITK and VTK filters (you can use existing effects as examples - they are all based on ITK and VTK filters).

For bone segmentation, you’ll also need tools to solidify segments (to remove internal holes and surface discontinuities).

There are a number of [Slicer forum](https://discourse.slicer.org) members that are experienced in teeth and CMF bone segmentation, so it you have a specific clinical problem to solve then you may ask the experts there.

---

<div class="post-metadata">

### Author: ![lbones](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@lbones](https://discourse.itk.org/u/lbones)
#### Post date: [June 13, 2021, 1:45pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/8 "2021-06-13T13:45:41Z")

</div>

Thank you both for your work @lassoan and @dzenanz.

I’m trying to implement FastGrowCut for my project, using C++ in MSVC.

I’m currently using both VTK and ITK across the project, this section is in the middle of my pipeline so I could use either. I’m just having trouble building either VTK version in slicer or the ITK version that I can see @dzenanz has been working on. I think my issue is similar in both

VTK/Slicer: Using the source Andras linked previously [Algorithm used for growing seeds - Support - 3D Slicer Community](https://discourse.slicer.org/t/algorithm-used-for-growing-seeds/11817)  
I am trying to compile growcut.cxx (attached - with error message)

Using vtkimagegrowcutsegment.h and vtkimagegrowcutsegment.hxx  
If I try to compile the code as is I get

- Cannot open include file: ‘vtkSlicerSegmentationsModuleLogicExport.h’: No such file or directory

I have tried replacing:

class VTK\_SLICER\_SEGMENTATIONS\_LOGIC\_EXPORT vtkImageGrowCutSegment : public vtkImageAlgorithm  
with  
#include “vtkCommonExecutionModelModule.h”  
//…  
class VTKCOMMONEXECUTIONMODEL\_EXPORT vtkImageGrowCutSegment : public vtkImageAlgorithm

and then I get the error attached.  
The program compiles successfully if I comment out the growcut block of text, to just read and write a file

In ITK  
Similarly the code wouldn’t compile out of the box, I removed some of the wrapping files which I don’t need and tried to simplify the CMake file  
The compilation fails saying that it cannot find #include “GrowCutExport.h”

VTK\_VERSION: 8.2.0  
ITK 5.2  
MSVC-X64  
[GrowCut Error.txt](https://discourse.itk.org/uploads/short-url/g5gqH2trDscOv62KtqwbfVzqMzi.txt) (2.7 KB)

I can’t locate either of these .h files on gitbub, are they not actually files with code but part of the macro exporting function? Do you have any tips on how I can get this code working

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [June 13, 2021, 3:06pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/9 "2021-06-13T15:06:13Z")

</div>

[GrowCut](https://github.com/InsightSoftwareConsortium/ITKGrowCut) can be compiled like any other remote module:

1. compile ITK
2. compile remote module pointing to the ITK build tree
3. compile your app pointing to ITK build tree (you should be able to include/use stuff from the remote module without any issue or any additional setup)

`GrowCutExport.h` is a file generated during compilation of the remote module.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [June 13, 2021, 6:17pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/10 "2021-06-13T18:17:15Z")

</div>

The header file that you miss is generated automatically (contains the export macro). If you only need to use the class in the same target then you can just remove the export macro and the include directive.

In the long term I would definitely recommend using the filter in ITK. I think currently, the ITK implementation still uses a very old version of this algorithm that is several times slower, uses much more memory, and lacks the very useful masking feature, compared than the one in Slicer.

@dzenanz it would be great if you could port all the optimizations from Slicer (or simply replace the internals of the current implementation with Slicer’s). Then we could switch to the ITK implementation and implement all further improvements there (adding spatial smoothness constraint, adding an option to provide extra cost image, improve multithreading, etc).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [June 13, 2021, 8:33pm UTC](https://discourse.itk.org/t/perform-graph-cut/3038/11 "2021-06-13T20:33:09Z")

</div>

> [@lassoan](#):
>
> port all the optimizations from Slicer

I definitely plan that. We are now in the funding gap between year 1 and year 2. As soon as the funding for Y2 comes, I will pick up that work.
