Skip to content

Commit f7a21c2

Browse files
committed
コメント追記・修正
1 parent cac39c0 commit f7a21c2

File tree

2 files changed

+77
-24
lines changed

2 files changed

+77
-24
lines changed

modules/ximgproc/include/opencv2/ximgproc/sparse_table_morphology.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,81 @@ namespace stMorph {
1414
//! @addtogroup imgproc_filter
1515
//! @{
1616

17+
/**
18+
* @struct kernelDecompInfo
19+
* @brief struct to hold the results of decomposing the structuring element.
20+
*/
1721
typedef struct _kernelDecompInfo
1822
{
23+
//! rows of the original kernel.
1924
int rows;
25+
//! cols of the original kernel.
2026
int cols;
27+
//!
28+
//! set of rectangles to covers the kernel which height and width both are power of 2.
29+
//! point stRects[rd][cd](c,r) means a rectangle left-top (c,r), width 2^rd and height 2^cd.
30+
//!
2131
std::vector<std::vector<std::vector<Point>>> stRects;
32+
//!
33+
//! Vec2b Mat which sotres the order to calculate sparse table.
34+
//! The type of returned mat is Vec2b.
35+
//! * if path[dr][dc][0] == 1 then st[dr+1][dc] will be calculated from st[dr][dc].
36+
//! * if path[dr][dc][1] == 1 then st[dr][dc+1] will be calculated from st[dr][dc].
37+
//!
2238
Mat plan;
39+
//! anchor position of the kernel.
2340
Point anchor;
41+
//! Number of times erosion and dilation are applied.
2442
int iterations;
2543
} kernelDecompInfo;
2644

45+
/**
46+
* @brief Decompose the structuring element.
47+
*
48+
* @param kernel structuring element used for subsequent morphological operations.
49+
* @param anchor position of the anchor within the element.
50+
* default value (-1, -1) means that the anchor is at the element center.
51+
* @param iterations number of times is applied.
52+
*/
2753
CV_EXPORTS_W kernelDecompInfo decompKernel(InputArray kernel,
2854
Point anchor = Point(-1, -1), int iterations = 1);
2955

56+
/**
57+
* @brief Erodes an image with a kernelDecompInfo using spase table method.
58+
*
59+
* @param src input image
60+
* @param dst output image of the same size and type as src.
61+
* @param kdi pre-computated kernelDecompInfo structure.
62+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
63+
* @param borderValue border value in case of a constant border
64+
*/
3065
CV_EXPORTS_W void erode( InputArray src, OutputArray dst, kernelDecompInfo kdi,
3166
BorderTypes borderType = BORDER_CONSTANT,
3267
const Scalar& borderValue = morphologyDefaultBorderValue() );
3368

69+
/**
70+
* @brief Dilates an image with a kernelDecompInfo using spase table method.
71+
*
72+
* @param src input image;
73+
* @param dst output image of the same size and type as src.
74+
* @param kdi pre-computated kernelDecompInfo structure.
75+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
76+
* @param borderValue border value in case of a constant border
77+
*/
3478
CV_EXPORTS_W void dilate( InputArray src, OutputArray dst, kernelDecompInfo kdi,
3579
BorderTypes borderType = BORDER_CONSTANT,
3680
const Scalar& borderValue = morphologyDefaultBorderValue() );
3781

82+
/**
83+
* @brief Performs advanced morphological transformations with a kernelDecompInfo.
84+
*
85+
* @param src input image;
86+
* @param dst output image of the same size and type as src.
87+
* @param op all operations supported by cv::morphologyEx (except cv::MORPH_HITMISS)
88+
* @param kdi pre-computated kernelDecompInfo structure.
89+
* @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
90+
* @param borderValue border value in case of a constant border
91+
*/
3892
CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst, int op, kernelDecompInfo kdi,
3993
BorderTypes borderType = BORDER_CONSTANT,
4094
const Scalar& borderValue = morphologyDefaultBorderValue() );

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace cv {
1111
namespace stMorph {
1212

13+
#pragma region decompKernel
14+
1315
// normalizeAnchor; Copied from filterengine.hpp.
1416
static inline Point normalizeAnchor(Point anchor, Size ksize)
1517
{
@@ -21,11 +23,6 @@ static inline Point normalizeAnchor(Point anchor, Size ksize)
2123
return anchor;
2224
}
2325

24-
enum Op
25-
{
26-
Min, Max
27-
};
28-
2926
int log2(int n)
3027
{
3128
int ans = -1;
@@ -97,7 +94,7 @@ std::vector<Point> findP2RectCorners(const Mat& stNode, int rowDepth, int colDep
9794
if (row > 0 && stNode.at<uchar>(row - 1, col) == 1
9895
&& row + 1 < stNode.rows && stNode.at<uchar>(row + 1, col) == 1) continue;
9996

100-
// ignore if neighboring block is white
97+
// ignore if deeper cell is white
10198
if (col + colOfst < stNode.cols && stNode.at<uchar>(row, col + colOfst) == 1) continue;
10299
if (col - colOfst >= 0 && stNode.at<uchar>(row, col - colOfst) == 1) continue;
103100
if (row + rowOfst < stNode.rows && stNode.at<uchar>(row + rowOfst, col) == 1) continue;
@@ -143,15 +140,11 @@ std::vector<std::vector<std::vector<Point>>> genPow2RectsToCoverKernel(
143140
return p2Rects;
144141
}
145142

143+
/*
144+
* Solves the rectilinear steiner arborescence problem greedy.
145+
*/
146146
Mat SolveRSAPGreedy(const Mat& initialMap)
147147
{
148-
/*
149-
* Solves the rectilinear steiner arborescence problem greedy.
150-
* https://link.springer.com/article/10.1007/BF01758762
151-
*
152-
* Following implementation is O(n^3)-time algorithm
153-
* which is different from the mothod proposed in the paper.
154-
*/
155148
CV_Assert(initialMap.type() == CV_8UC1);
156149
std::vector<Point> pos;
157150
for (int r = 0; r < initialMap.rows; r++)
@@ -193,7 +186,7 @@ Mat SolveRSAPGreedy(const Mat& initialMap)
193186
resMap.at<Vec2b>(row, maxX)[0] = 1;
194187

195188
pos[maxI] = Point(maxX, maxY);
196-
swap(pos[maxJ], pos[pos.size() - 1]);
189+
std::swap(pos[maxJ], pos[pos.size() - 1]);
197190
pos.pop_back();
198191
}
199192
return resMap;
@@ -202,13 +195,6 @@ Mat SolveRSAPGreedy(const Mat& initialMap)
202195
Mat sparseTableFillPlanning(
203196
std::vector<std::vector<std::vector<Point>>> pow2Rects, int rowDepthLim, int colDepthLim)
204197
{
205-
/*
206-
* Plan the order to fill the required 2d-sparse-table nodes.
207-
* The type of returned mat is Vec2b.
208-
* if path[dr][dc][0] == 1 then st[dr+1][dc] will be calculated from st[dr][dc].
209-
* if path[dr][dc][1] == 1 then st[dr][dc+1] will be calculated from st[dr][dc].
210-
*/
211-
212198
// list up required sparse table nodes.
213199
Mat stMap = Mat::zeros(rowDepthLim, colDepthLim, CV_8UC1);
214200
for (int rd = 0; rd < rowDepthLim; rd++)
@@ -234,9 +220,6 @@ kernelDecompInfo decompKernel(InputArray kernel, Point anchor, int iterations)
234220
{
235221
_kernel.at<uchar>(0, 0) = 1;
236222
}
237-
// Fix anchor to the center of the kernel.
238-
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
239-
240223

241224
int rowDepthLim = log2(longestRowRunLength(_kernel)) + 1;
242225
int colDepthLim = log2(longestColRunLength(_kernel)) + 1;
@@ -246,11 +229,21 @@ kernelDecompInfo decompKernel(InputArray kernel, Point anchor, int iterations)
246229
Mat stPlan
247230
= sparseTableFillPlanning(pow2Rects, rowDepthLim, colDepthLim);
248231

232+
// Fix anchor to the center of the kernel.
249233
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
250234

251235
return { _kernel.rows, _kernel.cols, pow2Rects, stPlan, anchor, iterations };
252236
}
253237

238+
#pragma endregion
239+
240+
#pragma region st-morphology
241+
242+
enum Op
243+
{
244+
Min, Max
245+
};
246+
254247
void morphDfs(int minmax, Mat& st, Mat& dst,
255248
std::vector<std::vector<std::vector<Point>>> row2Rects, const Mat& stPlan,
256249
int rowDepth, int colDepth)
@@ -414,6 +407,10 @@ void morphologyEx(InputArray src, OutputArray dst, int op, kernelDecompInfo kdi,
414407
}
415408
}
416409

410+
#pragma endregion
411+
412+
#pragma region cv-morphology
413+
417414
void erode(InputArray src, OutputArray dst, InputArray kernel,
418415
Point anchor, int iterations,
419416
BorderTypes borderType, const Scalar& borderVal)
@@ -438,4 +435,6 @@ void morphologyEx(InputArray src, OutputArray dst, int op,
438435
morphologyEx(src, dst, op, kdi, borderType, borderVal);
439436
}
440437

438+
#pragma endregion
439+
441440
}} // cv::stMorph::

0 commit comments

Comments
 (0)