Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

python - Create Image Patches Using Numpy

Let's assume that I have input of 4x4 image with 3 channels with following pixel values: enter image description here

And I want to make it to 12 x 9 matrix of image patches like this (using 2x2 kernel on a 4x4 image): enter image description here

How can I achieve this using numpy? Thank you for your help.

question from:https://stackoverflow.com/questions/65950498/create-image-patches-using-numpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming 4x4x3 as input and 12x9 as output

from scipy.signal import convolve
import numpy as np

# creating the 4x4x3 input image
a = np.arange( 1,16+1).reshape(4,4)
b = np.arange(17,32+1).reshape(4,4)
c = np.arange(33,48+1).reshape(4,4)
i_4x4x3 = np.dstack((a, b, c))

# creating four 2x2 kernels
mask_tl = np.array([0,0,0,1]).reshape(2,2)
mask_tr = np.array([0,0,1,0]).reshape(2,2)
mask_bl = np.array([0,1,0,0]).reshape(2,2)
mask_br = np.array([1,0,0,0]).reshape(2,2)
mask_tl = mask_tl[:,:,None]
mask_tr = mask_tr[:,:,None]
mask_bl = mask_bl[:,:,None]
mask_br = mask_br[:,:,None]

# convolving the input with all four kernels
tl = convolve(i_4x4x3, mask_tl, mode='valid')
tr = convolve(i_4x4x3, mask_tr, mode='valid')
bl = convolve(i_4x4x3, mask_bl, mode='valid')
br = convolve(i_4x4x3, mask_br, mode='valid')
i = np.dstack((
    tl.reshape(-1,3),
    tr.reshape(-1,3),
    bl.reshape(-1,3),
    br.reshape(-1,3)))
i=i.reshape(i.shape[0],-1).transpose()

display(a,b,c)
display(i)

Output:

array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
array([[17, 18, 19, 20],
       [21, 22, 23, 24],
       [25, 26, 27, 28],
       [29, 30, 31, 32]])
array([[33, 34, 35, 36],
       [37, 38, 39, 40],
       [41, 42, 43, 44],
       [45, 46, 47, 48]])
array([[ 1,  2,  3,  5,  6,  7,  9, 10, 11],
       [ 2,  3,  4,  6,  7,  8, 10, 11, 12],
       [ 5,  6,  7,  9, 10, 11, 13, 14, 15],
       [ 6,  7,  8, 10, 11, 12, 14, 15, 16],
       [17, 18, 19, 21, 22, 23, 25, 26, 27],
       [18, 19, 20, 22, 23, 24, 26, 27, 28],
       [21, 22, 23, 25, 26, 27, 29, 30, 31],
       [22, 23, 24, 26, 27, 28, 30, 31, 32],
       [33, 34, 35, 37, 38, 39, 41, 42, 43],
       [34, 35, 36, 38, 39, 40, 42, 43, 44],
       [37, 38, 39, 41, 42, 43, 45, 46, 47],
       [38, 39, 40, 42, 43, 44, 46, 47, 48]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...