Error message:
operands could not be broadcast together with shapes (603) (613)
What should I do?
Do both of the list need to be the same length?
Or should I zero-pad it?
Here's my code:
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
The problem arises because you do not specify the right mode
.
Read it up in the documentation:
numpy.convolve
The default for numpy.convolve is mode='full'
.
This returns the convolution at each point of overlap, with an output shape of (N+M-1,).
N
is the size of the input array, M
is the size of the filter. So the output is larger than the input.
Instead you want to use np.convolve(filter,img[...],mode='same')
.
Also have a look at scipy.convolve which allows 2D convolution using the FFT.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.