Add fast grayscale morphology using sparse table data structure #3929
+1,133
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.
Abstract
This is a pull request for a program that calculates grayscale morphological operations quickly by using a data structure called a sparse table.
About sparse table
1D sparse table
For the data structure, see this: https://www.geeksforgeeks.org/sparse-table/
It is possible to quickly find the minimum (maximum) value of an array within a range whose length is a power of two.
Let's call the nth row of the sparse table the "row of depth n".$2^n$ of the original array.
The nth row contains the minimum (maximum) values in consecutive subsequences of length
Let us denote by$F$ the operation of computing the next row from a row in a sparse table.
2D sparse table
For the data structure, see this: https://www.geeksforgeeks.org/2d-range-minimum-query-in-o1/
Change the order of the subscripts to:
where$dr$ is the depth in the row direction, $dc$ is the depth in the column direction, $r$ is the index in the row direction, and $c$ is the index in the column direction.
$st[dr][dc]$ is a two-dimensional table that lists the minimum (maximum) values within a rectangle R of height $2^{dr}$ and width $2^{dc}$ that is slid across the original array A. In other words, it is the result of a morphological operation when A is a grayscale image and R is the structuring element.
In terms of the depth (dr, dc) of a sparse table, the operation of increasing the depth by +1 in the row and col directions will be denoted as Fr and Fc, respectively.
Sparse table morphology
Morphological operations are performed in the following steps:
At step 1, I implemented a method to find corners in a sparse table of structuring elements.
I don't know if it's the best method, but it's giving good results.
At step 4-1, st[dr][dc] is created by reusing st[dr'][dc'] of smaller size.
There are several possible reuse strategies, but I implemented a method that minimizes the total number of calculations of Fr and Fc.
This is reduced to the Rectilinear Steiner Arborescence Problem. (https://link.springer.com/article/10.1007/BF01758762).
Examples
With a structuring element of MORPH_ELLIPSE, size(5, 5)
The structuring element is decomposed into six rectangles.
The result of grouping in step 2 is as follows:
For size(4, 2), we need to calculate st[1][2], and for size(1, 4), we need to calculate st[2][0].
Performance measurement
When kernel is decomposited in advance:
From the graph, time complexity against diameter of kernel are as follows:
Including decomposition time:
When the kernel size approaches the same size as the target image, the kernel decomposition time becomes comparable to that of CV_RECT.