CutSegment finds a minimal cut in an image where every image pixel represents a graph vertex. We assume a 4-connected grid (cf. CutGrid) and that the capacity of an edge only depends on the image information at the two linked pixels.
In order to compute the cut, the following steps have to be performed:
In the following sections, we explain these steps with respect to the graph sketched in the images above.
When we define the capacity of each edge, we can either do this by using the GAC approach already implemented or by overwriting the CutSegment::gradient(double,double) method. In this example we follow the latter approach:
If you also want to handle color images correctly, you should also overwrite the CutSegment::gradient(double[3],double[3]) method.
Now, we have to compute the maximum flow. In order to do this via the newly defined MyCutSegment, we have to load the image and the preselected source and sink set into MyCutSegment:
We assumed here, that the image is already loaded row-wise in the variable I. In L, a pre-labeling is stored row-wise where the value '0' indicates the pre-selected source set an '128' the pre-selected sink-set. Both areas have to form connected components within L. Otherwise, only one component is considered.
Now that the problem is formulated, we have to compute the value of the maximum flow. This is the most time consuming method of the class and is called via
For most applications, we are not only interested in the value of the minimum cut, but also in the segmentation itself. The classical way in describing this is to assign a binary labeling to each pixel. This labeling can be provided via CutSegment::getLabel or CutSegment::getLabels
The complete code can be seen here: cutsegment.cpp.