Thursday, February 7, 2013

Hit-or-Miss in ImageJ



 Continuing the exploration of the Hit-or-Miss operator [Link], this second post shows how to use ImageJ for the detection of nodes and leaves in a graph using an approach similar to a Hit-or-Miss.


1- In ImageJ, a Hit-or-Miss ?


By default, there is no Hit-or-Miss operator in ImageJ, however, there is something similar. In the menu Process > Binary > Options..., the  dialog box of Fig.1 contains two text fields 'Iterations (1-100)' and 'Count (1-8)'.

Fig.1: Dialog box tuning the behavior of the Mathematical Morphology functions.

The text field entitled 'Count (1-8)' indicates the number of neighbors around the central pixel in the 3x3 kernel taking into account for the convolution operation. The default value '1' means that if at least one pixel is FALSE in the 3x3 kernel, then the central pixel is converted to FALSE during the erosion. This is the classical behavior of the erosion process.

2- Detecting leaves of a graph


Now, if we plan to detect the ends of a graph. The question is 'How many FALSE neighbors are surrounding the TRUE central pixel?' ... The answer is '7', because as shown in Fig. 4, an end is connected to only one TRUE neighbor.

0 0 0
0 1 0
0 1 0
Fig. 2: One of the eight structuring elements required to detect ends (leaves) of a graph. The other kernels are described in the previous post [Link].

If you modify the 'Count (1-8)' text field to '7', click on the OK button and run a 'Erode', only the pixels of the ends are removed (Fig. 3B). Thus, to visualize them, you have to XOR the original image and the eroded one (Fig. 3C).

Fig. 3: Erosion of image (A) with a Count (1-8) set to '7'. B) Eroded image. C) Result of (A XOR B) and inverted. For sake of clarity, a dilation was applied to the leaves.


3- What about the nodes (crossings) ?


Similarly, the crossings (nodes) of a graph have three or four TRUE neighbors corresponding to five and four FALSE, respectively (Fig. 4).

0 0 1   1 0 1
1 1 0   0 1 0
0 0 1   1 0 1
Fig. 4: Examples of a 3-branches and a 4-branches nodes, respectively. They are surrounded by five and four FALSE neighbors.
Rather than eroding the crossings (there is no way to set 4 and 5 in the 'Count (1-8)' text field), it is easier to remove all the pixels of the skeleton except the nodes. Indeed, the leaves (seven FALSE neighbors) and the branches (six FALSE neighbors) can be set to '6' in the 'Count (1-8)' text field.

Fig. 5: Detection of nodes (crossings) in the graph. The 'Count (1-8)' was set to '6'.

In conclusion, this is not as powerful as a genuine Hit-or-Miss operator − there is no way to detect corners, for example − but in many cases, this is enough for common tasks. Now, to go further, you can implement your own version of a Hit-or-Miss...

<< Previous: Hit-or-Miss − Definition




Further reading

Mathematical morphology [Link]
Image Processing [Link]

No comments:

Post a Comment