`

opencv_python学习笔记十三

 
阅读更多

16 图像平滑

通过低通滤波器对图像进行模糊

通过自定义滤波器对图像进行卷积

def filter2D(src, #输入图像

ddepth, #图像深度

kernel, #卷积核,单通道浮点矩阵

dst=None, #输出图像

anchor=None, #一个被滤波的点在核内的位置(中心)

delta=None,

borderType=None)#边界类型

def ones(shape, #数组的形状

dtype=None, #数组的数据类型

order='C'):#数组元素在内存中的排列方式,c表示c语言的
a= empty(shape, dtype, order)
multiarray.copyto(a, 1, casting='unsafe')
return a

示例代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2016/11/17 10:54
# @Author : Retacn
# @Site : 图像平滑 卷积
# @File : imageFilter.py
# @Software: PyCharm

import cv2
import numpy as np
from matplotlib import pyplot as plt

img=cv2.imread('test.jpg')
kernel=np.ones((5,5),#数组形状
np.float32)/25#数组的数据类型

#print(kernel)
dst=cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]),plt.yticks([])

plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]),plt.yticks([])

plt.show()

图像模糊(图像平滑)

使用低通滤波器可以实现图像模糊

1 平均

函数原型

def blur(src, #源图像

ksize, #内核大小

dst=None, #输出图像

anchor=None, #中心锚点

borderType=None)# 边界模式

2 高斯模糊

函数原型

def GaussianBlur(src, #输入图像

ksize, #高斯滤波模版大小

sigmaX, #横向滤波系数

dst=None, #输出图像

sigmaY=None,#纵向滤波系数

borderType=None)

3 中值模糊

def medianBlur(src, #源图像

ksize, #中值滤波器的模版的大小

dst=None)#输出图像

4 双边滤波

def bilateralFilter(src, #输入图像

d, #每个像素邻域的直径

sigmaColor, #颜色空间的标准偏差

sigmaSpace, #坐标空间的标准偏差

dst=None, #输出图像

borderType=None)#边缘点插值类型

示例代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2016/11/17 13:14
# @Author : Retacn
# @Site : 平均,对图像进行均值滤波
# @File : imageBlur.py
# @Software: PyCharm

import cv2
import numpy as np
from matplotlib import pyplot as plt

#读入图像
img=cv2.imread("../test.jpg")
#均值滤波
#blur=cv2.blur(img,(5,5))

#高斯模糊
#blur=cv2.GaussianBlur(img,(5,5),0)

#中值模糊
#blur=cv2.medianBlur(img,5)

#双边滤波
blur=cv2.bilateralFilter(img,9,75,75)

#原图像
plt.subplot(121),plt.imshow(img),plt.title("Original")
plt.xticks([]),plt.yticks([])

#平均后
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]),plt.yticks([])
plt.show()

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics