我有以下代码用于在二维上进行卷积:
matrix = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
kernel = np.array([[1,-1],[1,-1]])
kr, kc = kernel.shape
mr, mc = matrix.shape
output = np.empty((mc-kc+1, mr-kr+1))
for row in range(mc-kc+1):
for column in range(mr-kr+1):
output[row][column] = (matrix[row:row+kr,column:column+kc]*kernel).sum()
print(output)
我想在真彩色图像上尝试这个,这是我试过的
#matrix is now 3D
print(matrix.shape)
Out: (340, 360, 3)
#kernel is now 3D
print(kernel.shape)
Out: (34, 36, 3)
kr, kc, kdim = kernel.shape
mr, mc, mdim = matrix.shape
output = np.empty((mc-kc+1, mr-kr+1, 3))
for row in range(mc-kc+1):
for column in range(mr-kr+1):
for dim in range(3):
output[row][column][dim] = (matrix[row:row+kr,column:column+kc]*kernel).sum()
print(output)
Out: ValueError: operands could not be broadcast together with shapes (33,36,3) (34,36,3)
我认为深度维度存在问题,但我仍然没有看到如何修复它。帮助
答案 0 :(得分:0)
您已在output
声明中以及在行和列上的循环中交换行和列。试试这个版本:
# note: [rows, columns, channel]
output = np.empty((mr - kr + 1, mc - kc + 1, 3))
# note: `row` loops over matrix rows, `column` - over columns
for row in range(mr - kr + 1):
for column in range(mc - kc + 1):
for dim in range(3):
output[row][column][dim] = (matrix[row:row + kr, column:column + kc] * kernel).sum()
print(output)