iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >OpenCV实现图像滤波之双边滤波
  • 682
分享到

OpenCV实现图像滤波之双边滤波

2024-04-02 19:04:59 682人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

本文实例为大家分享了OpenCV实现双边滤波的具体代码,供大家参考,具体内容如下 1、2D卷积 #!/usr/bin/env python3 # -*- coding: utf-

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: OpenCV实现图像滤波之双边滤波

本文链接: https://www.lsjlt.com/news/137718.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作