Skip to content

Commit 953330b

Browse files
committed
カーネル分解リファクタ、不要になった公開メソッドを隠蔽、ユニットテスト修正
1 parent aee9a47 commit 953330b

File tree

3 files changed

+109
-188
lines changed

3 files changed

+109
-188
lines changed

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

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ namespace stMorph {
1414
//! @addtogroup imgproc_filter
1515
//! @{
1616

17+
typedef struct _kernelDecompInfo
18+
{
19+
int rows;
20+
int cols;
21+
std::vector<std::vector<std::vector<Point>>> stRects;
22+
Mat plan;
23+
Point anchor;
24+
int iterations;
25+
} kernelDecompInfo;
26+
27+
CV_EXPORTS_W kernelDecompInfo decompKernel(InputArray kernel,
28+
Point anchor = Point(-1, -1), int iterations = 1);
29+
30+
CV_EXPORTS_W void erode( InputArray src, OutputArray dst, kernelDecompInfo kdi,
31+
BorderTypes borderType = BORDER_CONSTANT,
32+
const Scalar& borderValue = morphologyDefaultBorderValue() );
33+
34+
CV_EXPORTS_W void dilate( InputArray src, OutputArray dst, kernelDecompInfo kdi,
35+
BorderTypes borderType = BORDER_CONSTANT,
36+
const Scalar& borderValue = morphologyDefaultBorderValue() );
37+
38+
CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst, int op, kernelDecompInfo kdi,
39+
BorderTypes borderType = BORDER_CONSTANT,
40+
const Scalar& borderValue = morphologyDefaultBorderValue() );
41+
1742
/**
1843
* @brief Faster implementation of cv::erode with sparse table concept.
1944
*
@@ -81,42 +106,7 @@ CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst,
81106
Point anchor = Point(-1,-1), int iterations = 1,
82107
BorderTypes borderType = BORDER_CONSTANT,
83108
const Scalar& borderValue = morphologyDefaultBorderValue() );
84-
85-
typedef struct _kernelDecompInfo
86-
{
87-
int rows;
88-
int cols;
89-
std::vector<std::vector<std::vector<Point>>> stRects;
90-
Mat plan;
91-
Point anchor;
92-
int iterations;
93-
} kernelDecompInfo;
94-
95-
CV_EXPORTS_W kernelDecompInfo getKernelDecompInfo(InputArray kernel,
96-
Point anchor = Point(-1, -1), int iterations = 1);
97-
CV_EXPORTS_W void erode( InputArray src, OutputArray dst,
98-
kernelDecompInfo kdi,
99-
BorderTypes borderType = BORDER_CONSTANT,
100-
const Scalar& borderValue = morphologyDefaultBorderValue() );
101-
CV_EXPORTS_W void dilate( InputArray src, OutputArray dst,
102-
kernelDecompInfo kdi,
103-
BorderTypes borderType = BORDER_CONSTANT,
104-
const Scalar& borderValue = morphologyDefaultBorderValue() );
105-
CV_EXPORTS_W void morphologyEx( InputArray src, OutputArray dst,
106-
int op, kernelDecompInfo kdi,
107-
BorderTypes borderType = BORDER_CONSTANT,
108-
const Scalar& borderValue = morphologyDefaultBorderValue() );
109-
110109
//! @}
111110

112-
CV_EXPORTS_W std::vector<std::vector<std::vector<Point>>> genPow2RectsToCoverKernel(
113-
const Mat& kernel, int rowLim, int colLim);
114-
CV_EXPORTS_W Mat sparseTableFillPlanning(
115-
std::vector<std::vector<std::vector<Point>>> stNodeMap, int rowLim, int colLim);
116-
117-
CV_EXPORTS_W int log2(int n);
118-
CV_EXPORTS_W int longestRowRunLength(const Mat& kernel);
119-
CV_EXPORTS_W int longestColRunLength(const Mat& kernel);
120-
121111
}} // cv::stMorph::
122112
#endif

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,37 @@ Mat sparseTableFillPlanning(
220220
return path;
221221
}
222222

223+
kernelDecompInfo decompKernel(InputArray kernel, Point anchor, int iterations)
224+
{
225+
Mat _kernel = kernel.getMat();
226+
// Fix kernel in case of it is empty.
227+
if (_kernel.empty())
228+
{
229+
_kernel = getStructuringElement(MORPH_RECT, Size(1 + iterations * 2, 1 + iterations * 2));
230+
anchor = Point(iterations, iterations);
231+
iterations = 1;
232+
}
233+
if (countNonZero(_kernel) == 0)
234+
{
235+
_kernel.at<uchar>(0, 0) = 1;
236+
}
237+
// Fix anchor to the center of the kernel.
238+
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
239+
240+
241+
int rowDepthLim = log2(longestRowRunLength(_kernel)) + 1;
242+
int colDepthLim = log2(longestColRunLength(_kernel)) + 1;
243+
std::vector<std::vector<std::vector<Point>>> pow2Rects
244+
= genPow2RectsToCoverKernel(_kernel, rowDepthLim, colDepthLim);
245+
246+
Mat stPlan
247+
= sparseTableFillPlanning(pow2Rects, rowDepthLim, colDepthLim);
248+
249+
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
250+
251+
return { _kernel.rows, _kernel.cols, pow2Rects, stPlan, anchor, iterations };
252+
}
253+
223254
void morphDfs(int minmax, Mat& st, Mat& dst,
224255
std::vector<std::vector<std::vector<Point>>> row2Rects, const Mat& stPlan,
225256
int rowDepth, int colDepth)
@@ -318,37 +349,6 @@ void morphOp(Op minmax, InputArray _src, OutputArray _dst, kernelDecompInfo kdi,
318349
}
319350
}
320351

321-
kernelDecompInfo getKernelDecompInfo(InputArray kernel, Point anchor, int iterations)
322-
{
323-
Mat _kernel = kernel.getMat();
324-
// Fix kernel in case of it is empty.
325-
if (_kernel.empty())
326-
{
327-
_kernel = getStructuringElement(MORPH_RECT, Size(1 + iterations * 2, 1 + iterations * 2));
328-
anchor = Point(iterations, iterations);
329-
iterations = 1;
330-
}
331-
if (countNonZero(_kernel) == 0)
332-
{
333-
_kernel.at<uchar>(0, 0) = 1;
334-
}
335-
// Fix anchor to the center of the kernel.
336-
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
337-
338-
339-
int rowDepthLim = log2(longestRowRunLength(_kernel)) + 1;
340-
int colDepthLim = log2(longestColRunLength(_kernel)) + 1;
341-
std::vector<std::vector<std::vector<Point>>> pow2Rects
342-
= genPow2RectsToCoverKernel(_kernel, rowDepthLim, colDepthLim);
343-
344-
Mat stPlan
345-
= sparseTableFillPlanning(pow2Rects, rowDepthLim, colDepthLim);
346-
347-
anchor = stMorph::normalizeAnchor(anchor, _kernel.size());
348-
349-
return { _kernel.rows, _kernel.cols, pow2Rects, stPlan, anchor, iterations };
350-
}
351-
352352
void erode(InputArray src, OutputArray dst, kernelDecompInfo kdi,
353353
BorderTypes borderType, const Scalar& borderVal)
354354
{
@@ -408,26 +408,25 @@ void morphologyEx(InputArray src, OutputArray dst, int op, kernelDecompInfo kdi,
408408
_dst = temp - _src;
409409
break;
410410
case MORPH_HITMISS:
411-
CV_Error(cv::Error::StsBadArg, "stMorph doesn't support HITMISS operation");
411+
CV_Error(cv::Error::StsBadArg, "StMorph doesn't support HIT-MISS operation.");
412412
default:
413-
CV_Error(cv::Error::StsBadArg, "unknown morphological operation");
413+
CV_Error(cv::Error::StsBadArg, "Unknown morphological operation.");
414414
}
415415
}
416416

417-
//------------------------------------------
418417
void erode(InputArray src, OutputArray dst, InputArray kernel,
419418
Point anchor, int iterations,
420419
BorderTypes borderType, const Scalar& borderVal)
421420
{
422-
kernelDecompInfo kdi = getKernelDecompInfo(kernel, anchor, iterations);
421+
kernelDecompInfo kdi = decompKernel(kernel, anchor, iterations);
423422
morphOp(Op::Min, src, dst, kdi, borderType, borderVal);
424423
}
425424

426425
void dilate(InputArray src, OutputArray dst, InputArray kernel,
427426
Point anchor, int iterations,
428427
BorderTypes borderType, const Scalar& borderVal)
429428
{
430-
kernelDecompInfo kdi = getKernelDecompInfo(kernel, anchor, iterations);
429+
kernelDecompInfo kdi = decompKernel(kernel, anchor, iterations);
431430
morphOp(Op::Max, src, dst, kdi, borderType, borderVal);
432431
}
433432

@@ -442,7 +441,7 @@ void morphologyEx(InputArray src, OutputArray dst, int op,
442441
_kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1, 1));
443442
}
444443

445-
kernelDecompInfo kdi = getKernelDecompInfo(_kernel, anchor, iterations);
444+
kernelDecompInfo kdi = decompKernel(_kernel, anchor, iterations);
446445
morphologyEx(src, dst, op, kdi, borderType, borderVal);
447446
}
448447

0 commit comments

Comments
 (0)