OpenCV: Hit-or-Miss

| OpenCV 3.4.2 Open Source Computer Vision

Hit-or-Miss

Prev Tutorial: More Morphology Transformations
Next Tutorial: Extract horizontal and vertical lines by using morphological operations

## 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 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 (Eroding and Dilating) and (More Morphology Transformations).

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 \(B_1\) while not matching the shape of a second structuring element \(B_2\) at the same time. Mathematically, the operation applied to an image \(A\) can be expressed as follows:

\[ A\circledast B = (A\ominus B_1) \cap (A^c\ominus B_2) \]

Therefore, the hit-or-miss operation comprises three steps:

1. Erode image \(A\) with structuring element \(B_1\).
2. Erode the complement of image \(A\) ( \(A^c\)) with structuring element \(B_2\).
3. AND results from step 1 and step 2.

The structuring elements \(B_1\) and \(B_2\) can be combined into a single element \(B\). Let's see an example:

Structuring elements (kernels). Left: kernel to 'hit'. Middle: kernel to 'miss'. Right: final combined kernel

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
Output binary 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.

C++

You can also download it from here

#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_<int>(3, 3) <<
0, 1, 0,
1, -1, 1,
0, 1, 0);
Mat output_image;
morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
const int rate = 50;
kernel = (kernel + 1) * 127;
kernel.convertTo(kernel, CV_8U);
resize(kernel, kernel, Size(), rate, rate, INTER_NEAREST);
imshow("kernel", kernel);
moveWindow("kernel", 0, 0);
resize(input_image, input_image, Size(), rate, rate, INTER_NEAREST);
imshow("Original", input_image);
moveWindow("Original", 0, 200);
resize(output_image, output_image, Size(), rate, rate, INTER_NEAREST);
imshow("Hit or Miss", output_image);
moveWindow("Hit or Miss", 500, 200);
waitKey(0);
return 0;
}
Java

You can also download it from here

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
class HitMissRun{
public void run() {
Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
int row = 0, col = 0;
input_image.put(row ,col,
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 = new Mat( 3, 3, CvType.CV_16S );
kernel.put(row ,col,
0, 1, 0,
1, -1, 1,
0, 1, 0 );
Mat output_image = new Mat();
Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);
int rate = 50;
Core.add(kernel, new Scalar(1), kernel);
Core.multiply(kernel, new Scalar(127), kernel);
kernel.convertTo(kernel, CvType.CV_8U);
Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);
HighGui.imshow("kernel", kernel);
HighGui.moveWindow("kernel", 0, 0);
Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
HighGui.imshow("Original", input_image);
HighGui.moveWindow("Original", 0, 200);
Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
HighGui.imshow("Hit or Miss", output_image);
HighGui.moveWindow("Hit or Miss", 500, 200);
HighGui.waitKey(0);
System.exit(0);
}
}
public class HitMiss
{
public static void main(String[] args) {
// load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new HitMissRun().run();
}
}
Python

You can also download it from here

import cv2 as cv
import numpy as np
input_image = np.array((
[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]), dtype="uint8")
kernel = np.array((
[0, 1, 0],
[1, -1, 1],
[0, 1, 0]), dtype="int")
output_image = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel)
rate = 50
kernel = (kernel + 1) * 127
kernel = np.uint8(kernel)
kernel = cv.resize(kernel, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("kernel", kernel)
cv.moveWindow("kernel", 0, 0)
input_image = cv.resize(input_image, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("Original", input_image)
cv.moveWindow("Original", 0, 200)
output_image = cv.resize(output_image, None , fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("Hit or Miss", output_image)
cv.moveWindow("Hit or Miss", 500, 200)
cv.waitKey(0)
cv.destroyAllWindows()

As you can see, it is as simple as using the function morphologyEx() with the operation type 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
Kernel and output result for finding left end points

Now try your own patterns!