Exemples de comment augmenter la taille d'une matrice 2d ("pad an array") avec numpy en python ?
Créer un tableau 2D avec numpy
Considérons le tableau suivant :
\begin{equation}
A = \left( \begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right)
\end{equation}
qui peut être implémenté en utilisant numpy et python :
import numpy as npA = np.arange(0,12)A = A.reshape(4,3)print(A)
donne
[[ 0 1 2][ 3 4 5][ 6 7 8][ 9 10 11]]
et
print( A.shape )
donne
(4, 3)
Pad un tableau numpy
Pour augmenter la taille d'une matrice 2d, une solution consiste à utiliser numpy.pad:
Pad avec des valeurs constantes
Ajouter un pad (1,1) avec la valeur -99
B = np.pad(A, (1, 1), constant_values=-99)
donne
[[-99 -99 -99 -99 -99][-99 0 1 2 -99][-99 3 4 5 -99][-99 6 7 8 -99][-99 9 10 11 -99][-99 -99 -99 -99 -99]]
Ajouter un pad (3,3) avec la valeur -99
B = np.pad(A, (3, 3), constant_values=-99)
donne
[[-99 -99 -99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99 -99 -99][-99 -99 -99 0 1 2 -99 -99 -99][-99 -99 -99 3 4 5 -99 -99 -99][-99 -99 -99 6 7 8 -99 -99 -99][-99 -99 -99 9 10 11 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99 -99 -99]]
Ajouter un pad (3,1) avec la valeur -99
B = np.pad(A, (3, 1), constant_values=-99)
donne
[[-99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99][-99 -99 -99 -99 -99 -99 -99][-99 -99 -99 0 1 2 -99][-99 -99 -99 3 4 5 -99][-99 -99 -99 6 7 8 -99][-99 -99 -99 9 10 11 -99][-99 -99 -99 -99 -99 -99 -99]]
Pad avec la valeur la plus proche
Ajouter un pad (1,1) avec la valeur la plus proche
B = np.pad(A, (1, 1), 'edge')
donne
[[ 0 0 1 2 2][ 0 0 1 2 2][ 3 3 4 5 5][ 6 6 7 8 8][ 9 9 10 11 11][ 9 9 10 11 11]]
et
print(B.shape)
donne
(6, 5)
Exemples
Trouver le voisinage d'un pixel
B = np.pad(A, (1, 1), 'edge')
puis découpé la matrice B pour trouver les 8 pixels de voisinage"
Côté droit
B[0:-2,0:-2]B[1:-1,0:-2]B[2:,0:-2]
donne
[[0 0 1][0 0 1][3 3 4][6 6 7]][[ 0 0 1][ 3 3 4][ 6 6 7][ 9 9 10]][[ 3 3 4][ 6 6 7][ 9 9 10][ 9 9 10]]
Milieu haut et bas :
B[0:-2,1:-1]B[2:,1:-1]
donne
[[0 1 2][0 1 2][3 4 5][6 7 8]][[ 3 4 5][ 6 7 8][ 9 10 11][ 9 10 11]]
Côté gauche
B[0:-2,2:]B[1:-1,2:]B[2:,2:]
donne
[[1 2 2][1 2 2][4 5 5][7 8 8]][[ 1 2 2][ 4 5 5][ 7 8 8][10 11 11]][[ 4 5 5][ 7 8 8][10 11 11][10 11 11]]
Ajouter un cadre à une image
from matplotlib import imageimport matplotlib.pyplot as pltimg = image.imread("eiffel-tower.jpeg")plt.imshow(img)plt.show()print(img.shape)

img1 = np.pad(img, ((100, 100), (200, 200), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_01.png", bbox_inches='tight', dpi=100)plt.show()
img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_02.png", bbox_inches='tight', dpi=100)plt.show()
img1 = np.pad(img, ((0, 0), (200, 200), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_03.png", bbox_inches='tight', dpi=100)plt.show()
img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), 'edge')print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_04.png", bbox_inches='tight', dpi=100)plt.show()




