Skip to content

Commit 7b40378

Browse files
committed
カーネルの前処理工程を bitwise_and 実装に置き換え
1 parent a6b7f25 commit 7b40378

File tree

1 file changed

+22
-44
lines changed

1 file changed

+22
-44
lines changed

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,33 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
2828
st[0][0] = kernel;
2929
for (int colDepth = 1; colDepth <= log2[kernel.cols]; colDepth++)
3030
{
31-
int rowStep = 0;
32-
int rowSkip = 0;
33-
int rowLim = kernel.rows - rowSkip;
34-
3531
int colStep = 1 << (colDepth - 1);
36-
int colSkip = (1 << colDepth) - 1;
37-
int colLim = kernel.cols - colSkip;
32+
int colLim = kernel.cols - (1 << colDepth) + 1;
3833

34+
Rect rect1(0, 0, colLim, kernel.rows);
35+
Rect rect2(colStep, 0, colLim, kernel.rows);
36+
Mat src1 = st[0][colDepth - 1](rect1);
37+
Mat src2 = st[0][colDepth - 1](rect2);
3938
st[0][colDepth].create(kernel.size(), kernel.type());
40-
uchar* ptr1 = st[0][colDepth - 1].ptr();
41-
uchar* ptr2 = st[0][colDepth - 1].ptr(rowStep, colStep);
42-
uchar* dst = st[0][colDepth].ptr();
43-
for (int row = 0; row < rowLim; row++)
44-
{
45-
for (int col = 0; col < colLim; col++)
46-
{
47-
*dst++ = *ptr1++ & *ptr2++;
48-
}
49-
ptr1 += colSkip;
50-
ptr2 += colSkip;
51-
dst += colSkip;
52-
}
39+
Mat next = st[0][colDepth](rect1);
40+
cv::bitwise_and(src1, src2, next);
5341
}
5442
for (int rowDepth = 1; rowDepth <= log2[kernel.rows]; rowDepth++)
5543
{
5644
int rowStep = 1 << (rowDepth - 1);
57-
int rowSkip = (1 << rowDepth) - 1;
58-
int rowLim = kernel.rows - rowSkip;
45+
int rowLim = kernel.rows - (1 << rowDepth) + 1;
5946
for (int colDepth = 0; colDepth <= log2[kernel.cols]; colDepth++)
6047
{
6148
int colStep = 0;
62-
int colSkip = (1 << colDepth) - 1;
63-
int colLim = kernel.cols - colSkip;
49+
int colLim = kernel.cols - (1 << colDepth) + 1;
6450

51+
Rect rect1(0, 0, colLim, rowLim);
52+
Rect rect2(colStep, rowStep, colLim, rowLim);
53+
Mat src1 = st[rowDepth - 1][colDepth](rect1);
54+
Mat src2 = st[rowDepth - 1][colDepth](rect2);
6555
st[rowDepth][colDepth].create(kernel.size(), kernel.type());
66-
uchar* ptr1 = st[rowDepth - 1][colDepth].ptr();
67-
uchar* ptr2 = st[rowDepth - 1][colDepth].ptr(rowStep, colStep);
68-
uchar* dst = st[rowDepth][colDepth].ptr();
69-
for (int row = 0; row < rowLim; row++)
70-
{
71-
for (int col = 0; col < colLim; col++)
72-
{
73-
*dst++ = *ptr1++ & *ptr2++;
74-
}
75-
ptr1 += colSkip;
76-
ptr2 += colSkip;
77-
dst += colSkip;
78-
}
56+
Mat next = st[rowDepth][colDepth](rect1);
57+
cv::bitwise_and(src1, src2, next);
7958
}
8059
}
8160

@@ -107,7 +86,7 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
10786
if (row > 0 && ptr[-kernel.cols]
10887
&& row < rowLim && ptr[kernel.cols] == 1) continue;
10988

110-
// ignore one of neighbor block is white; will be alive in deeper table
89+
// ignore if neighboring block is white; will be alive in deeper table
11190
if (col + colOfst <= colLim && ptr[colOfst] == 1) continue;
11291
if (col - colOfst >= 0 && ptr[-colOfst] == 1) continue;
11392
if (row + rowOfst <= rowLim && ptr[x] == 1) continue;
@@ -119,7 +98,6 @@ std::vector<Rect> genPow2RectsToCoverKernel(InputArray _kernel)
11998
}
12099
}
121100
}
122-
123101
return p2Rects;
124102
}
125103

@@ -354,18 +332,18 @@ void morphologyEx(InputArray src, OutputArray dst, int op,
354332
switch (op)
355333
{
356334
case MORPH_ERODE:
357-
stMorph::erode(_src, _dst, _kernel, anchor, iterations, borderType, borderVal);
335+
stMorph::erode(src, dst, kernel, anchor, iterations, borderType, borderVal);
358336
break;
359337
case MORPH_DILATE:
360-
stMorph::dilate(_src, _dst, _kernel, anchor, iterations, borderType, borderVal);
338+
stMorph::dilate(src, dst, kernel, anchor, iterations, borderType, borderVal);
361339
break;
362340
case MORPH_OPEN:
363-
stMorph::erode(_src, _dst, _kernel, anchor, iterations, borderType, borderVal);
364-
stMorph::dilate(_dst, _dst, _kernel, anchor, iterations, borderType, borderVal);
341+
stMorph::erode(src, dst, kernel, anchor, iterations, borderType, borderVal);
342+
stMorph::dilate(dst, dst, kernel, anchor, iterations, borderType, borderVal);
365343
break;
366344
case MORPH_CLOSE:
367-
stMorph::dilate(_src, _dst, _kernel, anchor, iterations, borderType, borderVal);
368-
stMorph::erode(_dst, _dst, _kernel, anchor, iterations, borderType, borderVal);
345+
stMorph::dilate(src, dst, kernel, anchor, iterations, borderType, borderVal);
346+
stMorph::erode(dst, dst, kernel, anchor, iterations, borderType, borderVal);
369347
break;
370348
case MORPH_GRADIENT:
371349
stMorph::erode(_src, temp, _kernel, anchor, iterations, borderType, borderVal);

0 commit comments

Comments
 (0)