Image padding is an essential technique in image processing which is used to maintain data consistency at the edges of an image. It involves adding layers of pixels to the image, ensuring uniform processing when applying filters and other operations. This prevents information loss and supports the desired output dimensions, making it essential for precise image analysis and enhancement.
Zero padding adds black borders around the image; black borders are achieved by adding pixels with intensity value 0 around the image. The thickness of the borders depends on the type of filter kernel used.
It’s the simplest form of padding and is often used when we want to maintain the original size of the data while adding a border of uniform values.
It defines a function zero_padding
that adds zero padding to an input array based on the specified padding size. In this case, it applies zero padding with a size of 1 to input_array
and prints the resulting array with zeros added around its edges.
import numpy as npimage_array = np.array([[1, 2, 3, 4, 5],[6, 7, 0, 1, 2],[3, 4, 5, 6, 7],[0, 1, 2, 3, 4],[5, 6, 7, 0, 1]])def zero_padding(arr, padding_size):return np.pad(arr, pad_width=padding_size, mode='constant', constant_values=0)# Apply zero padding with a padding size of 1zero_padded_image = zero_padding(image_array, 1)print("\nZero Padded Array:")print(zero_padded_image)
Mirror padding, also known as symmetric padding or reflective padding, adds a boundary around the image by mirror-reflecting the image on the original image border. The thickness of the boundary can be adjusted.
The pixels near the vertical and horizontal edges act as the line of reflection that adds new boundary pixels directly above/below and left/right of the image. For the remaining boundary pixels, the corner pixels act as the line of reflection.
Note: The boundary pixel values become the edge over which the values are reflected to get the padding boundary values.
Mirror padding is applicable when the areas near the border contain important image details, and we want to extend those details. It reflects or mirrors the values at the edges to fill the padded regions. This helps in maintaining the continuity of patterns and textures at the borders. Mirror padding is useful when the border regions have intricate patterns or features that we want to extend without creating abrupt changes or artifacts.
It includes a function named mirror_padding
that applies mirror padding to the input array, extending it by duplicating values from the edges based on the specified padding size. In this case, it applies mirror padding with a size of 1 to input_array
and then displays the resulting array with mirrored values added around its edges.
import numpy as npimage_array = np.array([[1, 2, 5, 9, 6],[4, 7, 0, 1, 2],[3, 4, 5, 6, 7],[0, 1, 2, 3, 4],[5, 6, 7, 0, 1]])def mirror_padding(arr, padding_size):return np.pad(arr, pad_width=padding_size, mode='reflect')# Apply mirror padding with a padding size of 1mirror_padded_image = mirror_padding(image_array, 2)print("\nMirror Padded Array:")print(mirror_padded_image)
Replicate padding adds the closest values outside the boundary, ensuring that values outside the boundary are set equal to the nearest image border value.
Replicate padding is used when we want to preserve the values at the edges of our data while extending it. It effectively replicates the values of the nearest edge pixel to fill the padded regions. This type of padding is suitable when the areas near the border of the image or data have a relatively constant or uniform pattern that we want to maintain. For example, if we have an image with a solid color border, replicate padding would be a good choice to extend that solid color to the padded regions, preserving the appearance of a continuous border.
It defines a function named replicate_padding
that applies replicate padding to the input array, extending it by copying values from the edges based on the specified padding size. In this case, it applies replicate padding with a size of 1 to input_array
and then displays the resulting array with replicated values added around its edges.
import numpy as npimage_array = np.array([[1, 2, 3, 4, 5],[6, 7, 0, 1, 2],[3, 4, 5, 6, 7],[0, 1, 2, 3, 4],[5, 6, 7, 0, 1]])def replicate_padding(arr, padding_size):return np.pad(arr, pad_width=padding_size, mode='edge')# Apply replicate padding with a padding size of 1replicate_padded_image = replicate_padding(image_array, 1)print("\nReplicate Padded Array:")print(replicate_padded_image)
Selecting the appropriate padding method is crucial to ensure that the extended data or image behaves as expected in various image processing and machine learning tasks.
Free Resources