Skip to content

Morphological Transform

Python
import random

import cv2
from matplotlib import pyplot as plt

import albumentations as A
Python
def visualize(image):
    plt.figure(figsize=(10, 5))
    plt.axis('off')
    plt.imshow(image)
Python
def load_rgb(image_path):
    image = cv2.imread(image_path)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Load the image from the disk

Python
img_path = "../images/scan.jpeg"
img = load_rgb(img_path)

Visualize the original image

Python
visualize(img)

png

Dilation

Dilation expands the white (foreground) regions in a binary or grayscale image.

Python
transform = A.Compose([A.Morphological(p=1, scale=(2, 3), operation='dilation')], p=1)
Python
transformed = transform(image=img)
visualize(transformed["image"])

png

Erosion

Erosion shrinks the white (foreground) regions in a binary or grayscale image.

Python
transform = A.Compose([A.Morphological(p=1, scale=(2, 3), operation='erosion')], p=1)
Python
transformed = transform(image=img)
visualize(transformed["image"])

png