修复 图像添加高斯噪声的代码bug和可读性 #254
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
why:
原仓库训练代码里没有使用 添加高斯噪声的数据增强方式,而是椒盐噪声;若使用添加高斯噪声去数据增强,则训练代码会报错
tofix:
image_proc.py 中的random_gaussian函数里
1)第171行img = img.reshape([width, height]) 应该加入图像的通道数判断,然后修改为:img = img.reshape([height, width, channels])
2) 虽然random_gaussian函数里,img 的尺寸为1024x1024,width 和 height 相同;但是代码里的 width, height = img.shape 表述有歧义,如果config.py 里的size设置了宽和高不一样的尺寸,容易导致bug,应该写为 height, width = img.shape[:2]
test code:
-- coding: utf-8 --
import cv2
import random
import numpy as np
from PIL import Image
def random_gaussian(image, mean=0.1, sigma=0.35):
def gaussianNoisy(im, mean=mean, sigma=sigma):
for _i in range(len(im)):
im[_i] += random.gauss(mean, sigma)
return im
if name == 'main':
image = cv2.imread('./images/test.jpg')
size = (1024, 1024)
image = cv2.resize(image, size, interpolation=cv2.INTER_LINEAR)
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).convert('RGB')
gaussian_image = random_gaussian(image)
gaussian_image.save("./images/test_gaussian.png")