简体   繁体   English

使用 Numpy 创建映像补丁

[英]Create Image Patches Using Numpy

Let's assume that I have input of 4x4 image with 3 channels with following pixel values:假设我输入了具有 3 个通道的 4x4 图像,其像素值如下: 在此处输入图像描述

And I want to make it to 12 x 9 matrix of image patches like this (using 2x2 kernel on a 4x4 image):我想把它变成这样的 12 x 9 图像块矩阵(在 4x4 图像上使用 2x2 kernel): 在此处输入图像描述

How can I achieve this using numpy?如何使用 numpy 实现这一目标? Thank you for your help.谢谢您的帮助。

Assuming 4x4x3 as input and 12x9 as output假设 4x4x3 作为输入,12x9 作为 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: 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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM