使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)


Posted in Python onMay 28, 2019

在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声。Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现。

代码:

import numpy as np
import random
import cv2

def sp_noise(image,prob):
  '''
  添加椒盐噪声
  prob:噪声比例 
  '''
  output = np.zeros(image.shape,np.uint8)
  thres = 1 - prob 
  for i in range(image.shape[0]):
    for j in range(image.shape[1]):
      rdn = random.random()
      if rdn < prob:
        output[i][j] = 0
      elif rdn > thres:
        output[i][j] = 255
      else:
        output[i][j] = image[i][j]
  return output


def gasuss_noise(image, mean=0, var=0.001):
  ''' 
    添加高斯噪声
    mean : 均值 
    var : 方差
  '''
  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)
  #cv.imshow("gasuss", out)
  return out

可见,只要我们得到满足某个分布的多维数组,就能作为噪声添加到图片中。

例如:

import cv2
import numpy as np

>>> im = np.empty((5,5), np.uint8) # needs preallocated input image
>>> im
array([[248, 168, 58,  2,  1], # uninitialized memory counts as random, too ? fun ;) 
    [ 0, 100,  2,  0, 101],
    [ 0,  0, 106,  2,  0],
    [131,  2,  0, 90,  3],
    [ 0, 100,  1,  0, 83]], dtype=uint8)
>>> im = np.zeros((5,5), np.uint8) # seriously now.
>>> im
array([[0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]], dtype=uint8)
>>> cv2.randn(im,(0),(99))     # normal
array([[ 0, 76,  0, 129,  0],
    [ 0,  0,  0, 188, 27],
    [ 0, 152,  0,  0,  0],
    [ 0,  0, 134, 79,  0],
    [ 0, 181, 36, 128,  0]], dtype=uint8)
>>> cv2.randu(im,(0),(99))     # uniform
array([[19, 53, 2, 86, 82],
    [86, 73, 40, 64, 78],
    [34, 20, 62, 80, 7],
    [24, 92, 37, 60, 72],
    [40, 12, 27, 33, 18]], dtype=uint8)

然后再:

img = ...
noise = ...

image = img + noise

参考链接:

1、https://stackoverflow.com/questions/22937589/how-to-add-noise-gaussian-salt-and-pepper-etc-to-image-in-python-with-opencv#

2、https://stackoverflow.com/questions/14435632/impulse-gaussian-and-salt-and-pepper-noise-with-opencv#

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
布同自制Python函数帮助查询小工具
Mar 13 Python
用Python写的图片蜘蛛人代码
Aug 27 Python
Python Tkinter基础控件用法
Sep 03 Python
Python中input和raw_input的一点区别
Oct 21 Python
matplotlib subplots 设置总图的标题方法
May 25 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
使用Python控制摄像头拍照并发邮件
Apr 23 Python
Python容器使用的5个技巧和2个误区总结
Sep 26 Python
解决pycharm不能自动补全第三方库的函数和属性问题
Mar 12 Python
Python持续监听文件变化代码实例
Jul 22 Python
基于python实现百度语音识别和图灵对话
Nov 02 Python
Python3中对json格式数据的分析处理
Jan 28 Python
Python学习笔记之变量、自定义函数用法示例
May 28 #Python
Python分布式进程中你会遇到的问题解析
May 28 #Python
Python增强赋值和共享引用注意事项小结
May 28 #Python
Django框架用户注销功能实现方法分析
May 28 #Python
Django框架首页和登录页分离操作示例
May 28 #Python
Django框架封装外部函数示例
May 28 #Python
详解Numpy数组转置的三种方法T、transpose、swapaxes
May 27 #Python
You might like
php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法
2015/05/25 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
2016/02/23 PHP
利用PHP生成静态html页面的原理
2016/09/30 PHP
PHP面向对象程序设计之类与反射API详解
2016/12/02 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
php实现简单的权限管理的示例代码
2017/08/25 PHP
Yii2.0实现的批量更新及批量插入功能示例
2019/01/29 PHP
【消息提示组件】,兼容IE6/7&amp;&amp;FF2
2007/09/04 Javascript
Jsonp 跨域的原理以及Jquery的解决方案
2010/05/18 Javascript
ExtJS DOM元素操作经验分享
2013/08/28 Javascript
JS 操作Array数组的方法及属性实例解析
2014/01/08 Javascript
实现React单页应用的方法详解
2016/08/02 Javascript
jquery遍历标签中自定义的属性方法
2016/09/17 Javascript
AngularJS2中一种button切换效果的实现方法(二)
2017/03/27 Javascript
TypeScript基础入门教程之三重斜线指令详解
2018/10/22 Javascript
Electron vue的使用教程图文详解
2019/07/05 Javascript
python导出hive数据表的schema实例代码
2018/01/22 Python
Python redis操作实例分析【连接、管道、发布和订阅等】
2019/05/16 Python
python实现登录密码重置简易操作代码
2019/08/14 Python
thinkphp5 路由分发原理
2021/03/18 PHP
Html5移动端网页端适配(js+rem)
2021/02/03 HTML / CSS
Funko官方商店:源自美国,畅销全球搪胶收藏玩偶
2018/09/15 全球购物
Cotton On香港网站:澳洲时装连锁品牌
2018/11/01 全球购物
商务日语毕业生自荐信范文
2013/11/14 职场文书
毕业生的求职信范文分享
2013/12/04 职场文书
基督教婚礼主持词
2014/03/14 职场文书
读书活动总结
2014/04/28 职场文书
党员反对四风问题思想汇报
2014/09/12 职场文书
2014年社区工作总结
2014/11/18 职场文书
2016年秋季趣味运动会开幕词
2016/03/04 职场文书
fastdfs+nginx集群搭建的实现
2021/03/31 Servers
linux下导入、导出mysql数据库命令的实现方法
2021/05/26 MySQL
15个值得收藏的JavaScript函数
2021/09/15 Javascript
MySQL高级进阶sql语句总结大全
2022/03/16 MySQL
详细聊聊Oracle表碎片对性能有多大的影响
2022/03/19 Oracle
python模拟浏览器 使用selenium进入好友QQ空间并留言
2022/04/12 Python