Skip to content

Commit 6784b5f

Browse files
committed
morphologyEx の実装
1 parent 1acc393 commit 6784b5f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

modules/ximgproc/src/sparse_table_morphology.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,86 @@ void morphologyEx(InputArray _src, OutputArray _dst, int op,
550550
InputArray _kernel, Point anchor, int iterations,
551551
int borderType, const Scalar& borderValue)
552552
{
553+
CV_INSTRUMENT_REGION();
554+
555+
CV_Assert(!_src.empty());
556+
557+
Mat kernel = _kernel.getMat();
558+
if (kernel.empty())
559+
{
560+
kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1, 1));
561+
}
562+
563+
Mat src = _src.getMat(), temp;
564+
_dst.create(src.size(), src.type());
565+
Mat dst = _dst.getMat();
566+
567+
switch (op)
568+
{
569+
case MORPH_ERODE:
570+
stMorph::erode(src, dst, kernel, anchor, iterations, borderType, borderValue);
571+
break;
572+
case MORPH_DILATE:
573+
stMorph::dilate(src, dst, kernel, anchor, iterations, borderType, borderValue);
574+
break;
575+
case MORPH_OPEN:
576+
stMorph::erode(src, dst, kernel, anchor, iterations, borderType, borderValue);
577+
stMorph::dilate(dst, dst, kernel, anchor, iterations, borderType, borderValue);
578+
break;
579+
case MORPH_CLOSE:
580+
stMorph::dilate(src, dst, kernel, anchor, iterations, borderType, borderValue);
581+
stMorph::erode(dst, dst, kernel, anchor, iterations, borderType, borderValue);
582+
break;
583+
case MORPH_GRADIENT:
584+
stMorph::erode(src, temp, kernel, anchor, iterations, borderType, borderValue);
585+
stMorph::dilate(src, dst, kernel, anchor, iterations, borderType, borderValue);
586+
dst -= temp;
587+
break;
588+
case MORPH_TOPHAT:
589+
if (src.data != dst.data)
590+
temp = dst;
591+
stMorph::erode(src, temp, kernel, anchor, iterations, borderType, borderValue);
592+
stMorph::dilate(temp, temp, kernel, anchor, iterations, borderType, borderValue);
593+
dst = src - temp;
594+
break;
595+
case MORPH_BLACKHAT:
596+
if (src.data != dst.data)
597+
temp = dst;
598+
stMorph::dilate(src, temp, kernel, anchor, iterations, borderType, borderValue);
599+
stMorph::erode(temp, temp, kernel, anchor, iterations, borderType, borderValue);
600+
dst = temp - src;
601+
break;
602+
case MORPH_HITMISS:
603+
CV_Assert(src.type() == CV_8UC1);
604+
if (countNonZero(kernel) <= 0)
605+
{
606+
src.copyTo(dst);
607+
break;
608+
}
609+
{
610+
Mat k1, k2, e1, e2;
611+
k1 = (kernel == 1);
612+
k2 = (kernel == -1);
613+
614+
if (countNonZero(k1) <= 0)
615+
e1 = Mat(src.size(), src.type(), Scalar(255));
616+
else
617+
stMorph::erode(src, e1, k1, anchor, iterations, borderType, borderValue);
618+
619+
if (countNonZero(k2) <= 0)
620+
e2 = Mat(src.size(), src.type(), Scalar(255));
621+
else
622+
{
623+
Mat src_complement;
624+
bitwise_not(src, src_complement);
625+
stMorph::erode(src_complement, e2, k2, anchor, iterations, borderType, borderValue);
626+
}
627+
dst = e1 & e2;
628+
}
629+
break;
630+
default:
631+
CV_Error(cv::Error::StsBadArg, "unknown morphological operation");
632+
}
553633
}
554634

555635
}} // cv::st::

modules/ximgproc/test/test_sparse_table_morphology.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ TEST(ximgproc_StMorph_ex, regression_close) { ex_rgr(im(CV_8UC3), MORPH_CLOSE, k
334334
TEST(ximgproc_StMorph_ex, regression_gradient) { ex_rgr(im(CV_8UC3), MORPH_GRADIENT, kn5()); }
335335
TEST(ximgproc_StMorph_ex, regression_tophat) { ex_rgr(im(CV_8UC3), MORPH_TOPHAT, kn5()); }
336336
TEST(ximgproc_StMorph_ex, regression_blackhat) { ex_rgr(im(CV_8UC3), MORPH_BLACKHAT, kn5()); }
337-
TEST(ximgproc_StMorph_ex, regression_hitmiss) { ex_rgr(im(CV_8UC3), MORPH_HITMISS, kn5()); }
337+
TEST(ximgproc_StMorph_ex, regression_hitmiss) { ex_rgr(im(CV_8UC1), MORPH_HITMISS, kn5()); }
338338

339339
#pragma endregion
340340

0 commit comments

Comments
 (0)