原图像:
pymeanshift结果:
bilateralfilter结果:
代码部分:
import numpy as np
import cv2 as cv
def bilateral_filter(image):
# 高斯双边——边缘保留滤波
# bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is
# . very slow compared to most filters.
# 双边过滤可以非常好的处理不需要的图片噪点(瑕点)同时清晰地保留边缘轮廓,缺点在于:高斯双边过滤相较与其它的滤波器运行速度较慢
# 可以实现“磨皮”效果
"""
参数介绍:
src:Source 8-bit or floating-point, 1-channel or 3-channel image.8位或者浮点图片,单通道或者3通道RBG图片都可以
d:Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,
. it is computed from sigmaSpace.核函数直径,可以根据给出的sigmaSpace来计算,也可以直接给定。推荐d值不大于5(否则计算速度慢)
sigmaColor:Filter sigma in the color space.色彩空间标准差,即同化多大“范围”内不同像素的颜色
sigmaSpace:Filter sigma in the coordinate space. A larger value of the parameter means that
. farther pixels will influence each other as long as their colors are close enough (see sigmaColor
. ). When d\>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is
. proportional to sigmaSpace.空间核标准差,值越大则同化范围越大(同化还受到sigmaColor影响)。可以通过制定 d 来计算,也可以直接给定
"""
dst = cv.bilateralFilter(image, d=-1, sigmaColor=50, sigmaSpace=50)
cv.imshow("bilateral_filter", dst)
def mean_shift(image):
# 均值飘逸分割
# 实现类似油画效果
# 本算法是pyramid与meanshift的结合,当maxlevel设置为0时即为普通的meanshift
"""
@param src The source 8-bit, 3-channel image. 输入图片
. @param dst The destination image of the same format and the same size as the source.
. @param sp The spatial window radius. 空间窗口半径
. @param sr The color window radius. 颜色窗口半径
. @param maxLevel Maximum level of the pyramid for the segmentation. 最大迭代次数
. @param termcrit Termination criteria: when to stop meanshift iterations. 退出迭代的判断依据
"""
dst = cv.pyrMeanShiftFiltering(image, 25, 50)
cv.imshow("mean_shift", dst)
src = cv.imread("data/before_filter.jpg")
cv.imshow("before", src)
# t1 = cv.getTickCount()
# bilateral_filter(src)
# t2 = cv.getTickCount()
# time = (t2-t1)/cv.getTickFrequency()*1000
# print("用时为:{}".format(time)+"ms")
mean_shift(src)
cv.waitKey(0)
cv.destroyAllWindows()