pytorch图像加噪

skimage中random_noise使用的一些问题:

random_noise(image, mode='gaussian', seed=None, clip=True, **kwargs)
    Function to add random noise of various types to a floating-point image.
    
    Parameters
    ----------
    image : ndarray 
        Input image data. Will be converted to float. #输入为ndarry,输出为float
    mode : str, optional
        One of the following strings, selecting the type of noise to add:
#mode名字:
        - 'gaussian'  Gaussian-distributed additive noise.
        - 'localvar'  Gaussian-distributed additive noise, with specified
                      local variance at each point of `image`.
        - 'poisson'   Poisson-distributed noise generated from the data.
        - 'salt'      Replaces random pixels with 1.
        - 'pepper'    Replaces random pixels with 0 (for unsigned images) or
                      -1 (for signed images).
        - 's&p'       Replaces random pixels with either 1 or `low_val`, where
                      `low_val` is 0 for unsigned images or -1 for signed
                      images.
        - 'speckle'   Multiplicative noise using out = image + n*image, where
                      n is uniform noise with specified mean & variance.
    seed : int, optional
        If provided, this will set the random seed before generating noise,
        for valid pseudo-random comparisons.

#范围限定(对我来说,用默认clip(True)即可,我认为) 
   clip : bool, optional
        If True (default), the output will be clipped after noise applied
        for modes `'speckle'`, `'poisson'`, and `'gaussian'`. This is
        needed to maintain the proper image data range. If False, clipping
        is not applied, and the output may extend beyond the range [-1, 1].
    mean : float, optional
        Mean of random distribution. Used in 'gaussian' and 'speckle'.
        Default : 0.

#随机添加噪声的强度(方差,guassian, 范围[0.1],默认0.01)
    var : float, optional
        Variance of random distribution. Used in 'gaussian' and 'speckle'.
        Note: variance = (standard deviation) ** 2. Default : 0.01
    local_vars : ndarray, optional
        Array of positive floats, same shape as `image`, defining the local
        variance at every image point. Used in 'localvar'.

#随机添加噪声的强度(像素点个数, 椒盐, 范围[0.1], 默认0.05)
    amount : float, optional
        Proportion of image pixels to replace with noise on range [0, 1].
        Used in 'salt', 'pepper', and 'salt & pepper'. Default : 0.05
    salt_vs_pepper : float, optional
        Proportion of salt vs. pepper noise for 's&p' on range [0, 1].
        Higher values represent more salt. Default : 0.5 (equal amounts)
    
    Returns
    -------
    out : ndarray
        Output floating-point image data on range [0, 1] or [-1, 1] if the
        input `image` was unsigned or signed, respectively.

自己修改的代码:

def read_image(data_path):  # 读取二进制文件并返回归一化后的图片
    with open(data_path, 'rb') as f:
        data1 = np.fromfile(f, dtype=np.uint8)
        image = np.reshape(data1, (-1, 3, 96, 96))
        images = np.transpose(image, (0, 3, 2, 1))#转换通道.转成RGB通道
    return images/255.0

data_path = "/content/drive/MyDrive/train_X.bin"  # 数据集路径,r表示防止转义
images = read_image(data_path)

def gaussian_noise(images, sigma):
    """sigma: 噪声标准差""" #默认0.01,范围[0,1]
    sigma2 = sigma**2 #/ (255 ** 2)   # 噪声方差
    images_noisy = np.zeros_like(images)
    for ii in range(images.shape[0]):
        image = images[ii]
        # 使用skimage中的函数增加噪音
        noise_im = random_noise(image, mode="gaussian", var=sigma2, clip=True)
        images_noisy[ii] = noise_im
    return images_noisy

images_noise = gaussian_noise(images, 0.1)
print("image_noise:", images_noise.min(), "~", images_noise.max())

def salt_speckle_noise(images, ratio):
    """amount: 噪声数量占比""" #默认0.05,范围[0,1]

    images_noisy = np.zeros_like(images)
    for ii in range(images.shape[0]):
        image = images[ii]
        # 使用skimage中的函数增加噪音
        noise_im = random_noise(image, mode="s&p", amount=ratio) #salt_vs_pepper椒、盐噪声比默认0.5,范围[0.1]
        images_noisy[ii] = noise_im
    return images_noisy

images_noise = salt_speckle_noise(images, 0.05)
print("image_noise:", images_noise.min(), "~", images_noise.max())

关于加噪的其他注意问题:

1.使用 skimage.util.random_noise(img, mode="salt")会导致使用opencv去降噪的函数会提示无法支持的格式 ! 

问题的所在

skimage.util.random_noise()

out : ndarray

Output floating-point image data on range [0, 1] or [-1, 1] if the
input `image` was unsigned or signed, respectively.

该加噪函数(方法)将图像转化[0,1]或者[-1,1],而且数据类型变成了float64类型,而opencv处理的图像格式一般是uint8类型的,因此还需要进行转化。

首先需将[0,1]转化为0-255,然后将浮点float64转为uint8的。因为代码跑在服务器上没有图像界面,因此需要用visdom做图片的显示。附代码:

import cv2
import visdom
import numpy as np
import matplotlib.pyplot as plt
from skimage.util.dtype import convert
import skimage.util
 
viz = visdom.Visdom(env='img')
img = cv2.imread('qiao.jpg', 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
print(img.dtype)
#img=img.transpose(1,0,2)
# 使用一下函数进行加噪声会导致数据类型从uint8转为float64,而去噪的 函数只能处理8位的uint类型的图片
noise_img = skimage.util.random_noise(img, mode="salt")
# 将加了噪音的[0,1]之间转化为0-255
noise_img = noise_img*255
 
plt.figure(0)
noise_img = noise_img.astype(np.uint8)
viz.image(noise_img.transpose(2, 0, 1))
# denoise = cv2.GaussianBlur(noise_img, (5, 5), 1)
denoise = cv2.GaussianBlur(noise_img, ksize=(3, 3), sigmaX=0)
viz.image(denoise.transpose(2, 0, 1))
denoise_mean = cv2.medianBlur(noise_img, 5)
viz.image(denoise_mean.transpose(2, 0, 1))
denoise_mean = cv2.fastNlMeansDenoising(noise_img,h=30)
viz.text("均值滤波结果")
viz.image(denoise_mean.transpose(2, 0, 1))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch是一个流行的深度学习框架,可以用于图像修复任务。图像修复是指通过填补缺失或损坏的图像区域来恢复图像的完整性。下面是一种常见的PyTorch图像修复方法: 1. 数据准备:首先,需要准备包含有缺失或损坏区域的图像数据集。可以使用现有的数据集,或者自己创建一个。 2. 构建模型:使用PyTorch构建一个适合图像修复任务的深度学习模型。可以选择使用卷积神经网络(CNN)或生成对抗网络(GAN)等模型结构。 3. 数据预处理:对输入图像进行预处理,例如将图像归一化到特定范围、调整图像大小等。 4. 定义损失函数:选择适当的损失函数来衡量修复图像与原始图像之间的差异。常用的损失函数包括均方误差(MSE)和结构相似性指数(SSIM)等。 5. 训练模型:使用训练数据集对模型进行训练。通过反向传播算法和优化器(如Adam或SGD)来更新模型的参数,使其能够更好地恢复缺失或损坏的图像区域。 6. 评估模型:使用测试数据集对训练好的模型进行评估,计算修复图像的质量指标,如PSNR(峰值信噪比)和SSIM。 7. 图像修复:使用训练好的模型对新的图像进行修复。将缺失或损坏的区域作为输入,通过模型生成修复后的图像。 这只是一个简单的介绍,实际上,图像修复是一个复杂的任务,需要根据具体情况进行调整和改进。你可以根据自己的需求和实际情况来选择合适的模型和方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值