@@ -14,41 +14,7 @@ namespace cv {
14
14
namespace ximgproc {
15
15
namespace stMorph {
16
16
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)
52
18
{
53
19
CV_Assert (_kernel.type () == CV_8UC1);
54
20
@@ -119,11 +85,14 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
119
85
std::vector<Rect> p2Rects;
120
86
for (int rowDepth = 0 ; rowDepth <= log2[kernel.rows ]; rowDepth++)
121
87
{
122
- int rowSkip = (1 << rowDepth) - 1 ;
88
+ int rowOfst = 1 << rowDepth;
89
+ int rowSkip = rowOfst - 1 ;
123
90
int rowLim = kernel.rows - rowSkip;
91
+ int x = rowOfst * kernel.cols ;
124
92
for (int colDepth = 0 ; colDepth <= log2[kernel.cols ]; colDepth++)
125
93
{
126
- int colSkip = (1 << colDepth) - 1 ;
94
+ int colOfst = 1 << colDepth;
95
+ int colSkip = colOfst - 1 ;
127
96
int colLim = kernel.cols - colSkip;
128
97
129
98
uchar* ptr = st[rowDepth][colDepth].ptr ();
@@ -139,10 +108,10 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
139
108
if (row > 0 && ptr[-kernel.cols ] && row < rowLim && ptr[kernel.cols ] == 1 ) continue ;
140
109
141
110
// 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 ;
146
115
147
116
p2Rects.emplace_back (col, row, colDepth, rowDepth);
148
117
}
@@ -154,6 +123,8 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
154
123
return p2Rects;
155
124
}
156
125
126
+ std::vector<StStep> planSparseTableConstruction (std::vector<std::vector<bool >> sparseMatMap)
127
+ {
157
128
/*
158
129
*
159
130
*
@@ -163,8 +134,6 @@ static std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
163
134
* https://link.springer.com/article/10.1007/BF01758762
164
135
*
165
136
*/
166
- static std::vector<StStep> planSparseTableConstruction (std::vector<std::vector<bool >> sparseMatMap)
167
- {
168
137
auto comparePos = [](Point lp, Point rp) {
169
138
int diffx = lp.x - rp.x ;
170
139
int diffy = lp.y - rp.y ;
@@ -174,15 +143,12 @@ static std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<b
174
143
return diffy < 0 ;
175
144
};
176
145
std::priority_queue<Point, std::vector<Point>, decltype (comparePos)> points{ comparePos };
146
+ sparseMatMap[0 ][0 ] = true ;
177
147
for (int r = 0 ; r < sparseMatMap.size (); r++)
178
- {
179
148
for (int c = 0 ; c < sparseMatMap[r].size (); c++)
180
- {
181
149
if (sparseMatMap[r][c]) points.push (Point (c, r));
182
- }
183
- }
184
150
185
- std::vector<StStep> ans ;
151
+ std::vector<StStep> plan ;
186
152
while (points.size () >= 2 )
187
153
{
188
154
Point p1 = points.top ();
@@ -197,19 +163,16 @@ static std::vector<StStep> planSparseTableConstruction(std::vector<std::vector<b
197
163
points.push (Point (newX, newY));
198
164
}
199
165
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);
204
170
}
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;
210
173
}
211
174
212
- static void makeMinSparseTableMat (InputArray src, OutputArray dst, int rowStep, int colStep)
175
+ void makeMinSparseTableMat (InputArray src, OutputArray dst, int rowStep, int colStep)
213
176
{
214
177
CV_Assert (rowStep * colStep == 0 ); // one of "rowStep" or "colStep" is required to be 0.
215
178
@@ -243,7 +206,8 @@ static void makeMinSparseTableMat(InputArray src, OutputArray dst, int rowStep,
243
206
dstPtr += borderSkipStep;
244
207
}
245
208
}
246
- static void makeMaxSparseTableMat (InputArray src, OutputArray dst, int rowStep, int colStep)
209
+
210
+ void makeMaxSparseTableMat (InputArray src, OutputArray dst, int rowStep, int colStep)
247
211
{
248
212
CV_Assert (rowStep * colStep == 0 ); // one of "rowStep" or "colStep" is required to be 0.
249
213
0 commit comments