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:

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:


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
@@ -67,14 +67,14 @@ of them, you need to download and install them on your system.
...
@@ -67,14 +67,14 @@ of them, you need to download and install them on your system.
-[Numpy](http://numpy.scipy.org/) is a scientific computing package for Python. Required for the *Python interface*.
-[Numpy](http://numpy.scipy.org/) is a scientific computing package for Python. Required for the *Python interface*.
-[Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77) is used inside OpenCV for parallel code
-[Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77) is used inside OpenCV for parallel code
snippets. Using this will make sure that the OpenCV library will take advantage of all the cores
snippets. Using this will make sure that the OpenCV library will take advantage of all the cores
you have in your systems CPU.
you have in your system's CPU.
-[Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/) may be used to improve the performance
-[Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/) may be used to improve the performance
of color conversion, Haar training and DFT functions of the OpenCV library. Watch out, since
of color conversion, Haar training and DFT functions of the OpenCV library. Watch out, since
this isn't a free service.
this isn't a free service.
-[Intel IPP Asynchronous C/C++](http://software.intel.com/en-us/intel-ipp-preview) is currently focused delivering Intel Graphics
-[Intel IPP Asynchronous C/C++](http://software.intel.com/en-us/intel-ipp-preview) is currently focused delivering Intel Graphics
support for advanced image processing and computer vision functions.
support for advanced image processing and computer vision functions.
- OpenCV offers a somewhat fancier and more useful graphical user interface, than the default one
- OpenCV offers a somewhat fancier and more useful graphical user interface, than the default one
by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer look into the
by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer, look into the
documentations *highgui* module, under the *Qt New Functions* section. Version 4.6 or later of
documentations *highgui* module, under the *Qt New Functions* section. Version 4.6 or later of
the framework is required.
the framework is required.
-[Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) is a C++ template library for linear algebra.
-[Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) is a C++ template library for linear algebra.
...
@@ -83,7 +83,7 @@ of them, you need to download and install them on your system.
...
@@ -83,7 +83,7 @@ of them, you need to download and install them on your system.
more of our algorithms to work on the GPUs is a constant effort of the OpenCV team.
more of our algorithms to work on the GPUs is a constant effort of the OpenCV team.
-[OpenEXR](http://www.openexr.com/downloads.html) source files are required for the library to work with this high dynamic range (HDR)
-[OpenEXR](http://www.openexr.com/downloads.html) source files are required for the library to work with this high dynamic range (HDR)
image file format.
image file format.
- The OpenNI Framework contains a set of open source APIs that provide support for natural interaction with devices via methods such as voice command recognition, hand gestures and body
- The OpenNI Framework contains a set of open source APIs that provide support for natural interaction with devices via methods such as voice command recognition, hand gestures, and body
motion tracking. Prebuilt binaries can be found [here](http://structure.io/openni). The source code of [OpenNI](https://github.com/OpenNI/OpenNI) and [OpenNI2](https://github.com/OpenNI/OpenNI2) are also available on Github.
motion tracking. Prebuilt binaries can be found [here](http://structure.io/openni). The source code of [OpenNI](https://github.com/OpenNI/OpenNI) and [OpenNI2](https://github.com/OpenNI/OpenNI2) are also available on Github.
-[Miktex](http://miktex.org/2.9/setup) is the best [TEX](https://secure.wikimedia.org/wikipedia/en/wiki/TeX) implementation on
-[Miktex](http://miktex.org/2.9/setup) is the best [TEX](https://secure.wikimedia.org/wikipedia/en/wiki/TeX) implementation on
the Windows OS. It is required to build the *OpenCV documentation*.
the Windows OS. It is required to build the *OpenCV documentation*.
...
@@ -203,7 +203,7 @@ libraries). If you do not need the support for some of these you can just freely
...
@@ -203,7 +203,7 @@ libraries). If you do not need the support for some of these you can just freely
@code{.bash}
@code{.bash}
nmake
nmake
@endcode
@endcode
After this set the Qt enviroment variables using the following command on Windows 7:
After this set the Qt environment variables using the following command on Windows 7:
//!< .- 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/)
//!< .- Only supported for CV_8UC1 binary images. A tutorial can be found in the documentation