python三维卷积可以用什么函数? matlab只要用convn

Python012

python三维卷积可以用什么函数? matlab只要用convn,第1张

写了一个输入和卷积核dim=2是一样的(都是3)的卷积函数,可以试试多加一个for循环变成三维卷积

def conv3D(image, filter):

'''

三维卷积

:param image: 输入,shape为 [h,w,c], c=3

:param filter:  卷积核,shape为 [x,y,z], z=3

:return:

'''

h, w, c = image.shape

x, y, z = filter.shape

height_new = h - x + 1  # 输出 h

width_new = w - y + 1  # 输出 w

image_new = np.zeros((height_new, width_new), dtype=np.float)

for i in range(height_new):

for j in range(width_new):

r = np.sum(image[i:i+x, j:j+x, 0] * filter[:,:,0])

g = np.sum(image[i:i+y, j:j+y, 1] * filter[:,:,1])

b = np.sum(image[i:i+z, j:j+z, 2] * filter[:,:,2])

image_new[i, j] = np.sum([r,g,b])

image_new = image_new.clip(0, 255)

image_new = np.rint(image_new).astype('uint8')

return image_new

全部用文件IO的话可以这样: matlab把所有参数输出到一个文件里,然后用system命令调python脚本。python脚本读文件做计算结果再写文件。最后matlab再读文件得到结果。 假设python脚本的用法是: python xxx.py in.txt out.txt 则matlab调用命令...