[英]Shapes not matching in numpy.convolve
錯誤信息:
operands could not be broadcast together with shapes (603) (613)
我該怎么辦?
兩個列表的長度都必須相同嗎?
還是應該零填充?
這是我的代碼:
def gaussian_smooth1(img, sigma):
'''
Do gaussian smoothing with sigma.
Returns the smoothed image.
'''
result = np.zeros_like(img)
#get the filter
filter = gaussian_filter(sigma)
#get the height and width of img
width = len(img[0])
height = len(img)
#smooth every color-channel
for c in range(3):
#smooth the 2D image img[:,:,c]
#tip: make use of numpy.convolve
for x in range(height):
result[x,:,c] = np.convolve(filter,img[x,:,c])
for y in range(width):
result[:,y,c] = np.convolve(filter,img[:,y,c])
return result
出現問題是因為您沒有指定正確的mode
。
在文檔中閱讀:
numpy.convolve
numpy.convolve的默認值為mode='full'
。
這將在每個重疊點返回卷積,輸出形狀為(N + M-1,)。
N
是輸入數組的大小, M
是過濾器的大小。 因此,輸出大於輸入。
相反,您想使用np.convolve(filter,img[...],mode='same')
。
還可以看看scipy.convolve ,它允許使用FFT進行2D卷積。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.