Skip to content

Commit 271149b

Browse files
committed
implemented sparse table morphology
1 parent acd145f commit 271149b

File tree

5 files changed

+945
-0
lines changed

5 files changed

+945
-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,165 @@
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+
* @brief struct to hold the results of decomposing the structuring element.
20+
*/
21+
class CV_EXPORTS KernelDecompInfo
22+
{
23+
public:
24+
/**
25+
* @param kernel structuring element used for subsequent morphological operations.
26+
* @param anchor position of the anchor within the element.
27+
* default value (-1, -1) means that the anchor is at the element center.
28+
* @param iterations number of times is applied.
29+
*/
30+
KernelDecompInfo(InputArray kernel,
31+
Point anchor = Point(-1, -1), int iterations = 1);
32+
33+
//! rows of the original kernel.
34+
int rows;
35+
//! cols of the original kernel.
36+
int cols;
37+
//!
38+
//! set of rectangles to covers the kernel which height and width both are power of 2.
39+
//! point stRects[rd][cd](c,r) means a rectangle left-top (c,r), width 2^rd and height 2^cd.
40+
//!
41+
std::vector<std::vector<std::vector<Point>>> stRects;
42+
//!
43+
//! Vec2b Mat which sotres the order to calculate sparse table.
44+
//! The type of returned mat is Vec2b.
45+
//! * if path[dr][dc][0] == 1 then st[dr+1][dc] will be calculated from st[dr][dc].
46+
//! * if path[dr][dc][1] == 1 then st[dr][dc+1] will be calculated from st[dr][dc].
47+
//!
48+
Mat plan;
49+
//! anchor position of the kernel.
50+
Point anchor;
51+
//! Number of times erosion and dilation are applied.
52+
int iterations;
53+
};
54+
55+
/**
56+
* @brief Erodes an image with a kernelDecompInfo using spase table method.
57+
*
58+
* @param src input image
59+
* @param dst output image of the same size and type as src.
60+
* @param kdi pre-computated kernelDecompInfo structure.
61+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
62+
* @param borderValue border value in case of a constant border
63+
*/
64+
CV_EXPORTS void erode( InputArray src, OutputArray dst, KernelDecompInfo kdi,
65+
BorderTypes borderType = BORDER_CONSTANT,
66+
const Scalar& borderValue = morphologyDefaultBorderValue() );
67+
68+
/**
69+
* @brief Dilates an image with a kernelDecompInfo using spase table method.
70+
*
71+
* @param src input image;
72+
* @param dst output image of the same size and type as src.
73+
* @param kdi pre-computated kernelDecompInfo structure.
74+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
75+
* @param borderValue border value in case of a constant border
76+
*/
77+
CV_EXPORTS void dilate( InputArray src, OutputArray dst, KernelDecompInfo kdi,
78+
BorderTypes borderType = BORDER_CONSTANT,
79+
const Scalar& borderValue = morphologyDefaultBorderValue() );
80+
81+
/**
82+
* @brief Performs advanced morphological transformations with a kernelDecompInfo.
83+
*
84+
* @param src input image;
85+
* @param dst output image of the same size and type as src.
86+
* @param op all operations supported by cv::morphologyEx (except cv::MORPH_HITMISS)
87+
* @param kdi pre-computated kernelDecompInfo structure.
88+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
89+
* @param borderValue border value in case of a constant border
90+
*/
91+
CV_EXPORTS void morphologyEx( InputArray src, OutputArray dst, int op, KernelDecompInfo kdi,
92+
BorderTypes borderType = BORDER_CONSTANT,
93+
const Scalar& borderValue = morphologyDefaultBorderValue() );
94+
95+
/**
96+
* @brief Faster implementation of cv::erode with sparse table concept.
97+
*
98+
* @param src input image; the number of channels can be arbitrary, but the depth should be one of
99+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
100+
* @param dst output image of the same size and type as src.
101+
* @param kernel structuring element used for erosion; if `element=Mat()`, a `3 x 3` rectangular
102+
* structuring element is used. Kernel can be created using #getStructuringElement.
103+
* @param anchor position of the anchor within the element; default value (-1, -1) means that the
104+
* anchor is at the element center.
105+
* @param iterations number of times erosion is applied.
106+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
107+
* @param borderValue border value in case of a constant border
108+
*
109+
* @see cv::erode
110+
*/
111+
CV_EXPORTS void erode( InputArray src, OutputArray dst, InputArray kernel,
112+
Point anchor = Point(-1,-1), int iterations = 1,
113+
BorderTypes borderType = BORDER_CONSTANT,
114+
const Scalar& borderValue = morphologyDefaultBorderValue() );
115+
116+
/**
117+
* @brief Faster implementation of cv::dilate with sparse table concept.
118+
*
119+
* @param src input image; the number of channels can be arbitrary, but the depth should be one of
120+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
121+
* @param dst output image of the same size and type as src.
122+
* @param kernel structuring element used for dilation; if element=Mat(), a 3 x 3 rectangular
123+
* structuring element is used. Kernel can be created using #getStructuringElement
124+
* @param anchor position of the anchor within the element; default value (-1, -1) means that the
125+
* anchor is at the element center.
126+
* @param iterations number of times dilation is applied.
127+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not suported.
128+
* @param borderValue border value in case of a constant border
129+
*
130+
* @see cv::dilate
131+
*/
132+
CV_EXPORTS void dilate( InputArray src, OutputArray dst, InputArray kernel,
133+
Point anchor = Point(-1,-1), int iterations = 1,
134+
BorderTypes borderType = BORDER_CONSTANT,
135+
const Scalar& borderValue = morphologyDefaultBorderValue() );
136+
137+
/**
138+
* @brief Faster implementation of cv::morphologyEx with sparse table concept.
139+
140+
* @param src Source image. The number of channels can be arbitrary. The depth should be one of
141+
* CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
142+
* @param dst Destination image of the same size and type as source image.
143+
* @param op Type of a morphological operation, see #MorphTypes
144+
* @param kernel Structuring element. It can be created using #getStructuringElement.
145+
* @param anchor Anchor position with the kernel. Negative values mean that the anchor is at the
146+
* kernel center.
147+
* @param iterations Number of times erosion and dilation are applied.
148+
* @param borderType Pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
149+
* @param borderValue Border value in case of a constant border. The default value has a special
150+
* meaning.
151+
* @note The number of iterations is the number of times erosion or dilatation operation will be applied.
152+
* For instance, an opening operation (#MORPH_OPEN) with two iterations is equivalent to apply
153+
* successively: erode -> erode -> dilate -> dilate (and not erode -> dilate -> erode -> dilate).
154+
*
155+
* @see cv::morphologyEx
156+
*/
157+
CV_EXPORTS void morphologyEx( InputArray src, OutputArray dst,
158+
int op, InputArray kernel,
159+
Point anchor = Point(-1,-1), int iterations = 1,
160+
BorderTypes borderType = BORDER_CONSTANT,
161+
const Scalar& borderValue = morphologyDefaultBorderValue() );
162+
//! @}
163+
164+
}}} // cv::ximgproc::stMorph::
165+
#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::tr1::get<0>(params);
25+
MorphShapes knType = std::tr1::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::stMorph::morphologyEx(src, dst, op, kernel);
35+
}
36+
37+
SANITY_CHECK_NOTHING();
38+
}
39+
40+
}} // opencv_test:: ::

0 commit comments

Comments
 (0)