Skip to content

Commit 42936d3

Browse files
committed
imgproc: fix MORPH_HITMISS operation when kernel has no negative values
1 parent 7b8e630 commit 42936d3

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

modules/imgproc/src/morph.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,8 +2012,6 @@ void cv::morphologyEx( InputArray _src, OutputArray _dst, int op,
20122012
CV_IPP_RUN_FAST(ipp_morphologyEx(op, src, dst, kernel, anchor, iterations, borderType, borderValue));
20132013
#endif
20142014

2015-
Mat k1, k2, e1, e2; //only for hit and miss op
2016-
20172015
switch( op )
20182016
{
20192017
case MORPH_ERODE:
@@ -2051,21 +2049,29 @@ void cv::morphologyEx( InputArray _src, OutputArray _dst, int op,
20512049
break;
20522050
case MORPH_HITMISS:
20532051
CV_Assert(src.type() == CV_8UC1);
2054-
k1 = (kernel == 1);
2055-
k2 = (kernel == -1);
2056-
if (countNonZero(k1) <= 0)
2057-
e1 = src;
2058-
else
2059-
erode(src, e1, k1, anchor, iterations, borderType, borderValue);
2060-
if (countNonZero(k2) <= 0)
2061-
e2 = src;
2062-
else
2052+
if(countNonZero(kernel) <=0)
2053+
{
2054+
src.copyTo(dst);
2055+
break;
2056+
}
20632057
{
2058+
Mat k1, k2, e1, e2;
2059+
k1 = (kernel == 1);
2060+
k2 = (kernel == -1);
2061+
2062+
if (countNonZero(k1) <= 0)
2063+
e1 = src;
2064+
else
2065+
erode(src, e1, k1, anchor, iterations, borderType, borderValue);
2066+
20642067
Mat src_complement;
20652068
bitwise_not(src, src_complement);
2066-
erode(src_complement, e2, k2, anchor, iterations, borderType, borderValue);
2069+
if (countNonZero(k2) <= 0)
2070+
e2 = src_complement;
2071+
else
2072+
erode(src_complement, e2, k2, anchor, iterations, borderType, borderValue);
2073+
dst = e1 & e2;
20672074
}
2068-
dst = e1 & e2;
20692075
break;
20702076
default:
20712077
CV_Error( CV_StsBadArg, "unknown morphological operation" );

modules/imgproc/test/test_filter.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,3 +2065,36 @@ TEST(Imgproc_Sobel, borderTypes)
20652065
EXPECT_EQ(expected_dst.size(), dst.size());
20662066
EXPECT_DOUBLE_EQ(0.0, cvtest::norm(expected_dst, dst, NORM_INF));
20672067
}
2068+
2069+
TEST(Imgproc_MorphEx, hitmiss_regression_8957)
2070+
{
2071+
Mat_<uchar> src(3, 3);
2072+
src << 0, 255, 0,
2073+
0, 0, 0,
2074+
0, 255, 0;
2075+
2076+
Mat_<uchar> kernel = src / 255;
2077+
2078+
Mat dst;
2079+
morphologyEx(src, dst, MORPH_HITMISS, kernel);
2080+
2081+
Mat ref = Mat::zeros(3, 3, CV_8U);
2082+
ref.at<uchar>(1, 1) = 255;
2083+
2084+
ASSERT_DOUBLE_EQ(norm(dst, ref, NORM_INF), 0.);
2085+
}
2086+
2087+
TEST(Imgproc_MorphEx, hitmiss_zero_kernel)
2088+
{
2089+
Mat_<uchar> src(3, 3);
2090+
src << 0, 255, 0,
2091+
0, 0, 0,
2092+
0, 255, 0;
2093+
2094+
Mat_<uchar> kernel = Mat_<uchar>::zeros(3, 3);
2095+
2096+
Mat dst;
2097+
morphologyEx(src, dst, MORPH_HITMISS, kernel);
2098+
2099+
ASSERT_DOUBLE_EQ(norm(dst, src, NORM_INF), 0.);
2100+
}

0 commit comments

Comments
 (0)