OpenCV实现图像滤波之双边滤波
作者:Sam Chou 时间:2022-05-14 07:35:30
本文实例为大家分享了opencv实现双边滤波的具体代码,供大家参考,具体内容如下
1、2D卷积
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
使用自定义卷积核进行图像2D卷积操作
函数原型:
filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst
函数返回值:dst:2d卷积操作后的结果
函数解析:
ddepth:指定输出图像深度,-1表示与src深度保持一致
kernel:卷积内核大小, 需大于零,可以不同,如核大小(4,5)
anchor:锚点;默认值Point(-1,-1)表示锚位于内核中央
delta:在将它们存储在dst中之前,将delta可选值添加到已过滤的像素中,默认为None
borderType:边框模式用于图像外部的像素, 默认边缘像素拷贝
"""
import cv2 as cv
import numpy as np
img = cv.imread('./test.png')
# 自定义的一些卷积核
kernel = np.ones((5, 5), np.float32) / 25
kernel_user_1 = np.array([[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]]) / 9
kernel_user_2 = np.array([[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 1]]) / 9
kernel_user_3 = np.array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]) / 9
kernel_user_4 = np.array([[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]]) / 16
dst = cv.filter2D(img, -1, kernel)
dst1 = cv.filter2D(img, -1, kernel_user_1)
dst2 = cv.filter2D(img, -1, kernel_user_2)
dst3 = cv.filter2D(img, -1, kernel_user_3)
dst4 = cv.filter2D(img, -1, kernel_user_4)
h1 = np.hstack((img, dst, dst1))
h2 = np.hstack((dst2, dst3, dst4))
cv.imshow('show', np.vstack((h1, h2)))
cv.waitKey(0)
cv.destroyAllWindows()
# 理解提高
small = np.array(range(10, 55, 5), np.uint8).reshape(3, -1)
print(small)
print('*' * 60)
small_filter = cv.filter2D(small, -1, (np.ones((3, 3), np.float32) / (3 * 3)))
print(small_filter)
2、双边滤波
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
双边滤波器可以很好的保存图像边缘细节并滤除掉低频分量的噪音,
但是双边滤波器的效率不是太高,花费的时间相较于其他滤波器而言也比较长。
函数原型:
bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) -> dst
重点参数解析:
d:表示在过滤过程中每个像素邻域的直径范围。如果该值是非正数,则将由sigmaSpace计算
sigmaColor:颜色空间过滤器的sigma值,值越大表示有越宽广的颜色混合到一起
sigmaSpace: 坐标空间中滤波器的sigma值,如果该值较大,则意味着越远的像素将相互影响
borderType:边框模式用于图像外部的像素, 默认边缘像素拷贝
"""
import cv2 as cv
import numpy as np
# img_path = './images/Fig4.11(a).jpg'
# img_path = './images/Fig5.08(b).jpg'
# img_path = './images/Fig0519(a)(florida_satellite_original).tif'
img_path = 'noisy2.png'
img = cv.imread(img_path)
def nothing(x):
pass
cv.namedWindow('image')
# 创建滑动条
cv.createTrackbar('d', 'image', 0, 100, nothing)
cv.createTrackbar('sigmaColor', 'image', 0, 200, nothing)
cv.createTrackbar('sigmaSpace', 'image', 0, 200, nothing)
cv.imshow('img', img)
cv.imshow('image', img)
while True:
k = cv.waitKey(25) & 0XFF
if chr(k) == 'q':
break
if chr(k) == 'k':
d = cv.getTrackbarPos('d', 'image')
sigmaColor = cv.getTrackbarPos('sigmaColor', 'image')
sigmaSpace = cv.getTrackbarPos('sigmaSpace', 'image')
b_filter = cv.bilateralFilter(img, d, sigmaColor, sigmaSpace)
ret, thresh = cv.threshold(b_filter, 127, 255, cv.THRESH_BINARY)
sava_name = ''.join(('outputs/', 'b_filter', str(d), '_', str(sigmaColor), '_', str(sigmaColor)))
cv.imshow('image', np.hstack((b_filter, thresh)))
cv.imwrite(sava_name + '.jpg', b_filter)
cv.imwrite(sava_name + '_thr.jpg', thresh)
cv.destroyAllWindows()
来源:https://blog.csdn.net/weixin_45602979/article/details/108667286
标签:opencv,双边滤波,图像滤波
0
投稿
猜你喜欢
Python opencv缺陷检测的实现及问题解决
2023-03-10 20:41:55
利用打码兔和超人打码自封装的打码类分享
2023-03-13 21:44:40
Python流程控制if条件选择与for循环
2022-04-03 21:09:52
SQL中distinct的用法(四种示例分析)
2024-01-15 17:18:40
Mysql中批量替换某个字段的部分数据(推荐)
2024-01-14 19:56:04
python中的turtle库函数简单使用教程
2022-08-06 23:22:18
Bootstrap modal使用及点击外部不消失的解决方法
2024-04-10 13:56:45
MySQL 连接与管理
2011-09-10 15:52:42
什么是Python变量作用域
2021-12-25 21:36:24
Vue Router 实现动态路由和常见问题及解决方法
2024-05-05 09:09:43
Pandas数据集的分块读取的实现
2021-02-03 07:52:24
分享vim python缩进等一些配置
2022-09-28 00:12:55
Python封装zabbix-get接口的代码分享
2021-12-05 08:57:39
一文学会使用OpenCV构建文档扫描仪
2023-08-02 12:35:05
在程序中压缩sql server2000的数据库备份文件的代码
2024-01-23 23:01:35
PDO::getAttribute讲解
2023-06-06 02:58:22
Python快速实现一键抠图功能的全过程
2021-03-03 14:58:39
用js实现小球的自由移动代码
2023-08-23 02:57:29
三十分钟MySQL快速入门(图解)
2024-01-21 21:49:32
python二叉树遍历的实现方法
2021-09-19 03:53:14