当前位置:网站首页>往图片添加椒盐噪声或高斯噪声
往图片添加椒盐噪声或高斯噪声
2022-07-06 23:59:00 【瀛台夜雪】
往图片添加椒盐噪声或高斯噪声
椒盐噪声添加
椒盐噪声(salt & pepper noise)是数字图像的一个常见噪声,所谓椒盐,椒就是黑,盐就是白,椒盐噪声就是在图像上随机出现黑色白色的像素。椒盐噪声是一种因为信号脉冲强度引起的噪声,产生该噪声的算法也比较简单。
往灰度图添加椒盐噪声
#往灰度图像添加椒盐噪声
# src 输入图像的路径
# dst 输出图像的路径
# prob 噪声的占比
def addSaltPepperNoise_gray(src,dst,probility=0.05,method="salt_pepper"):
#将图片转换成灰度图
imarray=np.array(Image.open(src).convert('L'))
height,width=imarray.shape
for i in range(height):
for j in range(width):
#随机添加椒盐噪声
if np.random.random(1)<probility:
if np.random.random(1)<0.05:
imarray[i,j]=0
else:
imarray[i,j]=255
#将生成的椒盐噪声图像添加到
new_im=Image.fromarray(imarray)
new_im.save(dst)
return new_im
往彩色图添加椒盐噪声
def RGBAddNoise(src, dst, prob): # 同时加杂乱(RGB单噪声)RGB图噪声 prob:噪声占比
imarray = np.array(Image.open(src))
height, width, channels = imarray.shape
# prob = 0.05 #噪声占比 已经比较明显了 >0.1 严重影响画质
NoiseImg = imarray.copy()
NoiseNum = int(prob * height * width)
for i in range(NoiseNum):
rows = np.random.randint(0, height - 1)
cols = np.random.randint(0, width - 1)
channel = np.random.randint(0, 3)
if np.random.randint(0, 2) == 0:
NoiseImg[rows, cols, channel] = 0
else:
NoiseImg[rows, cols, channel] = 255
# return NoiseImg
new_im = Image.fromarray(NoiseImg)
new_im.save(dst)
return new_im
高斯噪声添加
高斯噪声是指噪声分布的概率密度函数服从高斯分布(正态分布)的一类噪声,其产生的主要原因是由于相机在拍摄时视场较暗且亮度不均匀造成的,同时相机长时间工作使得温度过高也会引起高斯噪声,另外电路元器件自身噪声和互相影响也是造成高斯噪声的重要原因之一。
往灰度图添加高斯噪声
def addGaussNoiseGray(src,dst,mean,sigma):
image=cv2.imread(src,cv2.IMREAD_GRAYSCALE)
height,width=image.shape
gauss=np.random.normal(mean,sigma,image.shape)
gauss=gauss.reshape(height,width)
noiseImage=image+gauss
noiseImage=np.clip(noiseImage,0,255)
noiseImage=noiseImage.astype('uint8')
cv2.imwrite(dst,noiseImage)
return noiseImage
往彩色图添加高斯噪声
#彩色图像中添加高斯噪声
def addGaussNoiseRgb(src,dst,mean,sigma):
image=cv2.imread(src)
height,width,channels=image.shape
#建立高斯噪声
gauss=np.random.normal(mean,sigma,(height,width,channels))
gauss=gauss.reshape(height,width,channels)
noiseImage=image+gauss
noiseImage=np.clip(noiseImage,0,255)
noiseImage=noiseImage.astype('uint8')
cv2.imwrite(dst,noiseImage)
return noiseImage
结果展示

边栏推荐
- [论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
- Five core elements of architecture design
- What are the common message queues?
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
- K6el-100 leakage relay
- 论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
- Educational Codeforces Round 22 B. The Golden Age
- Flinksql 读写pgsql
- 一条 update 语句的生命经历
猜你喜欢

《2022中国低/无代码市场研究及选型评估报告》发布
![[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training](/img/d6/e6db0d76e81e49a83a30f8c1832f09.png)
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training

SAP ABAP BDC(批量数据通信)-018

bat 批示处理详解

三级菜单数据实现,实现嵌套三级菜单数据

LabVIEW is opening a new reference, indicating that the memory is full

Message queue: how to deal with message backlog?

论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】

Differences and introduction of cluster, distributed and microservice

Dj-zbs2 leakage relay
随机推荐
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
DOM node object + time node comprehensive case
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
C nullable type
CVE-2021-3156 漏洞复现笔记
Hcip eighth operation
什么是消息队列?
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
5阶多项式轨迹
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
Modes of optical fiber - single mode and multimode
English语法_名词 - 所有格
Five core elements of architecture design
Unity keeps the camera behind and above the player
架构设计的五个核心要素
JSP setting header information export to excel
TabLayout修改自定义的Tab标题不生效问题
Preliminary practice of niuke.com (9)
【oracle】简单的日期时间的格式化与排序问题
论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】