Skip to content

Commit 239d8fb

Browse files
committed
implemented sparse table morphology
1 parent acd145f commit 239d8fb

File tree

5 files changed

+1133
-0
lines changed

5 files changed

+1133
-0
lines changed

modules/ximgproc/include/opencv2/ximgproc.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "ximgproc/color_match.hpp"
6464
#include "ximgproc/radon_transform.hpp"
6565
#include "ximgproc/find_ellipses.hpp"
66+
#include "ximgproc/sparse_table_morphology.hpp"
6667

6768

6869
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef __OPENCV_SPARSE_TABLE_MORPHOLOGY_HPP__
6+
#define __OPENCV_SPARSE_TABLE_MORPHOLOGY_HPP__
7+
8+
#include <opencv2/core.hpp>
9+
#include <vector>
10+
11+
namespace cv {
12+
namespace ximgproc {
13+
namespace stMorph {
14+
15+
//! @addtogroup imgproc_filter
16+
//! @{
17+
18+
/**
19+
* @struct kernelDecompInfo
20+
* @brief struct to hold the results of decomposing the structuring element.
21+
*/
22+
struct CV_EXPORTS kernelDecompInfo
23+
{
24+
//! rows of the original kernel.
25+
int rows;
26+
//! cols of the original kernel.
27+
int cols;
28+
//!
29+
//! set of rectangles to covers the kernel which height and width both are power of 2.
30+
//! point stRects[rd][cd](c,r) means a rectangle left-top (c,r), width 2^rd and height 2^cd.
31+
//!
32+
std::vector<std::vector<std::vector<Point>>> stRects;
33+
//!
34+
//! Vec2b Mat which sotres the order to calculate sparse table.
35+
//! The type of returned mat is Vec2b.
36+
//! * if path[dr][dc][0] == 1 then st[dr+1][dc] will be calculated from st[dr][dc].
37+
//! * if path[dr][dc][1] == 1 then st[dr][dc+1] will be calculated from st[dr][dc].
38+
//!
39+
Mat plan;
40+
//! anchor position of the kernel.
41+
Point anchor;
42+
//! Number of times erosion and dilation are applied.
43+
int iterations;
44+
};
45+
46+
/**
47+
* @brief Decompose the structuring element.
48+
*
49+
* @param kernel structuring element used for subsequent morphological operations.
50+
* @param anchor position of the anchor within the element.
51+
* default value (-1, -1) means that the anchor is at the element center.
52+
* @param iterations number of times is applied.
53+
*/
54+
CV_EXPORTS kernelDecompInfo decompKernel(InputArray kernel,
55+
Point anchor = Point(-1, -1), int iterations = 1);
56+
57+
/**
58+
* @brief Erodes an image with a kernelDecompInfo using spase table method.
59+
*
60+
* @param src input image
61+
* @param dst output image of the same size and type as src.
62+
* @param kdi pre-computated kernelDecompInfo structure.
63+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
64+
* @param borderValue border value in case of a constant border
65+
*/
66+
CV_EXPORTS void erode( InputArray src, OutputArray dst, kernelDecompInfo kdi,
67+
BorderTypes borderType = BORDER_CONSTANT,
68+
const Scalar& borderValue = morphologyDefaultBorderValue() );
69+
70+
/**
71+
* @brief Dilates an image with a kernelDecompInfo using spase table method.
72+
*
73+
* @param src input image;
74+
* @param dst output image of the same size and type as src.
75+
* @param kdi pre-computated kernelDecompInfo structure.
76+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
77+
* @param borderValue border value in case of a constant border
78+
*/
79+
CV_EXPORTS void dilate( InputArray src, OutputArray dst, kernelDecompInfo kdi,
80+
BorderTypes borderType = BORDER_CONSTANT,
81+
const Scalar& borderValue = morphologyDefaultBorderValue() );
82+
83+
/**
84+
* @brief Performs advanced morphological transformations with a kernelDecompInfo.
85+
*
86+
* @param src input image;
87+
* @param dst output image of the same size and type as src.
88+
* @param op all operations supported by cv::morphologyEx (except cv::MORPH_HITMISS)
89+
* @param kdi pre-computated kernelDecompInfo structure.
90+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
91+
* @param borderValue border value in case of a constant border
92+
*/
93+
CV_EXPORTS void morphologyEx( InputArray src, OutputArray dst, int op, kernelDecompInfo kdi,
94+
BorderTypes borderType = BORDER_CONSTANT,
95+
const Scalar& borderValue = morphologyDefaultBorderValue() );
96+
97+
/**
98+
* @brief Faster implementation of cv::erode with sparse table concept.
99+
*
100+
* @param src input image; the number of channels can be arbitrary, but the depth should be one of
101+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
102+
* @param dst output image of the same size and type as src.
103+
* @param kernel structuring element used for erosion; if `element=Mat()`, a `3 x 3` rectangular
104+
* structuring element is used. Kernel can be created using #getStructuringElement.
105+
* @param anchor position of the anchor within the element; default value (-1, -1) means that the
106+
* anchor is at the element center.
107+
* @param iterations number of times erosion is applied.
108+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
109+
* @param borderValue border value in case of a constant border
110+
*
111+
* @see cv::erode
112+
*/
113+
CV_EXPORTS void erode( InputArray src, OutputArray dst, InputArray kernel,
114+
Point anchor = Point(-1,-1), int iterations = 1,
115+
BorderTypes borderType = BORDER_CONSTANT,
116+
const Scalar& borderValue = morphologyDefaultBorderValue() );
117+
118+
/**
119+
* @brief Faster implementation of cv::dilate with sparse table concept.
120+
*
121+
* @param src input image; the number of channels can be arbitrary, but the depth should be one of
122+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
123+
* @param dst output image of the same size and type as src.
124+
* @param kernel structuring element used for dilation; if element=Mat(), a 3 x 3 rectangular
125+
* structuring element is used. Kernel can be created using #getStructuringElement
126+
* @param anchor position of the anchor within the element; default value (-1, -1) means that the
127+
* anchor is at the element center.
128+
* @param iterations number of times dilation is applied.
129+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not suported.
130+
* @param borderValue border value in case of a constant border
131+
*
132+
* @see cv::dilate
133+
*/
134+
CV_EXPORTS void dilate( InputArray src, OutputArray dst, InputArray kernel,
135+
Point anchor = Point(-1,-1), int iterations = 1,
136+
BorderTypes borderType = BORDER_CONSTANT,
137+
const Scalar& borderValue = morphologyDefaultBorderValue() );
138+
139+
/**
140+
* @brief Faster implementation of cv::morphologyEx with sparse table concept.
141+
142+
* @param src Source image. The number of channels can be arbitrary. The depth should be one of
143+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
144+
* @param dst Destination image of the same size and type as source image.
145+
* @param op Type of a morphological operation, see #MorphTypes
146+
* @param kernel Structuring element. It can be created using #getStructuringElement.
147+
* @param anchor Anchor position with the kernel. Negative values mean that the anchor is at the
148+
* kernel center.
149+
* @param iterations Number of times erosion and dilation are applied.
150+
* @param borderType Pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
151+
* @param borderValue Border value in case of a constant border. The default value has a special
152+
* meaning.
153+
* @note The number of iterations is the number of times erosion or dilatation operation will be applied.
154+
* For instance, an opening operation (#MORPH_OPEN) with two iterations is equivalent to apply
155+
* successively: erode -> erode -> dilate -> dilate (and not erode -> dilate -> erode -> dilate).
156+
*
157+
* @see cv::morphologyEx
158+
*/
159+
CV_EXPORTS void morphologyEx( InputArray src, OutputArray dst,
160+
int op, InputArray kernel,
161+
Point anchor = Point(-1,-1), int iterations = 1,
162+
BorderTypes borderType = BORDER_CONSTANT,
163+
const Scalar& borderValue = morphologyDefaultBorderValue() );
164+
//! @}
165+
166+
}}} // cv::ximgproc::stMorph::
167+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#include "perf_precomp.hpp"
6+
7+
namespace opencv_test {
8+
namespace {
9+
10+
typedef tuple<MorphTypes, MorphShapes> MorphTypes_MorphShapes_t;
11+
typedef TestBaseWithParam<MorphTypes_MorphShapes_t> SparseTableMorphologyPerfTest;
12+
13+
PERF_TEST_P(SparseTableMorphologyPerfTest, perf,
14+
testing::Combine(
15+
testing::Values(
16+
MORPH_ERODE, MORPH_DILATE, MORPH_OPEN, MORPH_CLOSE,
17+
MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT),
18+
testing::Values(MORPH_RECT, MORPH_CROSS, MORPH_ELLIPSE)
19+
) )
20+
{
21+
MorphTypes_MorphShapes_t params = GetParam();
22+
int seSize = 51;
23+
Size sz = sz1080p;
24+
MorphTypes op = std::get<0>(params);
25+
MorphShapes knType = std::get<1>(params);
26+
27+
Mat src(sz, CV_8UC3), dst(sz, CV_8UC3);
28+
Mat kernel = getStructuringElement(knType, cv::Size(2 * seSize + 1, 2 * seSize + 1));
29+
30+
declare.in(src, WARMUP_RNG).out(dst);
31+
32+
TEST_CYCLE_N(5)
33+
{
34+
cv::ximgproc::stMorph::morphologyEx(src, dst, op, kernel);
35+
}
36+
37+
SANITY_CHECK_NOTHING();
38+
}
39+
40+
}} // opencv_test:: ::

0 commit comments

Comments
 (0)