Matlab – Miss or Hit Transform

Miss or Hit Manual 3

This post is about miss or hit transform in image processing. Before you will be able to understand miss or hit transform, there is a requirement which you need to understand first which is erosion. If you have no idea what erosion is or you not understand erosion clearly, please refer to my previous post on erosion. Hit or miss transform is one of the powerful method for finding shapes and their locations in an image. Hit or miss can be defined entirely in term of erosion only. Hit or miss transform is very useful for detecting specific shapes that are intended to extract such as squares, triangles, ridges, corners, junctions and etc. In this post,i will provide you with Matlab source code for miss or hit transform as an example to you. Furthermore, i will include the example with teoretical part how to do miss or hit transform using manual way.

Firstly, we will look on manual way to do miss or hit transform. Look carefully on picture below.

Miss or Hit Manual 1

In the image above, A is the original image while SE1 is structuring element 1. The first step is to erode A with SE1.

Miss or Hit Manual 2

Now image C is a complement of image A. SE2 is structuring element 2. Now we need to erode A complement (C) with structuring element 2 (SE2) which resulting in image D. Take note that structuring element 2 (SE2) is NOT a must to be equal with structuring element 1 (SE1). It’s depending on the question.

Miss or Hit Manual 3

Finally, we will find intersection between two results of erosion earlier. If there is any intesection, than the intersection mean that it’s a hit. Otherwise, it will be a miss. In this example, it will be all miss because as you can see not even one point is intersecting.

Below is the example Matlab source code for miss or hit transform


%% Section 1
a=[1 1 1 1 1 1 1 1 1 1;…
1 1 1 1 1 1 1 1 1 1;…
0 0 0 0 0 0 1 1 1 1;…
0 0 0 0 0 1 0 1 1 1;…
0 0 0 0 0 0 0 0 0 0;…
0 0 0 1 1 0 1 1 1 1];

SE1=[0 1 0;…
1 1 1;…
0 1 0];



%% Section 2


d=imerode (c,SE2);


%% final result
e=b & d; % b intersect d


subplot(3,3,1),imshow (a);title(‘a’);subplot(3,3,2),imshow (SE1);title(‘SE1’);subplot(3,3,3),imshow (b);title(‘b (imerode a and SE 1)’);
subplot(3,3,4),imshow (c);title(‘c (complement of a)’);subplot(3,3,5),imshow (SE2);title(‘SE2’);subplot(3,3,6),imshow (d);title(‘d (imerode c and SE 2)’);
subplot(3,3,7),imshow (e);title(‘intersect b and d’);


This code has been tested and should working perfectly. The title on top of each image with explanation using miss or hit manually on the above should be able to make you understand the source code. The result figure as show below.

Miss or Hit Matlab


Take note that the question is same between theoretical or manual way with the Matlab code. However, there is a difference in the result between both two. It’s normal since erosion definition in Matlab is different with theoretical erosion.

Thank You.


2 thoughts on “Matlab – Miss or Hit Transform”

  1. i m using you code and i am implementing hit miss transformation just like in book page 640 chapter 9
    dip book rafael c.gonzalez 3rd edition but i am receiving wrong results

Leave a Reply

Your email address will not be published. Required fields are marked *