常州机器视觉培训

常州上位机软件开发

常州工业机器人编程设计培训

常州PLC培训

常州PLC

常州PLC编程培训

常州电工培训

常州和讯plc培训中心欢迎您!
热门课程
联系方式
  • 常州和讯自动化培训中心
  • 常州市新北区府琛商务广场2号楼1409室
  • 电话:0519-85602926
  • 手机:15861139266 13401342299
当前位置:网站首页 > 新闻中心 新闻中心
基于openCV给图片加点噪点-常州机器视觉学习,常州上位机学习
日期:2024-3-21 17:02:38人气:  标签:常州机器视觉学习 常州上位机学习

一、为输入图像添加盐噪声(白色噪点)


def salt_noise(src, percetage):

    NoiseImg = copy(src)

    NoiseNum = int(percetage * src.shape1710915557.png * src.shape1710915557.png)

    for i in range(NoiseNum):

        randX = random.random_integers(0, src.shape1710915557.png - 1)

        randY = random.random_integers(0, src.shape1710915557.png - 1)

        NoiseImg[randX, randY] = 255

    return NoiseImg

这个函数 salt_noise(src, percentage) 的作用是向输入的图像 src 中添加盐噪声。具体来说,它会在图像中随机选取一定比例的像素点,并将这些像素点的数值设置为255(白色),从而模拟盐噪声的效果。

函数参数解释:

src: 输入的原始图像,即待添加噪声的图像。

percentage: 添加盐噪声的比例,即在图像中被修改为白色的像素点占总像素点数量的比例。

函数实现步骤:

复制输入的原始图像,以免修改原始图像。

计算需要添加盐噪声的像素点数量 NoiseNum,该数量由输入的百分比 percentage 与图像总像素点数量相乘得到。

使用循环遍历 NoiseNum 次,每次在图像中随机选择一个像素点,并将其像素值设为255(白色),从而添加盐噪声。

返回添加噪声后的图像 NoiseImg。


二、椒盐噪声


def pepper_salt_noise(src, percetage):

    NoiseImg = copy(src)

    NoiseNum = int(percetage * src.shape1710915557.png * src.shape1710915557.png)

    for i in range(NoiseNum):

        randX = random.random_integers(0, src.shape1710915557.png - 1)

        randY = random.random_integers(0, src.shape1710915557.png - 1)

        if random.random_integers(0, 1) <= 0.9:

            NoiseImg[randX, randY] = 0

        else:

            NoiseImg[randX, randY] = 255

    return NoiseImg



pepper_salt_noise(src, percentage) 的作用是向输入的图像 src 中添加椒盐噪声。椒盐噪声是一种同时包含盐(白色)和胡椒(黑色)噪声的图像噪声模型。


函数参数解释:

src: 输入的原始图像,即待添加噪声的图像。


percentage: 添加椒盐噪声的比例,即在图像中被修改为盐或胡椒颜色的像素点占总像素点数量的比例。



函数实现步骤:

复制输入的原始图像,以免修改原始图像。


计算需要添加椒盐噪声的像素点数量 NoiseNum,该数量由输入的百分比 percentage 与图像总像素点数量相乘得到。


使用循环遍历 NoiseNum 次,每次在图像中随机选择一个像素点,并根据概率决定将其像素值设为0(黑色)或255(白色),从而模拟椒盐噪声。


返回添加噪声后的图像 NoiseImg。


三、高斯噪声


def gasuss_noise(image, mean=0, var=0.001):

    image = np.array(image / 255, dtype=float)

    noise = np.random.normal(mean, var ** 0.5, image.shape)

    out = image + noise

    if out.min() < 0:

        low_clip = -1.

    else:

        low_clip = 0.

    out = np.clip(out, low_clip, 1.0)

    out = np.uint8(out * 255)

    return out



这段代码定义了一个名为 gauss_noise(image, mean=0, var=0.001) 的函数,用于向输入的图像添加高斯噪声。高斯噪声是一种常见的噪声类型,符合高斯分布。


函数参数解释:


image: 输入的原始图像。


mean: 高斯噪声的均值,默认为0。


var: 高斯噪声的方差,默认为0.001。


函数实现步骤

将输入的图像转换为浮点数数组,范围从0到1。


使用 np.random.normal() 生成均值为 mean,方差为 var 的高斯噪声数组,与原始图像形状相同。


将生成的高斯噪声数组与原始图像相加,得到带有噪声的图像。


对输出进行裁剪,确保像素值范围在 [0, 1] 之间。


将处理后的图像乘以255并转换为无符号8位整数,以便正确显示图像。



import cv2

import random

import numpy as np

from numpy import *

from matplotlib import pyplot as plt

from copy import copy


def gasuss_noise(image, mean=0, var=0.001):

    image = np.array(image / 255, dtype=float)

    noise = np.random.normal(mean, var ** 0.5, image.shape)

    out = image + noise

    if out.min() < 0:

        low_clip = -1.

    else:

        low_clip = 0.

    out = np.clip(out, low_clip, 1.0)

    out = np.uint8(out * 255)

    return out


def salt_noise(src, percetage):

    NoiseImg = copy(src)

    NoiseNum = int(percetage * src.shape1710915557.png * src.shape1710915557.png)

    for i in range(NoiseNum):

        randX = random.random_integers(0, src.shape1710915557.png - 1)

        randY = random.random_integers(0, src.shape1710915557.png - 1)

        NoiseImg[randX, randY] = 255

    return NoiseImg


def pepper_salt_noise(src, percetage):

    NoiseImg = copy(src)

    NoiseNum = int(percetage * src.shape1710915557.png * src.shape1710915557.png)

    for i in range(NoiseNum):

        randX = random.random_integers(0, src.shape1710915557.png - 1)

        randY = random.random_integers(0, src.shape1710915557.png - 1)

        if random.random_integers(0, 1) <= 0.9:

            NoiseImg[randX, randY] = 0

        else:

            NoiseImg[randX, randY] = 255

    return NoiseImg


#

img = cv2.imread('../data/0001.jpg', 0)

img1 = gasuss_noise(img, 0.1, 0.02)

img2 = salt_noise(img, 0.1)

img3 = pepper_salt_noise(img, 0.2)


plt.subplot(221)

plt.imshow(img, cmap='gray')

plt.title('img')

plt.axis('off')

plt.subplot(222)

plt.imshow(img1, cmap='gray')

plt.title('img_gasuss_noise')

plt.axis('off')

plt.subplot(223)

plt.imshow(img2, cmap='gray')

plt.title('salt_noise')

plt.axis('off')

plt.subplot(224)

plt.imshow(img3, cmap='gray')

plt.title('pepper_salt_noise')

plt.axis('off')

plt.show()

1710915557.png

本文网址:
下一篇:没有资料

相关信息:
版权所有 CopyRight 2006-2017 江苏和讯自动化设备有限公司 常州自动化培训中心 电话:0519-85602926 地址:常州市新北区府琛商务广场2号楼1409室
苏ICP备14016686号-2 技术支持:常州山水网络
本站关键词:常州PLC培训 常州PLC编程培训 常州PLC编程 常州PLC培训班 网站地图 网站标签
在线与我们取得联系