Skip to content

Commit 83d6db0

Browse files
committed
p2RectCovering 枝狩り
1 parent cceeabf commit 83d6db0

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
2626
// generate sparse table for the kernel
2727
std::vector<std::vector<Mat>> st(log2[kernel.rows] + 1, std::vector<Mat>(log2[kernel.cols] + 1));
2828
st[0][0] = kernel;
29-
for (int colDepth = 1; colDepth <= log2[kernel.cols]; colDepth++)
29+
int colDepthLim = log2[kernel.cols];
30+
int rowDepthLim = log2[kernel.rows];
31+
for (int colDepth = 1; colDepth <= colDepthLim; colDepth++)
3032
{
3133
int colStep = 1 << (colDepth - 1);
3234
int colLim = kernel.cols - (1 << colDepth) + 1;
@@ -38,12 +40,19 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
3840
st[0][colDepth].create(kernel.size(), kernel.type());
3941
Mat next = st[0][colDepth](rect1);
4042
cv::bitwise_and(src1, src2, next);
43+
if (countNonZero(next) == 0)
44+
{
45+
colDepthLim = colDepth;
46+
break;
47+
}
4148
}
42-
for (int rowDepth = 1; rowDepth <= log2[kernel.rows]; rowDepth++)
49+
for (int rowDepth = 1; rowDepth <= rowDepthLim; rowDepth++)
4350
{
51+
int nonZero = 0;
52+
4453
int rowStep = 1 << (rowDepth - 1);
4554
int rowLim = kernel.rows - (1 << rowDepth) + 1;
46-
for (int colDepth = 0; colDepth <= log2[kernel.cols]; colDepth++)
55+
for (int colDepth = 0; colDepth <= colDepthLim; colDepth++)
4756
{
4857
int colStep = 0;
4958
int colLim = kernel.cols - (1 << colDepth) + 1;
@@ -55,18 +64,28 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
5564
st[rowDepth][colDepth].create(kernel.size(), kernel.type());
5665
Mat next = st[rowDepth][colDepth](rect1);
5766
cv::bitwise_and(src1, src2, next);
67+
68+
if (colDepth == 0)
69+
{
70+
nonZero = countNonZero(next);
71+
}
72+
}
73+
if (nonZero == 0)
74+
{
75+
rowDepthLim = rowDepth;
76+
break;
5877
}
5978
}
6079

6180
// find pow2 rectangles
6281
std::vector<Rect> p2Rects;
63-
for (int rowDepth = 0; rowDepth <= log2[kernel.rows]; rowDepth++)
82+
for (int rowDepth = 0; rowDepth <= rowDepthLim; rowDepth++)
6483
{
6584
int rowOfst = 1 << rowDepth;
6685
int rowSkip = rowOfst - 1;
6786
int rowLim = kernel.rows - rowSkip;
6887
int x = rowOfst * kernel.cols;
69-
for (int colDepth = 0; colDepth <= log2[kernel.cols]; colDepth++)
88+
for (int colDepth = 0; colDepth <= colDepthLim; colDepth++)
7089
{
7190
int colOfst = 1 << colDepth;
7291
int colSkip = colOfst - 1;

0 commit comments

Comments
 (0)