Hit&Miss morphological operation
authorLorena García <LorenaGdL@users.noreply.github.com>
Wed, 14 Oct 2015 11:30:09 +0000 (13:30 +0200)
committerLorena García <LorenaGdL@users.noreply.github.com>
Wed, 14 Oct 2015 12:01:53 +0000 (14:01 +0200)
modules/imgproc/include/opencv2/imgproc.hpp
modules/imgproc/src/morph.cpp

index d8ded74..2eecb9a 100644 (file)
@@ -225,8 +225,10 @@ enum MorphTypes{
                         //!< \f[\texttt{dst} = \mathrm{morph\_grad} ( \texttt{src} , \texttt{element} )= \mathrm{dilate} ( \texttt{src} , \texttt{element} )- \mathrm{erode} ( \texttt{src} , \texttt{element} )\f]
     MORPH_TOPHAT   = 5, //!< "top hat"
                         //!< \f[\texttt{dst} = \mathrm{tophat} ( \texttt{src} , \texttt{element} )= \texttt{src} - \mathrm{open} ( \texttt{src} , \texttt{element} )\f]
-    MORPH_BLACKHAT = 6  //!< "black hat"
+    MORPH_BLACKHAT = 6, //!< "black hat"
                         //!< \f[\texttt{dst} = \mathrm{blackhat} ( \texttt{src} , \texttt{element} )= \mathrm{close} ( \texttt{src} , \texttt{element} )- \texttt{src}\f]
+    MORPH_HITMISS  = 7  //!< "hit and miss"
+                        //!<   .- Only supported for CV_8UC1 binary images. Tutorial can be found in [this page](http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/)
 };
 
 //! shape of the structuring element
index 1ec1d25..0b2b613 100644 (file)
@@ -1870,6 +1870,8 @@ void cv::morphologyEx( InputArray _src, OutputArray _dst, int op,
     _dst.create(src.size(), src.type());
     Mat dst = _dst.getMat();
 
+    Mat k1, k2, e1, e2;                //only for hit and miss op
+
     switch( op )
     {
     case MORPH_ERODE:
@@ -1905,6 +1907,22 @@ void cv::morphologyEx( InputArray _src, OutputArray _dst, int op,
         erode( temp, temp, kernel, anchor, iterations, borderType, borderValue );
         dst = temp - src;
         break;
+    case MORPH_HITMISS:
+        CV_Assert(src.type() == CV_8U && src.channels() == 1);
+        k1 = (kernel == 1) / 255;
+        k2 = (kernel == -1) / 255;
+        normalize(src, src, 0, 1, NORM_MINMAX);
+        if (countNonZero(k1) <= 0)
+            e1 = src;
+        else
+            erode(src, e1, k1, anchor, iterations, borderType, borderValue);
+        if (countNonZero(k2) <= 0)
+            e2 = src;
+        else
+            erode(1 - src, e2, k2, anchor, iterations, borderType, borderValue);
+        dst = e1 & e2;
+        normalize(dst, dst, 0, 255, NORM_MINMAX);
+        break;
     default:
         CV_Error( CV_StsBadArg, "unknown morphological operation" );
     }