赞
踩
本文主要学习资源《机器学习实践指南》案例应用解析
Python可以使用滤波算法实现图像平滑, 是图像增强的一部分。图像平滑是一种区域增强的算法,平滑算法有邻域平均法、中指滤波、边界保持类滤波等,其目的有模糊、削除噪音两种。
滤波的本义是指信号有各种频率的成分,滤掉不想要的成分,即为滤掉常说的噪声,留下想要的成分.这即是滤波的过程,也是目的.
摘自《数字图像处理》
1. 高斯噪声滤波
说明
高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。
高斯滤波(Gauss filter)实质上是一种信号的滤波器,其用途为信号的平滑处理,数字图像用于后期应用,其噪声是最大的问题,因为误差会累计传递等原因,大多图像处理教材会在很早的时候介绍Gauss滤波器,用于得到信噪比SNR较高的图像(反应真实信号)。高斯平滑滤波器对于抑制服从正态分布的噪声非常有效。 (百度百科)
OpenCV提供的blur函数可以进行归一化块滤波操作:
cv2.blur(src,ksize[, dst[, anchor[, borderType[]]]) -> dst
此外,该函数使用了如下脉冲响应函数(核函数):
测试代码
# -*- coding: utf-8 -*-# coding=utf-8import cv2import numpy as npfn = "test.jpg"myimg = cv2.imread(fn)img = cv2.cvtColor(myimg, cv2.COLOR_BGR2GRAY)# 加上高斯噪声param=20# 灰阶范围grayscale = 256w = img.shape[1]h = img.shape[0]newimg = np.zeros((h,w),np.uint8)for x in range(0, h): for y in range(0, w-1, 2): r1 = np.random.random_sample() r2 = np.random.random_sample() z1 = param*np.cos(2*np.pi*r2)*np.sqrt((-2)*np.log(r1)) z2 = param*np.sin(2*np.pi*r2)*np.sqrt((-2)*np.log(r1)) fxy = int(img[x, y]+z1) fxy1 = int(img[x, y+1]+z2) #f(x,y) if fxy<0: fxy_val=0 elif fxy>grayscale-1: fxy_val=grayscale-1 else: fxy_val=fxy #f(x,y+1) if fxy1<0: fxy1_val=0 elif fxy1>grayscale-1: fxy1_val=grayscale-1 else: fxy1_val=fxy1 newimg[x,y]=fxy_val newimg[x,y+1]=fxy1_val# 滤波去噪lbimg = cv2.blur(newimg, (3, 3))cv2.imshow('src', newimg)cv2.imshow('dst', lbimg)cv2.waitKey()cv2.destroyAllWindows()
运行结果
如果使用下面脉冲响应函数:
程序代码:
# 滤波去噪# 图像四个边的像素处理lbimg=np.zeros((h+2,w+2),np.float32)tmpimg=np.zeros((h+2,w+2))myh=h+2myw=w+2tmpimg[1:myh-1,1:myw-1]=newimg[0:myh,0:myw]# 用第3个脉冲响应函数a = 1/16.0kernel=a*np.array([[1,2,1],[2,4,2],[1,2,1]])for y in range(1,myh-1): for x in range(1,myw-1): lbimg[y,x]=np.sum(kernel*tmpimg[y-1:y+2,x-1:x+2]) print(".")resultimg=np.array(lbimg[1:myh-1,1:myw-1],np.uint8)cv2.imshow('src',newimg)cv2.imshow('dst',resultimg)cv2.waitKey()cv2.destroyAllWindows()
2. 椒盐噪声滤波
代码
# -*- coding: utf-8 -*-# coding=utf-8import cv2import numpy as npfn = "test.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 = 100000for k in range(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()
运行效果:
3. OpenCV 2d滤波器 cv2.filter2D()
使用自定义内核对图像进行卷积。该功能将任意线性滤波器应用于图像。
对于2D图像可以进行低通或者高通滤波操作,低通滤波(LPF)有利于去噪,模糊图像,高通滤波(HPF)有利于找到图像边界。
# -*- coding: utf-8 -*-import cv2import numpy as npimg = cv2.imread('test.jpg')kernel = np.ones((5, 5), np.float32) / 25dst = cv2.filter2D(img, -1, kernel)cv2.imshow('dst', dst)cv2.imshow('source', img)cv2.waitKey(0)cv2.destroyAllWindows()
运行效果:
邻域平均法可有效消除高斯噪声,其数学公式如下:
S为邻域,不包括(x,y)(x,y)本身的像素点,核h(x,y)h(x,y)可为:
1. 邻域平均法对椒盐噪声滤波进行处理的操作
代码
# -*- coding: utf-8 -*-# coding=utf-8import cv2import numpy as npfn = "test.jpg"myimg = cv2.imread(fn)img = cv2.cvtColor(myimg, cv2.COLOR_BGR2GRAY)# 加上椒盐噪声param = 20# 灰阶范围w = img.shape[1]h = img.shape[0]newimg = np.array(img)# 噪声点数量noisecount = 100000for k in range(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 = np.zeros((h + 2, w + 2), np.float32)tmpimg = np.zeros((h + 2, w + 2))myh = h + 2myw = w + 2tmpimg[1:myh - 1, 1:myw - 1] = newimg[0:myh, 0:myw]# 用领域平均法的(设半径为2)脉冲响应函数a = 1 / 8.0kernel = a * np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])for y in range(1, myh - 1): for x in range(1, myw - 1): lbimg[y, x] = np.sum(kernel * tmpimg[y - 1:y + 2, x - 1:x + 2]) print(".")resultimg = np.array(lbimg[1:myh - 1, 1:myw - 1], np.uint8)cv2.imshow('src', newimg)cv2.imshow('dst', resultimg)cv2.waitKey()cv2.destroyAllWindows()
运行效果
2. 邻域平均法对高斯噪声滤波进行处理的操作
代码
# -*- coding: utf-8 -*-# coding=utf-8import cv2import numpy as npfn = "test.jpg"myimg = cv2.imread(fn)img = cv2.cvtColor(myimg, cv2.COLOR_BGR2GRAY)# 加上高斯噪声param = 20# 灰阶范围grayscale = 256w = img.shape[1]h = img.shape[0]newimg = np.zeros((h, w), np.uint8)# 加上高斯噪声param = 20# 灰阶范围grayscale = 256w = img.shape[1]h = img.shape[0]newimg = np.zeros((h, w), np.uint8)for x in range(0, h): for y in range(0, w, 2): r1 = np.random.random_sample() r2 = np.random.random_sample() z1 = param * np.cos(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1)) z2 = param * np.sin(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1)) fxy = int(img[x, y] + z1) fxy1 = int(img[x, y + 1] + z2) # f(x,y) if fxy < 0: fxy_val = 0 elif fxy > grayscale - 1: fxy_val = grayscale - 1 else: fxy_val = fxy # f(x,y+1) if fxy1 < 0: fxy1_val = 0 elif fxy1 > grayscale - 1: fxy1_val = grayscale - 1 else: fxy1_val = fxy1 newimg[x, y] = fxy_val newimg[x, y + 1] = fxy1_val print("-")# 邻域平均法去噪# 脉冲响应函数,核函数# 图像四个边的像素处理lbimg = np.zeros((h + 2, w + 2), np.float32)tmpimg = np.zeros((h + 2, w + 2))myh = h + 2myw = w + 2tmpimg[1:myh - 1, 1:myw - 1] = newimg[0:myh, 0:myw]# 用领域平均法的(设半径为2)脉冲响应函数a = 1 / 8.0kernel = a * np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])for y in range(1, myh - 1): for x in range(1, myw - 1): lbimg[y, x] = np.sum(kernel * tmpimg[y - 1:y + 2, x - 1:x + 2]) print(".")resultimg = np.array(lbimg[1:myh - 1, 1:myw - 1], np.uint8)cv2.imshow('src', newimg)cv2.imshow('dst', resultimg)cv2.waitKey()cv2.destroyAllWindows()
运行结果
中值滤波与邻域平均法类似,但计算的是中值,而不是平均值。具体算法是:将图像的每个像素用邻域(以当前像素为中心的正方形区域)像素的中值来代替。
1. 中值滤波对椒盐噪声除噪
代码
# -*- coding: utf-8 -*-# 中值滤波import cv2import numpy as npfn = "test.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 = 50000for k in range(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 = np.zeros((h + 2, w + 2), np.float32)tmpimg = np.zeros((h + 2, w + 2))myh = h + 2myw = w + 2tmpimg[1:myh - 1, 1:myw - 1] = newimg[0:myh, 0:myw]# 用中值法for y in range(1, myh - 1): for x in range(1, myw - 1): lbimg[y, x] = np.median(tmpimg[y - 1:y + 2, x - 1:x + 2]) print(".")resultimg = np.array(lbimg[1:myh - 1, 1:myw - 1], np.uint8)cv2.imshow('src', newimg)cv2.imshow('dst', resultimg)cv2.waitKey()cv2.destroyAllWindows()
运行结果
OpenCv2有medianBlur函数实现中值滤波:
cv2.medianBlur(src, ksize[, dst]) -> dst
代码:
# -*- coding: utf-8 -*-# code:# 中值滤波import cv2import numpy as npfn = "test.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 = 50000for k in range(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.medianBlur(newimg, 3)cv2.imshow('src', newimg)cv2.imshow('dst', lbimg)cv2.waitKey()cv2.destroyAllWindows()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。