@@ -26,7 +26,9 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
26
26
// generate sparse table for the kernel
27
27
std::vector<std::vector<Mat>> st (log2[kernel.rows ] + 1 , std::vector<Mat>(log2[kernel.cols ] + 1 ));
28
28
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++)
30
32
{
31
33
int colStep = 1 << (colDepth - 1 );
32
34
int colLim = kernel.cols - (1 << colDepth) + 1 ;
@@ -38,12 +40,19 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
38
40
st[0 ][colDepth].create (kernel.size (), kernel.type ());
39
41
Mat next = st[0 ][colDepth](rect1);
40
42
cv::bitwise_and (src1, src2, next);
43
+ if (countNonZero (next) == 0 )
44
+ {
45
+ colDepthLim = colDepth;
46
+ break ;
47
+ }
41
48
}
42
- for (int rowDepth = 1 ; rowDepth <= log2[kernel. rows ] ; rowDepth++)
49
+ for (int rowDepth = 1 ; rowDepth <= rowDepthLim ; rowDepth++)
43
50
{
51
+ int nonZero = 0 ;
52
+
44
53
int rowStep = 1 << (rowDepth - 1 );
45
54
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++)
47
56
{
48
57
int colStep = 0 ;
49
58
int colLim = kernel.cols - (1 << colDepth) + 1 ;
@@ -55,18 +64,28 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
55
64
st[rowDepth][colDepth].create (kernel.size (), kernel.type ());
56
65
Mat next = st[rowDepth][colDepth](rect1);
57
66
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 ;
58
77
}
59
78
}
60
79
61
80
// find pow2 rectangles
62
81
std::vector<Rect> p2Rects;
63
- for (int rowDepth = 0 ; rowDepth <= log2[kernel. rows ] ; rowDepth++)
82
+ for (int rowDepth = 0 ; rowDepth <= rowDepthLim ; rowDepth++)
64
83
{
65
84
int rowOfst = 1 << rowDepth;
66
85
int rowSkip = rowOfst - 1 ;
67
86
int rowLim = kernel.rows - rowSkip;
68
87
int x = rowOfst * kernel.cols ;
69
- for (int colDepth = 0 ; colDepth <= log2[kernel. cols ] ; colDepth++)
88
+ for (int colDepth = 0 ; colDepth <= colDepthLim ; colDepth++)
70
89
{
71
90
int colOfst = 1 << colDepth;
72
91
int colSkip = colOfst - 1 ;
0 commit comments