Skip to content

Commit acc32bb

Browse files
committed
クリーンアップ、変数名変更・・・
1 parent bd4e5ec commit acc32bb

File tree

3 files changed

+34
-57
lines changed

3 files changed

+34
-57
lines changed

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,9 @@ CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst,
8484

8585
//! @}
8686

87-
/*
88-
* Find a set of power-2-rectangles to cover the kernel.
89-
* power-2-rectangles is a rectangle whose height and width are both power of 2.
90-
* (The width and height values of returned rects ​​are the log2 of the actual values.)
91-
*/
9287
CV_EXPORTS_W std::vector<std::vector<std::vector<Point>>> genPow2RectsToCoverKernel(
9388
const Mat& kernel, int rowLim, int colLim);
94-
//
95-
/*
96-
* Plan the order to fill the required sparse table nodes.
97-
* returnd Mat type is Vec2b.
98-
*
99-
*/
100-
CV_EXPORTS_W Mat planSparseTableConstr(
89+
CV_EXPORTS_W Mat sparseTableFillPlanning(
10190
std::vector<std::vector<std::vector<Point>>> stNodeMap, int rowLim, int colLim);
10291

10392
CV_EXPORTS_W int log2(int n);
@@ -106,26 +95,3 @@ CV_EXPORTS_W int longestColRunLength(const Mat& kernel);
10695

10796
}} // cv::stMorph::
10897
#endif
109-
110-
/*
111-
112-
About sparse table:
113-
https://qiita.com/recuraki/items/0fcbc9e2abbc4fae5f62
114-
https://www.geeksforgeeks.org/sparse-table/
115-
116-
2D-sparse table:
117-
https://kopricky.github.io/code/DataStructure_Advanced/sparse_table_2D.html
118-
https://www.geeksforgeeks.org/2d-range-minimum-query-in-o1/
119-
120-
With 2D sparse table, we can get the min or max value in each power-of-2 rectangle quickly.
121-
122-
123-
1. Find a set of power-of-2 rectangles which covers the kernel.
124-
2. Group the rectangles by the size.
125-
3. Fill the sparse table
126-
127-
https://gobi-tk.hatenablog.com/entry/2024/09/18/012709
128-
https://link.springer.com/article/10.1007/BF01758762
129-
130-
131-
*/

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int longestColRunLength(const Mat& kernel)
7979
return maxLen;
8080
}
8181

82-
std::vector<Point> findSeeds(const Mat& stNode, int rowDepth, int colDepth)
82+
std::vector<Point> findP2RectCorners(const Mat& stNode, int rowDepth, int colDepth)
8383
{
8484
int rowOfst = 1 << rowDepth;
8585
int colOfst = 1 << colDepth;
@@ -97,7 +97,7 @@ std::vector<Point> findSeeds(const Mat& stNode, int rowDepth, int colDepth)
9797
if (row > 0 && stNode.at<uchar>(row - 1, col) == 1
9898
&& row + 1 < stNode.rows && stNode.at<uchar>(row + 1, col) == 1) continue;
9999

100-
// zignore if neighboring block is white; will be alive in deeper table
100+
// ignore if neighboring block is white
101101
if (col + colOfst < stNode.cols && stNode.at<uchar>(row, col + colOfst) == 1) continue;
102102
if (col - colOfst >= 0 && stNode.at<uchar>(row, col - colOfst) == 1) continue;
103103
if (row + rowOfst < stNode.rows && stNode.at<uchar>(row + rowOfst, col) == 1) continue;
@@ -109,43 +109,48 @@ std::vector<Point> findSeeds(const Mat& stNode, int rowDepth, int colDepth)
109109
return p2Rects;
110110
}
111111

112+
/*
113+
* Find a set of power-2-rectangles to cover the kernel.
114+
* power-2-rectangles is a rectangle whose height and width are both power of 2.
115+
*/
112116
std::vector<std::vector<std::vector<Point>>> genPow2RectsToCoverKernel(
113117
const Mat& kernel, int rowDepthLim, int colDepthLim)
114118
{
115119
CV_Assert(kernel.type() == CV_8UC1);
116120

117121
std::vector<std::vector<std::vector<Point>>> p2Rects;
118-
Mat stCache = kernel;
122+
Mat stNodeCache = kernel;
119123
for (int rowDepth = 0; rowDepth < rowDepthLim; rowDepth++)
120124
{
121-
Mat st = stCache.clone();
125+
Mat stNode = stNodeCache.clone();
122126
p2Rects.emplace_back(std::vector<std::vector<Point>>());
123127
for (int colDepth = 0; colDepth < colDepthLim; colDepth++)
124128
{
125-
p2Rects[rowDepth].emplace_back(findSeeds(st, rowDepth, colDepth));
129+
p2Rects[rowDepth].emplace_back(findP2RectCorners(stNode, rowDepth, colDepth));
126130
int colStep = 1 << colDepth;
127-
if (st.cols - colStep < 0) break;
128-
Rect s1(0, 0, st.cols - colStep, st.rows);
129-
Rect s2(colStep, 0, st.cols - colStep, st.rows);
130-
cv::min(st(s1), st(s2), st);
131+
if (stNode.cols - colStep < 0) break;
132+
Rect s1(0, 0, stNode.cols - colStep, stNode.rows);
133+
Rect s2(colStep, 0, stNode.cols - colStep, stNode.rows);
134+
cv::min(stNode(s1), stNode(s2), stNode);
131135
}
132136
int rowStep = 1 << rowDepth;
133-
if (stCache.rows - rowStep < 0) break;
134-
Rect s1(0, 0, stCache.cols, stCache.rows - rowStep);
135-
Rect s2(0, rowStep, stCache.cols, stCache.rows - rowStep);
136-
cv::min(stCache(s1), stCache(s2), stCache);
137+
if (stNodeCache.rows - rowStep < 0) break;
138+
Rect s1(0, 0, stNodeCache.cols, stNodeCache.rows - rowStep);
139+
Rect s2(0, rowStep, stNodeCache.cols, stNodeCache.rows - rowStep);
140+
cv::min(stNodeCache(s1), stNodeCache(s2), stNodeCache);
137141
}
138142

139-
// todo: implement greedy algorithm to minimize the rectangle set covering the kernel.
140-
141143
return p2Rects;
142144
}
143145

144146
Mat SolveRSAPGreedy(const Mat& initialMap)
145147
{
146148
/*
147-
* Solves the rectilinear steiner arborescence problem
149+
* Solves the rectilinear steiner arborescence problem greedy.
148150
* 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.
149154
*/
150155
CV_Assert(initialMap.type() == CV_8UC1);
151156
std::vector<Point> pos;
@@ -194,9 +199,16 @@ Mat SolveRSAPGreedy(const Mat& initialMap)
194199
return resMap;
195200
}
196201

197-
Mat planSparseTableConstr(
202+
Mat sparseTableFillPlanning(
198203
std::vector<std::vector<std::vector<Point>>> pow2Rects, int rowDepthLim, int colDepthLim)
199204
{
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+
200212
// list up required sparse table nodes.
201213
Mat stMap = Mat::zeros(rowDepthLim, colDepthLim, CV_8UC1);
202214
for (int rd = 0; rd < rowDepthLim; rd++)
@@ -219,7 +231,6 @@ void morphDfs(int minmax, Mat& st, Mat& dst,
219231
else cv::max(dst, st(rect), dst);
220232
}
221233

222-
// Fill col-direction first.
223234
if (stPlan.at<Vec2b>(rowDepth, colDepth)[1] == 1)
224235
{
225236
// col direction
@@ -258,7 +269,7 @@ void morphOp(Op minmax, InputArray _src, OutputArray _dst, InputArray kernel,
258269
std::vector<std::vector<std::vector<Point>>> pow2Rects
259270
= genPow2RectsToCoverKernel(_kernel, rowDepthLim, colDepthLim);
260271
Mat stPlan
261-
= planSparseTableConstr(pow2Rects, rowDepthLim, colDepthLim);
272+
= sparseTableFillPlanning(pow2Rects, rowDepthLim, colDepthLim);
262273

263274
Mat src = _src.getMat();
264275
_dst.create(_src.size(), _src.type());

modules/ximgproc/test/test_sparse_table_morphology.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ namespace {
1414
void assertArraysIdentical(InputArray ary1, InputArray ary2)
1515
{
1616
Mat xor = ary1.getMat() ^ ary2.getMat();
17-
ASSERT_EQ(cv::countNonZero(xor.reshape(1)), 0);
17+
CV_Assert(cv::countNonZero(xor.reshape(1)) == 0);
1818
}
1919
Mat im(int type)
2020
{
2121
int depth = CV_MAT_DEPTH(type);
2222
int ch = CV_MAT_CN(type);
2323
Mat img = imread(cvtest::TS::ptr()->get_data_path() + "cv/shared/lena.png");
24-
// ASSERT_EQ(img.type(), CV_8UC3);
24+
CV_Assert(img.type() == CV_8UC3);
2525

2626
if (ch == 1) cv::cvtColor(img, img, ColorConversionCodes::COLOR_BGR2GRAY, ch);
2727
if (depth == CV_8S) img /= 2;
@@ -342,7 +342,7 @@ void feture_planning(const Mat& mat)
342342
}
343343
}
344344

345-
auto r = stMorph::planSparseTableConstr(map, mat.rows, mat.cols);
345+
auto r = stMorph::sparseTableFillPlanning(map, mat.rows, mat.cols);
346346
VisualizePlanning(map, r);
347347
}
348348
TEST(ximgproc_StMorph_private, planning1) { feture_planning(knAsymm()); }

0 commit comments

Comments
 (0)