Python opencv生成模糊图像

在做目标检测由于数据不够想对现有的图像数据进行数据增强,数据增强一般有:旋转、翻转、模糊、光照调整、增加噪声、平移和缩放等方法。由于我需要做到实时目标检测,想增加图像的运动模糊数据(仅个人猜想运动模糊数据可以增强目标检测准确性)。

下面是引用到的模糊处理的方法:

    原文地址:https://www.cnblogs.com/arkenstone/p/8480759.html

1) 运动模糊图像

一般来说,运动模糊的图像都是朝同一方向运动的,那么就可以利用cv2.filter2D函数。

import numpy as np

def motion_blur(image, degree=10, angle=20):
    image = np.array(image)
    
    # 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
    M = cv2.getRotationMatrix2D((degree/2, degree/2), angle, 1)
    motion_blur_kernel = np.diag(np.ones(degree))
    motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
    
    motion_blur_kernel = motion_blur_kernel / degree        
    blurred = cv2.filter2D(image, -1, motion_blur_kernel)
    # convert to uint8
    cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
    blurred = np.array(blurred, dtype=np.uint8)
    return blurred

原图
动态模糊

2) 对焦模糊

opencv提供了GaussianBlur函数(具体参见这里).

image = cv2.GaussianBlur(image, ksize=(degree, degree), sigmaX=0, sigmaY=0)

对焦模糊

3) 噪点

其实就是在每个像素点添加随机扰动:

def gaussian_noise(image, degree=None):
    row, col, ch = image.shape
    mean = 0
    if not degree:
        var = np.random.uniform(0.004, 0.01)
    else:
        var = degree
    sigma = var ** 0.5
    gauss = np.random.normal(mean, sigma, (row, col, ch))
    gauss = gauss.reshape(row, col, ch)
    noisy = image + gauss
    cv2.normalize(noisy, noisy, 0, 255, norm_type=cv2.NORM_MINMAX)
    noisy = np.array(noisy, dtype=np.uint8)
    return noisy

噪点

参考:

猜你喜欢

转载自blog.csdn.net/qq_26535271/article/details/86242086