HitMiss tutorial
authorLorena García <LorenaGdL@users.noreply.github.com>
Tue, 3 Jan 2017 17:34:04 +0000 (18:34 +0100)
committerLorena García <LorenaGdL@users.noreply.github.com>
Tue, 3 Jan 2017 17:34:04 +0000 (18:34 +0100)
doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown [new file with mode: 0644]
doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png [new file with mode: 0644]
doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png [new file with mode: 0644]
doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png [new file with mode: 0644]
doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png [new file with mode: 0644]
doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png [new file with mode: 0644]
doc/tutorials/imgproc/table_of_content_imgproc.markdown
modules/imgproc/include/opencv2/imgproc.hpp
samples/cpp/tutorial_code/ImgProc/HitMiss.cpp [new file with mode: 0644]

diff --git a/doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown b/doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown
new file mode 100644 (file)
index 0000000..085e7c2
--- /dev/null
@@ -0,0 +1,60 @@
+Hit-or-Miss {#tutorial_hitOrMiss}
+=================================
+
+Goal
+----
+
+In this tutorial you will learn how to find a given configuration or pattern in a binary image by using the Hit-or-Miss transform (also known as Hit-and-Miss transform).
+This transform is also the basis of more advanced morphological operations such as thinning or pruning.
+
+We will use the OpenCV function @ref cv::morphologyEx.
+
+
+
+Hit-or-Miss theory
+-------------------
+
+Morphological operators process images based on their shape. These operators apply one or more *structuring elements* to an input image to obtain the output image.
+The two basic morphological operations are the *erosion* and the *dilation*. The combination of these two operations generate advanced morphological transformations such as *opening*, *closing*, or *top-hat* transform.
+To know more about these and other basic morphological operations refer to previous tutorials @ref tutorial_erosion_dilatation "here" and @ref tutorial_opening_closing_hats "here".
+
+The Hit-or-Miss transformation is useful to find patterns in binary images. In particular, it finds those pixels whose neighbourhood matches the shape of a first structuring element \f$B_1\f$
+while not matching the shape of a second structuring element \f$B_2\f$ at the same time. Mathematically, the operation applied to an image \f$A\f$ can be expressed as follows:
+\f[
+    A\circledast B = (A\ominus B_1) \cap (A^c\ominus B_2)
+\f]
+
+Therefore, the hit-or-miss operation comprises three steps:
+    1. Erode image \f$A\f$ with structuring element \f$B_1\f$.
+    2. Erode the complement of image \f$A\f$ (\f$A^c\f$) with structuring element \f$B_2\f$.
+    3. AND results from step 1 and step 2.
+
+The structuring elements \f$B_1\f$ and \f$B_2\f$ can be combined into a single element \f$B\f$. Let's see an example:
+![Structuring elements (kernels). Left: kernel to 'hit'. Middle: kernel to 'miss'. Right: final combined kernel](images/hitmiss_kernels.png)
+
+In this case, we are looking for a pattern in which the central pixel belongs to the background while the north, south, east, and west pixels belong to the foreground. The rest of pixels in the neighbourhood can be of any kind, we don't care about them. Now, let's apply this kernel to an input image:
+
+![Input binary image](images/hitmiss_input.png)
+![Output binary image](images/hitmiss_output.png)
+
+You can see that the pattern is found in just one location within the image.
+
+
+Code
+----
+
+The code corresponding to the previous example is shown below. You can also download it from
+[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp)
+@include samples/cpp/tutorial_code/ImgProc/HitMiss.cpp
+
+As you can see, it is as simple as using the function @ref cv::morphologyEx with the operation type @ref cv::MORPH_HITMISS and the chosen kernel.
+
+Other examples
+--------------
+
+Here you can find the output results of applying different kernels to the same input image used before:
+
+![Kernel and output result for finding top-right corners](images/hitmiss_example2.png)
+![Kernel and output result for finding left end points](images/hitmiss_example3.png)
+
+Now try your own patterns!
diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png
new file mode 100644 (file)
index 0000000..c4e0efb
Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png differ
diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png
new file mode 100644 (file)
index 0000000..dc97275
Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png differ
diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png
new file mode 100644 (file)
index 0000000..fec726f
Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png differ
diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png
new file mode 100644 (file)
index 0000000..cdf5e73
Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png differ
diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png
new file mode 100644 (file)
index 0000000..b7b8163
Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png differ
index 486c644..447f197 100644 (file)
@@ -27,6 +27,14 @@ In this section you will learn about the image processing (manipulation) functio
 
     Here we investigate different morphology operators
 
+-      @subpage tutorial_hitOrMiss
+
+    *Compatibility:* \> OpenCV 2.4
+
+    *Author:* Lorena García
+
+    Learn how to find patterns in binary images using the Hit-or-Miss operation
+
 -   @subpage tutorial_moprh_lines_detection
 
     *Compatibility:* \> OpenCV 2.0
index 007a238..95e5a15 100644 (file)
@@ -245,8 +245,8 @@ enum MorphTypes{
                         //!< \f[\texttt{dst} = \mathrm{tophat} ( \texttt{src} , \texttt{element} )= \texttt{src} - \mathrm{open} ( \texttt{src} , \texttt{element} )\f]
     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](https://web.archive.org/web/20160316070407/http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/)
+    MORPH_HITMISS  = 7  //!< "hit or miss"
+                        //!<   .- Only supported for CV_8UC1 binary images. A tutorial can be found in the documentation
 };
 
 //! shape of the structuring element
diff --git a/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp b/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp
new file mode 100644 (file)
index 0000000..0463aab
--- /dev/null
@@ -0,0 +1,32 @@
+#include <opencv2/core.hpp>
+#include <opencv2/imgproc.hpp>
+#include <opencv2/highgui.hpp>
+
+using namespace cv;
+
+int main(){
+    Mat input_image = (Mat_<uchar>(8, 8) <<
+        0, 0, 0, 0, 0, 0, 0, 0,
+        0, 255, 255, 255, 0, 0, 0, 255,
+        0, 255, 255, 255, 0, 0, 0, 0,
+        0, 255, 255, 255, 0, 255, 0, 0,
+        0, 0, 255, 0, 0, 0, 0, 0,
+        0, 0, 255, 0, 0, 255, 255, 0,
+        0, 255, 0, 255, 0, 0, 255, 0,
+        0, 255, 255, 255, 0, 0, 0, 0);
+
+    Mat kernel = (Mat_<uchar>(3, 3) <<
+        0, 1, 0,
+        1, -1, 1,
+        0, 1, 0);
+
+    Mat output_image;
+    morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
+
+    namedWindow("Original", CV_WINDOW_NORMAL);
+    imshow("Original", input_image);
+    namedWindow("Hit or Miss", CV_WINDOW_NORMAL);
+    imshow("Hit or Miss", output_image);
+    waitKey(0);
+    return 0;
+}
\ No newline at end of file