批量给图形添加噪声-python代码实现

批量给图形添加噪声-python代码实现,第1张

批量给图形添加高斯噪声和椒盐噪声-python代码实现 高斯噪声
import cv2
import os
import numpy as np

def add_noise_Guass(img, mean=0, var=0.01):  # 添加高斯噪声
    img = np.array(img / 255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, img.shape)
    out_img = img + noise
    if out_img.min() < 0:
        low_clip = -1
    else:
        low_clip = 0
        out_img = np.clip(out_img, low_clip, 1.0)
        out_img = np.uint8(out_img * 255)
    return out_img
# 读取函数,用来读取文件夹中的所有函数,输入参数是文件名
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        print(filename)  # 仅仅是为了测试
        img = cv2.imread(directory_name + "/" + filename)
        #####显示图片#######
        out_img = add_noise_Guass(img)
        cv2.imshow("img", out_img)
        cv2.waitKey(0)
        #####################

        #####保存图片#########
        cv2.imwrite("C:/Users/Dong.Shao2/Desktop/DataSet/gaussian" + "/" + filename, out_img * 255)


read_directory("C:/Users/Dong.Shao2/Desktop/DataSet/image")#这里传入所要读取文件夹的绝对路径,加引号(引号不能省略!)



椒盐噪声
import cv2
import random
import os
import numpy as np

def add_salt_pepper(img, prob):
    resultImg = np.zeros(img.shape, np.uint8)
    thres = 1 - prob
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            rdn = random.random()
            if rdn < prob:
                resultImg[i][j] = 0
            elif rdn > thres:
                resultImg[i][j] = 255
            else:
                resultImg[i][j] = img[i][j]
    return resultImg

# 读取函数,用来读取文件夹中的所有函数,输入参数是文件名
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        print(filename)  # 仅仅是为了测试
        img = cv2.imread(directory_name + "/" + filename)
        #####显示图片#######
        out_img = add_salt_pepper(img,0.05)
        cv2.imshow("img", out_img)
        cv2.waitKey(0)
        #####################

        #####保存图片#########
        cv2.imwrite("C:/Users/Dong.Shao2/Desktop/DataSet/salt_pepper" + "/" + filename, out_img )


read_directory("C:/Users/Dong.Shao2/Desktop/DataSet/image")#这里传入所要读取文件夹的绝对路径,加引号(引号不能省略!)


随机噪声
import cv2
import os
import numpy as np

def random_noise(image,noise_num):
    '''
    添加随机噪点(实际上就是随机在图像上将像素点的灰度值变为255即白色)
    param image: 需要加噪的图片
    param noise_num: 添加的噪音点数目
    return: img_noise
    '''
    # 参数image:,noise_num:
    img_noise = image
    # cv2.imshow("src", img)
    rows, cols, chn = img_noise.shape
    # 加噪声
    for i in range(noise_num):
        x = np.random.randint(0, rows)#随机生成指定范围的整数
        y = np.random.randint(0, cols)
        img_noise[x, y, :] = 255
    return img_noise

# 读取函数,用来读取文件夹中的所有函数,输入参数是文件名
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        print(filename)  # 仅仅是为了测试
        img = cv2.imread(directory_name + "/" + filename)
        #####显示图片#######
        out_img = random_noise(img,10000)
        cv2.imshow("img", out_img)
        cv2.waitKey(0)
        #####################

        #####保存图片#########
        cv2.imwrite("C:/Users/Dong.Shao2/Desktop/DataSet/random"  + "/" + filename, out_img )


read_directory("C:/Users/Dong.Shao2/Desktop/DataSet/image")#这里传入所要读取文件夹的绝对路径,加引号(引号不能省略!)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/732558.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存