Python_10_OpenCV中填充、模糊操作

2020/08/02

1、ROI(Region of Interest)填充

(1)什么是ROI?

就是被选中的区域

"""
ROI选区操作
"""
face = src[40:200, 90:300]               # 选区
cv.imshow("face", face)
gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY)        # 变成灰色
backface = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)    # 灰色还原
src[40:200, 90:300] = backface                     # 重新放回选区
cv.imshow("face", src)

(2)如何获取?

通过numpy,指定宽高
图像[高度范围,宽度范围]

face = src[40:200, 90:300] 

2、泛洪填充

(1)填充彩色图像

"""
泛洪填充之填充彩色图像
"""
def fill_color_demo(image):
    copyImg = image.copy()
    h, w = image.shape[:2]
    mask = np.zeros([h+2, w+2], np.uint8)
    cv.floodFill(copyImg, mask, (30, 30), (0, 255, 255), (100, 100, 100), (50, 50, 50),cv.FLOODFILL_FIXED_RANGE)
    # (30,30)-(100,100,100)得填充最低的像素,(30,30)+(50,50,50)得填充最高的像素
    cv.imshow("fill_color_demo", copyImg)

(2)R值填充

"""
泛洪填充之R值填充
"""
def fill_binary():
    image = np.zeros([400, 400, 3], np.uint8)       #创建图
    image[100:300, 100:300, :] = 255                #选区
    cv.imshow("fill_binary", image)

    mask = np.ones([402, 402, 1], np.uint8)         #造mask
    mask[101:301, 101:301] = 0
    cv.floodFill(image, mask, (200, 200), (0, 0, 255), cv.flOODFILL_MASK_ONLY)#采用floodFill时,只有mask不为1的时候才会被填充
    cv.imshow("fill_binary", image)

3、模糊操作

基于离散卷积
卷积原理:

(1)均值模糊

①基于平均值的模糊

"""
均值模糊
"""
def blur_demo(image):
    dst = cv.blur(image, (1, 3))#13列
    cv.imshow("blur_demo", dst)

②基于权重的模糊(高斯模糊)★★★★★

自己写高斯模糊
"""
高斯模糊
"""


def clamp(pv):   #clamp函数作用:确保在0-255之间
    if pv > 255:
        return 255
    if pv < 0:
        return 0
    else:
        return pv


def gaussian_noise(image):
    h, w, c = image.shape
    for row in range(h):            # 获取随机数,其实range = range(0,h,1)
        for col in range(w):
            s = np.random.normal(0, 20, 3) # 每次产生三个
            b = image[row, col, 0]   # bule
            g = image[row, col, 1]   # green
            r = image[row, col, 2]   # rad
            image[row, col, 0] = clamp(b + s[0])
            image[row, col, 1] = clamp(g + s[1])
            image[row, col, 2] = clamp(r + s[2])
    cv.imshow("gaussian_noise",image)
利用cv中高斯模糊API
"""
cv中高斯模糊API
"""
dst = cv.GaussianBlur(src,(0,0),15)
cv.imshow("Gaussian Blur",src)

(2)中值模糊

"""
中值模糊
"""
def median_blur_demo(image):
    dst = cv.medianBlur(image, 5)
    cv.imshow("median_blur_demo", dst)

(3)自定义模糊

定义5*5的

"""
自定义定义3*3的模糊
"""
def custom_blur_demo1(image):
    kernel = np.ones([5, 5], np.float32)/25
    dst = cv.filter2D(image, -1, kernel=kernel)  #一般是-1
    cv.imshow("custom_blur_demo", dst)

定义3*3的

"""
自定义定义3*3的模糊
"""
def custom_blur_demo2(image):
    kernel = np.array([1, 1, 1], [1, 1, 1], [1, 1, 1], np.float32)/9
    dst = cv.filter2D(image, -1, kernel=kernel)
    cv.imshow("custom_blur_demo", dst)

(4)锐化

锐化函数要符合的原则:
①基数
②加起来总和等于0,这时一般做边缘的梯度等,总和等于1一些增强等工作,使图像更有立体感

"""
锐化
"""
def custom_blur_demo3(image):
    kernel = np.array([0, -1, 0], [-1, 5, -1], [0, -1, 0], np.float32)
    dst = cv.filter2D(image, -1, kernel=kernel)
    cv.imshow("custom_blur_demo", dst)

(5)意义与应用场合

①均值模糊:去除随机噪深
②高斯模糊:去噪,和均值模糊相比,可以保护图像的特征就是轮廓
③中值模糊:去除椒盐噪深
④锐化:提高图像的对比度,突出细节,使图像更有立体感

4、边缘保留滤波(EPF)

(1)高斯的双边模糊

双线性模糊,相当于美颜滤镜,高斯磨皮,超赞!!!
在这里插入图片描述
上图所示参数中,
d一般为0,让sigmaColo和sigmaSpacer反算d;
sigmaColor颜色差异一般取值大一些,目的是让小的差异模糊掉(去燥深);
sigmaSpace空间差异一般取小一点,目的是使整个和小一点,把主要的差异保留出来

"""
高斯的双边模糊
"""
def bi_demo(image):# 高斯的双边模糊
    dst = cv.bilateralFilter(image, 0, 100, 15)
    cv.imshow("bi_demo", dst)

(2)均值迁移模糊

处理完像油画

"""
均值迁移模糊
"""


def shift_demo(image):#均值迁移模糊,处理完像油画
    dst = cv.pyrMeanShiftFiltering(image, 10, 50)
    cv.imshow("shift_demo", dst)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值