-
Couldn't load subscription status.
- Fork 5.9k
Add fast grayscale morphology using sparse table data structure #3929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@kyaco
kyaco
force-pushed
the
stMorph
branch
4 times, most recently
from
April 29, 2025 17:23
271149b to
72e534e
Compare
@kyaco
kyaco
force-pushed
the
stMorph
branch
6 times, most recently
from
May 6, 2025 15:24
b553e03 to
5295591
Compare
Hi, @asmorkalov. Please review this pr.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Uh oh!
There was an error while loading. Please reload this page.
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:
image
For size(4, 2), we need to calculate st[1][2], and for size(1, 4), we need to calculate st[2][0].
image
Performance measurement
When kernel is decomposited in advance:
image
From the graph, time complexity against diameter of kernel are as follows:
Including decomposition time:
image
When the kernel size approaches the same size as the target image, the kernel decomposition time becomes comparable to that of CV_RECT.