我正在尝试仅使用 NumPy 来实现图像卷积代码,类似于cv2.filter2D(...)的做法。
import numpy as np
import time
# kernal
H = np.array([[0,1,0],[1,-4,1],[0,1,0]])
# image
SEED = 23
img = np.random.RandomState(SEED).randint(10, size=(4, 4))
# shapes
Hi, Wi = img.shape
Hk, Wk = H.shape
hk = Hk//2
wk = Wk//2
# padding
new_img = np.pad(img, (hk, wk), 'constant', constant_values=0)
pHi, pWi = new_img.shape
print('img: ')
print(new_img)
print('kernal: ')
print(H)
print('\n')
# image convolution
##################################################
# Method 1
st = time.time()
out = np.zeros((Hi, Wi))
for i in range(hk, pHi-hk):
for j in range(wk, pWi-wk):
batch = new_img[i-hk:i+hk+1, j-wk:j+wk+1]
out[i-hk][j-wk] = np.sum(batch*H)
print('out: ')
print(out)
print("[*] process time : %f" % (time.time()- st))
print('\n')
##################################################
# Method 2
st = time.time()
Hi, Wi = img.shape
out = np.zeros((Hi, Wi))
H_1d = H.ravel()
hl = len(H_1d)
for i in range(Wi):
img_slice = new_img[:, i:Hk+i]
for j in range(Hi):
out[j][i] = np.sum(img_slice[j:j+Hk,:]*H)
il = len(img_slice)
h1 = 0
h2 = hl
while h2 <= il:
index = h1//Hk
# print(index)
out[index][i] = np.sum(img_slice[h1:h2]*H_1d)
h1 = h1 + Hk
h2 = h2 + Hk
print('out: ')
print(out)
print("[*] process time : %f" % (time.time()- st))
print('\n')
##################################################
它有效,但我需要更快、更有效的实施。为了使算法更快,我尝试在此处查看图像的卷积过程中找到模式,但仍然无法弄清楚应该做什么。
有人可以帮助我改进当前的代码,使其更快,仅使用 NumPy 吗?
交互式爱情
相关分类