# -*- coding: utf-8 -*-
#code:myhaspl@myhaspl.com
#归一化块滤波
import cv2
import numpy as np
fn="test3.jpg"
myimg=cv2.imread(fn)
img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上高斯噪声,能够參考曾经博文中的内容
......
......
#滤波去噪
lbimg=cv2.blur(newimg,(3,3))
cv2.imshow('src',newimg)
cv2.imshow('dst',lbimg)
cv2.waitKey()
cv2.destroyAllWindows()

右图是加上噪声,左图是去除噪声后,尽管进行了图像模糊,但仍能比較清晰

依据原理,使用第3个脉冲响应函数(也有人称它为核函数),例如以下:

本博客全部内容是原创,假设转载请注明来源

http://blog.csdn.net/myhaspl/

用python实现这个算法

#code:myhaspl@myhaspl.com
#归一化块滤波
...
...
#用第3个脉冲响应函数
a=1/16.0
kernel=a*np.array([[1,2,1],[2,4,2],[1,2,1]])
for y in xrange(1,myh-1):
for x in xrange(1,myw-1):
lbimg[y,x]=np.sum(kernel*tmpimg[y-1:y+2,x-1:x+2])
print ".",

效果例如以下图

opencv提供的blur函数使用说明例如以下 :

Blurs an image using the normalized box filter.

C++: void blur(InputArray src, OutputArray dst, Size ksize, Pointanchor=Point(-1,-1), int borderType=BORDER_DEFAULT )
Python: cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) → dst
Parameters:
  • src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8UCV_16U,CV_16SCV_32F or CV_64F.
  • dst – output image of the same size and type as src.
  • ksize – blurring kernel size.
  • anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
  • borderType – border mode used to extrapolate pixels outside of the image.

注意,blur函数使用了第1个脉冲响应函数,例如以下:

The function smoothes an image using the kernel:

对椒盐噪声的归一化块滤波滤波,须要将作用域扩大,但会更模糊,但效果更好
# -*- coding: utf-8 -*-
#code:myhaspl@myhaspl.com
#归一化块滤波
import cv2
import numpy as np
fn="test3.jpg"
myimg=cv2.imread(fn)
img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上椒盐噪声
#灰阶范围
w=img.shape[1]
h=img.shape[0]
newimg=np.array(img)
#噪声点数量
noisecount=100000
for k in xrange(0,noisecount):
xi=int(np.random.uniform(0,newimg.shape[1]))
xj=int(np.random.uniform(0,newimg.shape[0]))
newimg[xj,xi]=255 #滤波去噪
lbimg=cv2.blur(newimg,(5,5))
cv2.imshow('src',newimg)
cv2.imshow('dst',lbimg)
cv2.waitKey()
cv2.destroyAllWindows()

本博客全部内容是原创,假设转载请注明来源

http://blog.csdn.net/myhaspl/

数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)的更多相关文章

  1. 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)

    Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...

  2. 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

    拉普拉斯线性滤波,.边缘检測  . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...

  3. 数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波

    拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...

  4. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  5. 数学之路-python计算实战(19)-机器视觉-卷积滤波

    filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...

  6. 数学之路-python计算实战(9)-机器视觉-图像插值仿射

    插值 Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation – interpol ...

  7. 数学之路-python计算实战(13)-机器视觉-图像增强

    指数变换的基本表达式为:y=bc(x-a)-1 当中參数b.c控制曲线的变换形状,參数a控制曲线的位置. 指数变换的作用是扩展图像的高灰度级.压缩低灰度级.能够用于亮度过高的图像 本博客全部内容是原创 ...

  8. 数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

    # -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #邻域平均法滤波,半径为2 import cv2 import numpy as np fn=&qu ...

  9. 数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )

    高斯滤波就是对整幅图像进行加权平均的过程.每个像素点的值,都由其本身和邻域内的其它像素值经过加权平均后得到.高斯滤波的详细操作是:用一个模板(或称卷积.掩模)扫描图像中的每个像素.用模板确定的邻域内像 ...

随机推荐

  1. 第一个processing程序(2016-01-15)

    前几天下载和安装了最新的 processing,今天试一下,哈哈,真是简单之极啊,果然是给非程序员使用的,现在,我也是非程序员了.

  2. [置顶] android 心跳包的分析

    android 心跳的分析 最近在做一个项目中用到了心跳包的机制,其实就是传统的长连接.或许有的人知道消息推送的机制,消息推送也是一种长连接 ,是将数据有服务器端推送到客户端这边从而改变传统的“拉”的 ...

  3. [lua]笔试-按字典序列出指指定的序列的位置

    计算方法: n的阶乘记为f(n), s为输入序列, sub(i)为s的i到n的子序列.A(i)为第i位对应的字母在子序列sub(i)中的字典顺序 N(s) = sum_{1,n} T(i)*(A(i) ...

  4. Qt 错误: 无法运行 release 下的可执行文件

        学习Qt有一点时间了,但之前都是在debug版本下进行编译运行,偶然切换到release版本下,却出现了如下错误: 错误提示:   This application failed to sta ...

  5. 投票系统前台 C#,数据库SQL

    ------------vote.aspx.cs--------------------  private void Page_Load(object sender, System.EventArgs ...

  6. 异步和同步http请求超时机制

    异步超时设置: 例子: Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...

  7. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  8. Physiological Processes of Speech Production--Reading Notes (8)

    Upper Jaw The upper jaw, or the maxilla with the upper teeth, is the structure fixed to the skull, f ...

  9. 【Eclipse】Failed to load JavaHL Library

    1.选择window--->preferences->Team->SVN->SVN接口 2.选择SVNKit (Pure Java) xxxxxx  如下图所示 : 选择之后, ...

  10. week4_motion_of_ball_1(小球运动)——最基本

    # Ball motion with an explicit timer import simplegui # Initialize globals width = 600 height = 600 ...