@@ -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+
223254void 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-
352352void 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- // ------------------------------------------
418417void 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
426425void 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