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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Sam Chou   2021-10-11 我要评论
想了解OpenCV实现图像滤波之双边滤波的相关内容吗,Sam Chou在本文为您仔细讲解OpenCV双边滤波的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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()

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们