脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - OpenCV实现图像滤波之双边滤波

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

2022-01-21 00:31Sam Chou Python

这篇文章主要为大家详细介绍了OpenCV实现图像滤波之双边滤波,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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()

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

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

原文链接:https://blog.csdn.net/weixin_45602979/article/details/108667286

延伸 · 阅读

精彩推荐