Image padding with reflect (mirror) in Python
Question:
I have an image loaded in python as an np.array with shape (7, 7). I need to apply a filtering process to the image. For this I need to apply a kernel that will convolve the 2D image (like a moving window). However, to get an output filtered image with the same shape as the input I need to expand the original image (padding) and fill these pixels as a mirror before filtering.
Below I illustrate my problem with an image:
Note: Padding must be applied to all corners of the image.
How can I create this padding?
Here some example data:
img = np.array([[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 37, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]], dtype=np.uint8)
plt.imshow(img)
Answers:
We may use np.pad with mode
= 'reflect'
:
img = np.pad(img, ((2, 2), (2, 2)), 'reflect')
Example:
import numpy as np
img = np.array([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 37, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]], dtype=np.uint8)
pimg = np.pad(img, ((2, 2), (2, 2)), 'reflect')
Value of pimg
:
array([[17, 16, 15, 16, 17, 18, 19, 20, 21, 20, 19],
[10, 9, 8, 9, 10, 11, 12, 13, 14, 13, 12],
[ 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5],
[10, 9, 8, 9, 10, 11, 12, 13, 14, 13, 12],
[17, 16, 15, 16, 17, 18, 19, 20, 21, 20, 19],
[24, 23, 22, 23, 24, 25, 26, 27, 28, 27, 26],
[31, 30, 29, 30, 31, 32, 33, 34, 35, 34, 33],
[37, 37, 36, 37, 37, 39, 40, 41, 42, 41, 40],
[45, 44, 43, 44, 45, 46, 47, 48, 49, 48, 47],
[37, 37, 36, 37, 37, 39, 40, 41, 42, 41, 40],
[31, 30, 29, 30, 31, 32, 33, 34, 35, 34, 33]], dtype=uint8)
I have an image loaded in python as an np.array with shape (7, 7). I need to apply a filtering process to the image. For this I need to apply a kernel that will convolve the 2D image (like a moving window). However, to get an output filtered image with the same shape as the input I need to expand the original image (padding) and fill these pixels as a mirror before filtering.
Below I illustrate my problem with an image:
Note: Padding must be applied to all corners of the image.
How can I create this padding?
Here some example data:
img = np.array([[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 37, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]], dtype=np.uint8)
plt.imshow(img)
We may use np.pad with mode
= 'reflect'
:
img = np.pad(img, ((2, 2), (2, 2)), 'reflect')
Example:
import numpy as np
img = np.array([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 37, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]], dtype=np.uint8)
pimg = np.pad(img, ((2, 2), (2, 2)), 'reflect')
Value of pimg
:
array([[17, 16, 15, 16, 17, 18, 19, 20, 21, 20, 19],
[10, 9, 8, 9, 10, 11, 12, 13, 14, 13, 12],
[ 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5],
[10, 9, 8, 9, 10, 11, 12, 13, 14, 13, 12],
[17, 16, 15, 16, 17, 18, 19, 20, 21, 20, 19],
[24, 23, 22, 23, 24, 25, 26, 27, 28, 27, 26],
[31, 30, 29, 30, 31, 32, 33, 34, 35, 34, 33],
[37, 37, 36, 37, 37, 39, 40, 41, 42, 41, 40],
[45, 44, 43, 44, 45, 46, 47, 48, 49, 48, 47],
[37, 37, 36, 37, 37, 39, 40, 41, 42, 41, 40],
[31, 30, 29, 30, 31, 32, 33, 34, 35, 34, 33]], dtype=uint8)