Class SegmentFelzenszwalbHuttenlocher04<T extends ImageBase<T>>
Implementation of Felzenszwalb-Huttenlocher [1] image segmentation algorithm. It is fast and uses a graph based heuristic with a tuning parameter that can be used to adjust region size. Regions are irregularly shaped.
It works by constructing a graph in which pixels are the nodes and edges describe the relationship between adjacent pixels. Each pixel has a weight that is determined from the difference in pixel values using the F-norm. The weights for all the edges are computed first and sorted from smallest to largest. In the next step the first edge in the list is selected and is tested to see if the nodes should be connected or not. This process is repeated for all edges. Small regions are then merged into large ones. For more details see [1].
NOTE:
- Region ID's in output image will NOT be sequential. You need to call
getRegionId()
to find out what the ID's are. - The output image can't be a sub-image because it is used internally and needs to be a continuous block of memory.
- Pixel connectivity rule and weight metric is by the
FhEdgeWeights
class passed in to the constructor. - To emulate the reference implementation use a
Algorithmic Changes:
This implementation is a faithful of the original and has been compared against the authors
reference source code. It does produce different results from the reference, some times significant, due to the
sensitivity of the algorithm to minor differences. The sensitivity arises from it being a greedy algorithm.
Here is a list of minor differences that cause different regions due to its sensitivity. The order in which edges with identical weights are sorted is arbitrary. The order that edges are computed is arbitrary. Floating point error in weight calculation gradually causes segmentation to diverge to a different solution even when given the same input.
One difference from the original is that Gaussian blur is not applied to the input image by default. That should be done prior to the image being passed in.
[1] Felzenszwalb, Pedro F., and Daniel P. Huttenlocher. "Efficient graph-based image segmentation." International Journal of Computer Vision 59.2 (2004): 167-181.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Describes the relationship between to adjacent pixels in the image. -
Field Summary
Modifier and TypeFieldDescriptionprotected DogArray<SegmentFelzenszwalbHuttenlocher04.Edge>
protected FastArray<SegmentFelzenszwalbHuttenlocher04.Edge>
protected GrayS32
protected DogArray_I32
protected DogArray_F32
-
Constructor Summary
ConstructorDescriptionSegmentFelzenszwalbHuttenlocher04
(float k, int minimumSize, FhEdgeWeights<T> computeWeights) Specifies tuning parameter -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Searches for root nodes in the graph and adds their size to the list of region sizes.void
configureApproximateSort
(int numBins) If this function is called the exact sort routine will not be used and instead an approximate routine will be used.protected int
find
(int child) Finds the root given child.List of ID's for each region in the segmented image.Number of pixels in each regionprotected void
initialize
(T input, GrayS32 output) Predeclares all memory required and sets data structures to their initial valuesprotected void
Follows the merge procedure output in [1].protected void
Look at the remaining regions and if there are any small ones marge them into a larger regionvoid
Segments the image.
-
Field Details
-
graph
-
edges
-
edgesNotMatched
-
regionSize
-
threshold
-
-
Constructor Details
-
SegmentFelzenszwalbHuttenlocher04
Specifies tuning parameter- Parameters:
k
- Tuning parameter. Larger regions are preferred for larger values of K. Try 300minimumSize
- Regions smaller than this are merged into larger regionscomputeWeights
- Function used to compute the weight for all the edges.
-
-
Method Details
-
configureApproximateSort
public void configureApproximateSort(int numBins) If this function is called the exact sort routine will not be used and instead an approximate routine will be used.- Parameters:
numBins
- Number of bins. Try 2000. More bins the more accurate it will be
-
process
Segments the image. Each region in the output image is given a unique ID. To find out what the ID of each region is callgetRegionId()
. To get a list of number of pixels in each region callgetRegionSizes()
.- Parameters:
input
- Input image. Not modified.output
- Output segmented image. Modified.
-
initialize
Predeclares all memory required and sets data structures to their initial values -
mergeRegions
protected void mergeRegions()Follows the merge procedure output in [1]. Two regions are merged together if the edge linking them has a weight which is ≤ the minimum of the heaviest edges in the two regions. -
mergeSmallRegions
protected void mergeSmallRegions()Look at the remaining regions and if there are any small ones marge them into a larger region -
find
protected int find(int child) Finds the root given child. If the child does not point directly to the parent find the parent and make the child point directly towards it. -
computeOutput
protected void computeOutput()Searches for root nodes in the graph and adds their size to the list of region sizes. Makes sure all other nodes in the graph point directly at their root. -
getRegionId
List of ID's for each region in the segmented image. -
getRegionSizes
Number of pixels in each region -
getInputType
-