Skip to content

Commit 1b4d408

Browse files
committed
内部のメソッドを公開してテスト作成
1 parent 067d7bc commit 1b4d408

File tree

3 files changed

+112
-192
lines changed

3 files changed

+112
-192
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define __OPENCV_SPARSE_TABLE_MORPHOLOGY_HPP__
77

88
#include <opencv2/core.hpp>
9+
#include <vector>
910

1011
namespace cv {
1112
namespace ximgproc {
@@ -76,6 +77,50 @@ CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst, int op, InputAr
7677

7778
//! @}
7879

80+
// normalizeAnchor; Copied from filterengine.hpp.
81+
static inline Point normalizeAnchor(Point anchor, Size ksize)
82+
{
83+
if (anchor.x == -1)
84+
anchor.x = ksize.width / 2;
85+
if (anchor.y == -1)
86+
anchor.y = ksize.height / 2;
87+
CV_Assert(anchor.inside(Rect(0, 0, ksize.width, ksize.height)));
88+
return anchor;
89+
}
90+
91+
enum Dim
92+
{
93+
Col, Row
94+
};
95+
96+
struct StStep
97+
{
98+
StStep(int dimR, int dimC, Dim _ax)
99+
{
100+
dimRow = dimR;
101+
dimCol = dimC;
102+
ax = _ax;
103+
}
104+
int dimRow;
105+
int dimCol;
106+
Dim ax;
107+
};
108+
109+
/*
110+
* Find a smaller set of power-of-2 rectangles to cover the kernel.
111+
* - The width and the height of each rectangles are power of 2.
112+
* - Overlappings of rectangles are allowed.
113+
*
114+
* this method may be applied for the covering polygon problem with rectangle.
115+
* https://www.sciencedirect.com/science/article/pii/S0019995884800121z
116+
*/
117+
CV_EXPORTS_W std::vector<Rect> genPow2RectsToCoverKernel(InputArray kernel);
118+
//
119+
///*
120+
//* Plan the order to calculate the sparse table nodes.
121+
//*/
122+
CV_EXPORTS_W std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<bool>> requiredSparseTableNodeMap);
123+
79124
} // namespace st
80125
} // namespace ximgproc
81126
} // namespace cv

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,7 @@ namespace cv {
1414
namespace ximgproc {
1515
namespace stMorph {
1616

17-
// normalizeAnchor; Copied from filterengine.hpp.
18-
static inline Point normalizeAnchor(Point anchor, Size ksize)
19-
{
20-
if (anchor.x == -1)
21-
anchor.x = ksize.width / 2;
22-
if (anchor.y == -1)
23-
anchor.y = ksize.height / 2;
24-
CV_Assert(anchor.inside(Rect(0, 0, ksize.width, ksize.height)));
25-
return anchor;
26-
}
27-
28-
enum Dim
29-
{
30-
Col, Row
31-
};
32-
33-
struct StStep
34-
{
35-
StStep(int dimR, int dimC, Dim _ax)
36-
{
37-
dimRow = dimR;
38-
dimCol = dimC;
39-
ax = _ax;
40-
}
41-
int dimRow;
42-
int dimCol;
43-
Dim ax;
44-
};
45-
46-
/*
47-
* Find a smaller set of power-of-2 rectangles to cover the kernel.
48-
* - The width and the height of each rectangles are power of 2.
49-
* - Overlappings of rectangles are allowed.
50-
*/
51-
static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
17+
std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
5218
{
5319
CV_Assert(_kernel.type() == CV_8UC1);
5420

@@ -119,11 +85,14 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
11985
std::vector<Rect> p2Rects;
12086
for (int rowDepth = 0; rowDepth <= log2[kernel.rows]; rowDepth++)
12187
{
122-
int rowSkip = (1 << rowDepth) - 1;
88+
int rowOfst = 1 << rowDepth;
89+
int rowSkip = rowOfst - 1;
12390
int rowLim = kernel.rows - rowSkip;
91+
int x = rowOfst * kernel.cols;
12492
for (int colDepth = 0; colDepth <= log2[kernel.cols]; colDepth++)
12593
{
126-
int colSkip = (1 << colDepth) - 1;
94+
int colOfst = 1 << colDepth;
95+
int colSkip = colOfst - 1;
12796
int colLim = kernel.cols - colSkip;
12897

12998
uchar* ptr = st[rowDepth][colDepth].ptr();
@@ -139,10 +108,10 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
139108
if (row > 0 && ptr[-kernel.cols] && row < rowLim && ptr[kernel.cols] == 1) continue;
140109

141110
// ignore one of neighbor block is white; will be alive in deeper table
142-
if (col + (1 << colDepth) <= colLim && ptr[1 << colDepth] == 1) continue;
143-
if (col - (1 << colDepth) >= 0 && ptr[-(1 << colDepth)] == 1) continue;
144-
if (row + (1 << rowDepth) <= rowLim && ptr[(1 << rowDepth) * kernel.cols] == 1) continue;
145-
if (row - (1 << rowDepth) >= 0 && ptr[-(1 << rowDepth) * kernel.cols] == 1) continue;
111+
if (col + colOfst <= colLim && ptr[colOfst] == 1) continue;
112+
if (col - colOfst >= 0 && ptr[-colOfst] == 1) continue;
113+
if (row + rowOfst <= rowLim && ptr[x] == 1) continue;
114+
if (row - rowOfst >= 0 && ptr[-x] == 1) continue;
146115

147116
p2Rects.emplace_back(col, row, colDepth, rowDepth);
148117
}
@@ -154,6 +123,8 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
154123
return p2Rects;
155124
}
156125

126+
std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<bool>> sparseMatMap)
127+
{
157128
/*
158129
*
159130
*
@@ -163,8 +134,6 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
163134
* https://link.springer.com/article/10.1007/BF01758762
164135
*
165136
*/
166-
static std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<bool>> sparseMatMap)
167-
{
168137
auto comparePos = [](Point lp, Point rp) {
169138
int diffx = lp.x - rp.x;
170139
int diffy = lp.y - rp.y;
@@ -174,15 +143,12 @@ static std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<b
174143
return diffy < 0;
175144
};
176145
std::priority_queue<Point, std::vector<Point>, decltype(comparePos)> points{ comparePos };
146+
sparseMatMap[0][0] = true;
177147
for (int r = 0; r < sparseMatMap.size(); r++)
178-
{
179148
for (int c = 0; c < sparseMatMap[r].size(); c++)
180-
{
181149
if (sparseMatMap[r][c]) points.push(Point(c, r));
182-
}
183-
}
184150

185-
std::vector<StStep> ans;
151+
std::vector<StStep> plan;
186152
while (points.size() >= 2)
187153
{
188154
Point p1 = points.top();
@@ -197,19 +163,16 @@ static std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<b
197163
points.push(Point(newX, newY));
198164
}
199165

200-
for (int col = p1.x - 1; col >= newX; col--) ans.emplace_back(p1.y, col, Dim::Col);
201-
for (int row = p1.y - 1; row >= newY; row--) ans.emplace_back(row, p1.x, Dim::Row);
202-
for (int col = p2.x - 1; col >= newX; col--) ans.emplace_back(p2.y, col, Dim::Col);
203-
for (int row = p2.y - 1; row >= newY; row--) ans.emplace_back(row, p2.x, Dim::Row);
166+
for (int col = p1.x - 1; col >= newX; col--) plan.emplace_back(p1.y, col, Dim::Col);
167+
for (int row = p1.y - 1; row >= newY; row--) plan.emplace_back(row, p1.x, Dim::Row);
168+
for (int col = p2.x - 1; col >= newX; col--) plan.emplace_back(p2.y, col, Dim::Col);
169+
for (int row = p2.y - 1; row >= newY; row--) plan.emplace_back(row, p2.x, Dim::Row);
204170
}
205-
Point p1 = points.top();
206-
for (int col = p1.x - 1; col >= 0; col--) ans.emplace_back(p1.y, col, Dim::Col);
207-
for (int row = p1.y - 1; row >= 0; row--) ans.emplace_back(row, 0, Dim::Row);
208-
std::reverse(ans.begin(), ans.end());
209-
return ans;
171+
std::reverse(plan.begin(), plan.end());
172+
return plan;
210173
}
211174

212-
static void makeMinSparseTableMat(InputArray src, OutputArray dst, int rowStep, int colStep)
175+
void makeMinSparseTableMat(InputArray src, OutputArray dst, int rowStep, int colStep)
213176
{
214177
CV_Assert(rowStep * colStep == 0); // one of "rowStep" or "colStep" is required to be 0.
215178

@@ -243,7 +206,8 @@ static void makeMinSparseTableMat(InputArray src, OutputArray dst, int rowStep,
243206
dstPtr += borderSkipStep;
244207
}
245208
}
246-
static void makeMaxSparseTableMat(InputArray src, OutputArray dst, int rowStep, int colStep)
209+
210+
void makeMaxSparseTableMat(InputArray src, OutputArray dst, int rowStep, int colStep)
247211
{
248212
CV_Assert(rowStep * colStep == 0); // one of "rowStep" or "colStep" is required to be 0.
249213

0 commit comments

Comments
 (0)