Morphological Transformations¶

Goal¶

In this chapter,
  • Nosotros will learn different morphological operations similar Erosion, Dilation, Opening, Closing etc.
  • Nosotros will see dissimilar functions like : cv2.erode(), cv2.dilate(), cv2.morphologyEx() etc.

Theory¶

Morphological transformations are some unproblematic operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original epitome, second one is called structuring chemical element or kernel which decides the nature of operation. 2 bones morphological operators are Erosion and Dilation. Then its variant forms similar Opening, Endmost, Slope etc also comes into play. We volition see them i-past-one with assist of following image:

Input Image

1. Erosion¶

The basic idea of erosion is just like soil erosion simply, it erodes abroad the boundaries of foreground object (Always try to keep foreground in white). And then what does it do? The kernel slides through the image (every bit in 2D convolution). A pixel in the original image (either 1 or 0) volition exist considered 1 simply if all the pixels under the kernel is one, otherwise it is eroded (made to goose egg).

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 just white region decreases in the image. It is useful for removing pocket-size white noises (equally we have seen in colorspace affiliate), disassemble two continued objects etc.

Here, equally an instance, I would use a 5x5 kernel with full of ones. Let's see it how it works:

                                    import                  cv2                  import                  numpy                  as                  np                  img                  =                  cv2                  .                  imread                  (                  'j.png'                  ,                  0                  )                  kernel                  =                  np                  .                  ones                  ((                  5                  ,                  five                  ),                  np                  .                  uint8                  )                  erosion                  =                  cv2                  .                  erode                  (                  img                  ,                  kernel                  ,                  iterations                  =                  1                  )                

Result:

Erosion

2. Dilation¶

It is just opposite of erosion. Here, a pixel element is 'ane' if atleast i pixel nether the kernel is '1'. So it increases the white region in the image or size of foreground object increases. Usually, in cases similar racket removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we amplify it. Since noise is gone, they won't come dorsum, just our object area increases. It is too useful in joining broken parts of an object.

                                    dilation                  =                  cv2                  .                  dilate                  (                  img                  ,                  kernel                  ,                  iterations                  =                  1                  )                

Event:

Dilation

three. Opening¶

Opening is merely another name of erosion followed by dilation. It is useful in removing noise, as we explained above. Here nosotros use the function, cv2.morphologyEx()

                                    opening                  =                  cv2                  .                  morphologyEx                  (                  img                  ,                  cv2                  .                  MORPH_OPEN                  ,                  kernel                  )                

Consequence:

Opening

4. Endmost¶

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

                                    closing                  =                  cv2                  .                  morphologyEx                  (                  img                  ,                  cv2                  .                  MORPH_CLOSE                  ,                  kernel                  )                

Result:

Closing

5. Morphological Gradient¶

It is the departure betwixt dilation and erosion of an image.

The result will look similar the outline of the object.

                                    gradient                  =                  cv2                  .                  morphologyEx                  (                  img                  ,                  cv2                  .                  MORPH_GRADIENT                  ,                  kernel                  )                

Outcome:

Gradient

6. Height Hat¶

It is the divergence between input image and Opening of the image. Beneath instance is washed for a 9x9 kernel.

                                    tophat                  =                  cv2                  .                  morphologyEx                  (                  img                  ,                  cv2                  .                  MORPH_TOPHAT                  ,                  kernel                  )                

Consequence:

Top Hat

seven. Blackness Lid¶

It is the deviation between the endmost of the input epitome and input image.

                                    blackhat                  =                  cv2                  .                  morphologyEx                  (                  img                  ,                  cv2                  .                  MORPH_BLACKHAT                  ,                  kernel                  )                

Result:

Black Hat

Structuring Element¶

We manually created a structuring elements in the previous examples with help of Numpy. Information technology is rectangular shape. Just in some cases, you lot may need elliptical/circular shaped kernels. So for this purpose, OpenCV has a function, cv2.getStructuringElement(). You only laissez passer the shape and size of the kernel, you get the desired kernel.

                                # Rectangular Kernel                >>>                cv2                .                getStructuringElement                (                cv2                .                MORPH_RECT                ,(                5                ,                v                ))                array                ([[                one                ,                1                ,                1                ,                1                ,                1                ],                [                i                ,                i                ,                1                ,                ane                ,                1                ],                [                1                ,                1                ,                1                ,                1                ,                1                ],                [                1                ,                1                ,                one                ,                ane                ,                one                ],                [                ane                ,                i                ,                1                ,                one                ,                i                ]],                dtype                =                uint8                )                # Elliptical Kernel                >>>                cv2                .                getStructuringElement                (                cv2                .                MORPH_ELLIPSE                ,(                5                ,                5                ))                array                ([[                0                ,                0                ,                1                ,                0                ,                0                ],                [                1                ,                1                ,                i                ,                1                ,                1                ],                [                1                ,                1                ,                i                ,                1                ,                1                ],                [                1                ,                1                ,                1                ,                i                ,                i                ],                [                0                ,                0                ,                1                ,                0                ,                0                ]],                dtype                =                uint8                )                # Cross-shaped Kernel                >>>                cv2                .                getStructuringElement                (                cv2                .                MORPH_CROSS                ,(                5                ,                5                ))                assortment                ([[                0                ,                0                ,                one                ,                0                ,                0                ],                [                0                ,                0                ,                1                ,                0                ,                0                ],                [                ane                ,                1                ,                one                ,                1                ,                1                ],                [                0                ,                0                ,                1                ,                0                ,                0                ],                [                0                ,                0                ,                1                ,                0                ,                0                ]],                dtype                =                uint8                )              

Exercises¶