OpenCV  5.0.0-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
Morphological Transformations

Goal

Theory

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring element or kernel which decides the nature of operation. Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play. We will see them one-by-one with help of following image:

image

1. Erosion

The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of foreground object (Always try to keep foreground in white). So what it does? The kernel slides through the image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded (made to zero).

So what happends is that, all the pixels near boundary will be discarded depending upon the size of kernel. So the thickness or size of the foreground object decreases or simply white region decreases in the image. It is useful for removing small white noises (as we have seen in colorspace chapter), detach two connected objects etc.

We use the function: cv.erode (src, dst, kernel, anchor = new cv.Point(-1, -1), iterations = 1, borderType = cv.BORDER_CONSTANT, borderValue = cv.morphologyDefaultBorderValue())

Parameters
srcinput image; the number of channels can be arbitrary, but the depth should be one of cv.CV_8U, cv.CV_16U, cv.CV_16S, cv.CV_32F or cv.CV_64F.
dstoutput image of the same size and type as src.
kernelstructuring element used for erosion.
anchorposition of the anchor within the element; default value new cv.Point(-1, -1) means that the anchor is at the element center.
iterationsnumber of times erosion is applied.
borderTypepixel extrapolation method(see cv.BorderTypes).
borderValueborder value in case of a constant border

Try it

2. Dilation

It is just opposite of erosion. Here, a pixel element is '1' if at least one pixel under the kernel is '1'. So it increases the white region in the image or size of foreground object increases. Normally, in cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won't come back, but our object area increases. It is also useful in joining broken parts of an object.

We use the function: cv.dilate (src, dst, kernel, anchor = new cv.Point(-1, -1), iterations = 1, borderType = cv.BORDER_CONSTANT, borderValue = cv.morphologyDefaultBorderValue())

Parameters
srcinput image; the number of channels can be arbitrary, but the depth should be one of cv.CV_8U, cv.CV_16U, cv.CV_16S, cv.CV_32F or cv.CV_64F.
dstoutput image of the same size and type as src.
kernelstructuring element used for dilation.
anchorposition of the anchor within the element; default value new cv.Point(-1, -1) means that the anchor is at the element center.
iterationsnumber of times dilation is applied.
borderTypepixel extrapolation method(see cv.BorderTypes).
borderValueborder value in case of a constant border

Try it

3. Opening

Opening is just another name of erosion followed by dilation. It is useful in removing noise.

We use the function: cv.morphologyEx (src, dst, op, kernel, anchor = new cv.Point(-1, -1), iterations = 1, borderType = cv.BORDER_CONSTANT, borderValue = cv.morphologyDefaultBorderValue())

Parameters
srcsource image. The number of channels can be arbitrary. The depth should be one of cv.CV_8U, cv.CV_16U, cv.CV_16S, cv.CV_32F or cv.CV_64F
dstdestination image of the same size and type as source image.
optype of a morphological operation, (see cv.MorphTypes).
kernelstructuring element. It can be created using cv.getStructuringElement.
anchoranchor position with the kernel. Negative values mean that the anchor is at the kernel center.
iterationsnumber of times dilation is applied.
borderTypepixel extrapolation method(see cv.BorderTypes).
borderValueborder value in case of a constant border. The default value has a special meaning.

Try it

4. Closing

Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects, or small black points on the object.

Try it

5. Morphological Gradient

It is the difference between dilation and erosion of an image.

The result will look like the outline of the object.

Try it

6. Top Hat

It is the difference between input image and Opening of the image.

Try it

7. Black Hat

It is the difference between the closing of the input image and input image.

Try it

Structuring Element

We manually created a structuring elements in the previous examples with help of cv.Mat.ones. It is rectangular shape. But in some cases, you may need elliptical/circular shaped kernels. So for this purpose, OpenCV has a function, cv.getStructuringElement(). You just pass the shape and size of the kernel, you get the desired kernel.

We use the function: cv.getStructuringElement (shape, ksize, anchor = new cv.Point(-1, -1))

Parameters
shapeelement shape that could be one of cv.MorphShapes
ksizesize of the structuring element.
anchoranchor position within the element. The default value [−1,−1] means that the anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor position. In other cases the anchor just regulates how much the result of the morphological operation is shifted.

Try it