Skip to content

Commit 18bc4db

Browse files
committed
Tutorial Hit-or-Miss
1 parent bc18fb4 commit 18bc4db

File tree

5 files changed

+132
-10
lines changed

5 files changed

+132
-10
lines changed

doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
Hit-or-Miss {#tutorial_hitOrMiss}
22
=================================
33

4+
@prev_tutorial{tutorial_opening_closing_hats}
5+
@next_tutorial{tutorial_morph_lines_detection}
6+
47
Goal
58
----
69

710
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).
811
This transform is also the basis of more advanced morphological operations such as thinning or pruning.
912

10-
We will use the OpenCV function @ref cv::morphologyEx.
11-
12-
13+
We will use the OpenCV function **morphologyEx()** .
1314

1415
Hit-or-Miss theory
1516
-------------------
1617

1718
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.
1819
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.
19-
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".
20+
To know more about these and other basic morphological operations refer to previous tutorials (@ref tutorial_erosion_dilatation "Eroding and Dilating") and (@ref tutorial_opening_closing_hats "More Morphology Transformations").
2021

2122
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$
2223
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:
@@ -43,11 +44,27 @@ You can see that the pattern is found in just one location within the image.
4344
Code
4445
----
4546

46-
The code corresponding to the previous example is shown below. You can also download it from
47-
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp)
48-
@include samples/cpp/tutorial_code/ImgProc/HitMiss.cpp
47+
The code corresponding to the previous example is shown below.
48+
49+
@add_toggle_cpp
50+
You can also download it from
51+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgProc/HitMiss/HitMiss.cpp)
52+
@include samples/cpp/tutorial_code/ImgProc/HitMiss/HitMiss.cpp
53+
@end_toggle
54+
55+
@add_toggle_java
56+
You can also download it from
57+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgProc/HitMiss/HitMiss.java)
58+
@include samples/java/tutorial_code/ImgProc/HitMiss/HitMiss.java
59+
@end_toggle
60+
61+
@add_toggle_python
62+
You can also download it from
63+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/imgProc/HitMiss/hit_miss.py)
64+
@include samples/python/tutorial_code/imgProc/HitMiss/hit_miss.py
65+
@end_toggle
4966

50-
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.
67+
As you can see, it is as simple as using the function **morphologyEx()** with the operation type **MORPH_HITMISS** and the chosen kernel.
5168

5269
Other examples
5370
--------------

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ In this section you will learn about the image processing (manipulation) functio
2929

3030
Here we investigate different morphology operators
3131

32-
- @subpage tutorial_hitOrMiss
32+
- @subpage tutorial_hitOrMiss
33+
34+
*Languages:* C++, Java, Python
3335

3436
*Compatibility:* \> OpenCV 2.4
3537

samples/cpp/tutorial_code/ImgProc/HitMiss.cpp renamed to samples/cpp/tutorial_code/ImgProc/HitMiss/HitMiss.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,22 @@ int main(){
2323
Mat output_image;
2424
morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
2525

26-
const int rate = 10;
26+
const int rate = 50;
2727
kernel = (kernel + 1) * 127;
2828
kernel.convertTo(kernel, CV_8U);
29+
2930
resize(kernel, kernel, Size(), rate, rate, INTER_NEAREST);
3031
imshow("kernel", kernel);
32+
moveWindow("kernel", 0, 0);
33+
3134
resize(input_image, input_image, Size(), rate, rate, INTER_NEAREST);
3235
imshow("Original", input_image);
36+
moveWindow("Original", 0, 200);
37+
3338
resize(output_image, output_image, Size(), rate, rate, INTER_NEAREST);
3439
imshow("Hit or Miss", output_image);
40+
moveWindow("Hit or Miss", 500, 200);
41+
3542
waitKey(0);
3643
return 0;
3744
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import org.opencv.core.*;
2+
import org.opencv.highgui.HighGui;
3+
import org.opencv.imgproc.Imgproc;
4+
5+
class HitMissRun{
6+
7+
public void run() {
8+
Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
9+
int row = 0, col = 0;
10+
input_image.put(row ,col,
11+
0, 0, 0, 0, 0, 0, 0, 0,
12+
0, 255, 255, 255, 0, 0, 0, 255,
13+
0, 255, 255, 255, 0, 0, 0, 0,
14+
0, 255, 255, 255, 0, 255, 0, 0,
15+
0, 0, 255, 0, 0, 0, 0, 0,
16+
0, 0, 255, 0, 0, 255, 255, 0,
17+
0, 255, 0, 255, 0, 0, 255, 0,
18+
0, 255, 255, 255, 0, 0, 0, 0);
19+
20+
Mat kernel = new Mat( 3, 3, CvType.CV_16S );
21+
kernel.put(row ,col,
22+
0, 1, 0,
23+
1, -1, 1,
24+
0, 1, 0 );
25+
26+
Mat output_image = new Mat();
27+
Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);
28+
29+
int rate = 50;
30+
Core.add(kernel, new Scalar(1), kernel);
31+
Core.multiply(kernel, new Scalar(127), kernel);
32+
kernel.convertTo(kernel, CvType.CV_8U);
33+
34+
Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);
35+
HighGui.imshow("kernel", kernel);
36+
HighGui.moveWindow("kernel", 0, 0);
37+
38+
Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
39+
HighGui.imshow("Original", input_image);
40+
HighGui.moveWindow("Original", 0, 200);
41+
42+
Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
43+
HighGui.imshow("Hit or Miss", output_image);
44+
HighGui.moveWindow("Hit or Miss", 500, 200);
45+
46+
HighGui.waitKey(0);
47+
System.exit(0);
48+
}
49+
}
50+
51+
public class HitMiss
52+
{
53+
public static void main(String[] args) {
54+
// load the native OpenCV library
55+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
56+
new HitMissRun().run();
57+
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
import numpy as np
3+
4+
input_image = np.array((
5+
[0, 0, 0, 0, 0, 0, 0, 0],
6+
[0, 255, 255, 255, 0, 0, 0, 255],
7+
[0, 255, 255, 255, 0, 0, 0, 0],
8+
[0, 255, 255, 255, 0, 255, 0, 0],
9+
[0, 0, 255, 0, 0, 0, 0, 0],
10+
[0, 0, 255, 0, 0, 255, 255, 0],
11+
[0,255, 0, 255, 0, 0, 255, 0],
12+
[0, 255, 255, 255, 0, 0, 0, 0]), dtype="uint8")
13+
14+
kernel = np.array((
15+
[0, 1, 0],
16+
[1, -1, 1],
17+
[0, 1, 0]), dtype="int")
18+
19+
output_image = cv2.morphologyEx(input_image, cv2.MORPH_HITMISS, kernel)
20+
21+
rate = 50
22+
kernel = (kernel + 1) * 127
23+
kernel = np.uint8(kernel)
24+
25+
kernel = cv2.resize(kernel, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
26+
cv2.imshow("kernel", kernel)
27+
cv2.moveWindow("kernel", 0, 0)
28+
29+
input_image = cv2.resize(input_image, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
30+
cv2.imshow("Original", input_image)
31+
cv2.moveWindow("Original", 0, 200)
32+
33+
output_image = cv2.resize(output_image, None , fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
34+
cv2.imshow("Hit or Miss", output_image)
35+
cv2.moveWindow("Hit or Miss", 500, 200)
36+
37+
cv2.waitKey(0)
38+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)