Imaging Toolbox

Morphology in Delphi

Morphology

Morphology presents methods to change the shape of objects or the interrelationship between parts of an object.
The morphological methods can be described in simple terms of adding or removing pixels according to specific rules, which depends on the pattern of neighbour pixels.

The basic methods are: erode which peals a layer of pixels off the objects, and dilate, the counterpart to erode, which adds a layer of pixels to the objects.
Combining erosion followed by dilation is called opening, and is used to separate touching features or open these. Again, a counterpart exists where dilation is followed by erosion causing features to close.

All morphological methods described in this paper, are available in the Imaging Toolkit for Delphi.

Dilate

Dilate increases features in the image by adding an extra layer of feature pixels. The Dilate method uses a 3x3 matrix, where each background (OFF) centre pixel that is incountered, results in a search within the eight neighbour locations for feature pixels (ON), counting the feature pixels. If the number of ON pixels is greater than the Coefficient the centre OFF pixel is turned ON.

[Source Image] [Dilated image]
Original Dilated

If you dilate a circle numerous times with Coefficient set to 0, the circle shape will change towards a square. The reason that dilation proceeds faster in the diagonal (45° degree) directions are because of the greater spacing between pixels at 45° degree angles, as opposed to neighbour pixel at 90° angles. Note that square and rectangular shapes are stable in dilation.

[Dilated Image]

If the Coefficient is set to 1, the circle shape would conform to a dimond shape.

[Dilated Image]

However, alternating between Coefficient = 0 and Coefficient = 1, produces a pattern much closer to the original circular shape.

[Dilated Image]

Setting the Coefficient to higher values will slow the dilation process down, in the sense that less edge pixels are added per iteration. When Coefficient is set to a value of 5 or higher the effect of the dilation process becomes almost unnoticeable, with the exception that interior noicy (OFF) pixels are removed and the shape is maintained.

[Source Image] [Dilated image]
Noisy image Dilate, Coeficient = 5

If you have several individual features in the image that are located near each other, the Dilate method may cause these to merge together after N iterations. To avoid merging features we temporarily label each feature before dilating.

[Source Image] [Dilated image]
Original Features merged
[Dilated image]
Labled features

Erode

The Erode method erases the outer layer or features, thereby reducing their size. The Erode method uses a 3x3 matrix where each feature (ON) centre pixel that is incountered, results in a search within the eight neighbour locations for background pixels (OFF), and the found background pixels are counted. If the number of OFF pixels is greater than the Coefficient the centre ON pixel is turned OFF.

[Source Image] [Eroded image]
Original Eroded

If you erode for example a circle numerous times with Coefficient set to 0, the circle shape will change towards a dimond. The reason that erosion proceeds faster in the diagonal (45° degree) directions, is because of the greater spacing between pixels at 45° degree angles, as opposed to neighbour pixel at 90° angles. Note that square and rectangular shapes are stable in erosion.

[Eroded Image]

If the Coefficient is set to 1, the circle shape will conform to a squareish shape.

[Eroded Image]

However, alternating between Coefficient = 0 and Coefficient = 1, produces a pattern much closer to the original circular shape.

[Eroded Image]

Setting Coefficient to higher values will slow the erosion process down, in the sense that less edge pixels are pealed off per iteration. When Coefficient is set to a value of 5 or higher the effect of the erosion process becomes almost unnoticeable, with the exception that exterior noicy (ON) pixels are removed.

[Source Image] [Eroded image]
Noisy image Erode, Coeficient = 5

Open

The Open method is the combination of Erode followed by Dilate, and can be applied on images to separate touching features. Basically, Open first erodes the features N times until all become separated from each other, and then dilate the same number of times, i.e.increase features back to their original size.

[Source Image]
Original

[Opened Image]
Open, Iterations = 7, Coefficients = 0 and 1.

If Open cannot preserve the original shape of our features, the Open process can be implemented by first calling Erode with Iterations = N, where N is large enough to separate all features. Then apply Dilate with Iterations = M, where M > N. To avoid merging our features back together again, we'll temporarily label each feature. Finally, OR our original image and the processed image.

[Eroded Image]
Erode, Iterations = 8
[Dilated Image]
Dilate, Iterations = 10
[Ored Image]
Original OR’ed with dilated image

Close

The Close method is the combination of applying the method Dilate followed by Erode, and can be used to remove holes and cracks in features or combine sub-features. Basically, Close will expand the features size and "merge" neighbour sub-features and then descease the size back to the original.

[Source Image] [Closed Image]
Original Close, Iterations = 1
[Closed Image]
Close, Iterations = 6

As seen in the sample above, the small cracks are "removed" after a single iteration, but the larger holes remains. The holes in the two upper circles are not filled, until Iterations is set to 6.

The Coefficient were in the above example set to 0, as the circular features keept their original shape. Note that the dilation process on circles proceeds faster in the 45° degree directions, and likewise erosion also proceeds faster in the diagonal directions on circular shapes. The resulting closed circles would have been closer to their original shape if the Coefficient were alternating between 0 and 1 for each iteration, but this would require an extra iteration, i.e. a total of 7 iteration, to complete the closure of the holes.

Skeletonize

Skeleton produces a centre point or line(s) for each feature in the image that constitutes the internal structure of the features. Perhaps it is better explained in the following manor: Imagine that you set fire to an object from all sides, i.e. the boundary pixel, at the exact same time. As the fire burns towards the centre, two or more flames will eventually meet, and at this point extinguish one another, leaving behinde the centre structure of the object. Circles and squares are reduces to a single point, ellipses and rectangles are reduced to the centre line. The image below shows a few different shapes and their skeleton.
The image below shows the original image and the resulting image of skeletonizing superimposed onto this.

[Skeletonized Image]

Thinning

The Thin method generates a minimally connected line inside the objects at equidistance from the boundary. This method is somewhat similar to the Skeleton methods.
The image below shows the original image and the thinned image superimposed onto this.

[Thinned Image]

Shrinking

The Shrink method will reduce objects in the image to a single point located at the geometric centre. In other words this method finds the centre of mass of each object. If an object contains a hole, a "ring" is produced around the hole at equidistance from the nearest boundary.
The image below shows the original image and the resulting image of shrinking superimposed onto this.

[Shrinked Image]

Outline

The Outline method creates an outline of the features in the image. Outline produces a similar result as edge filters do on greyscale images.
The image below shows the original image and the outlined image superimposed onto this.

[Outline Image]

Read more about the Imaging Toolkit for Delphi.