我想将眼睛区域中任何像素强度值的 5% 作为噪声添加到整个图像中,所以我想要做的是选择眼睛区域内的任何像素并给定它们的简单像素强度添加 5% 的高斯噪声到整个图像。
def generate_noisy_image(x, variance):
noise = np.random.normal(0, variance, (1, x.shape[0]))
return x + noise
def loadimage(path):
filepath_list = listdir(path)
for filepath in filepath_list:
img = Image.open(path + filepath)
img = img.resize((81, 150))
img = np.asarray(img)
generate_noisy_image(img, 0.025)
img = Image.fromarray(img)
img.save('C:/Users/noisy-images/'+filepath, 'JPEG')
loadimage('C:/Users/my_images/')
ValueError:操作数无法与形状一起广播 (150,81) (1,150)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-96-1bebb687f5e7> in <module>
11
12
---> 13 loadimage('source path from images')
14
<ipython-input-96-1bebb687f5e7> in loadimage(path)
5 img = img.resize((81, 150))
6 img = np.asarray(img)
----> 7 generate_noisy_image(img, 0.025)
8 print(generate_noisy_image.shape)
9 img = Image.fromarray(img)
<ipython-input-95-7cc3346953f6> in generate_noisy_image(x, variance)
1 def generate_noisy_image(x, variance):
2 noise = np.random.normal(0, variance, (1, x.shape[0]))
----> 3 return x + noise