Python OpenCV实现图像增强操作详解

2022-10-19 18:23:20
目录
创作背景图像亮度增强和降低旋转水平镜像和垂直镜像高斯噪声其它图像增强的方法

创作背景

最近在忙着两个YOLOv7项目,通过看大量的论文,发现很多的相关的论文都会在收集图像后进行图像的增强,本文将使用python中的opencv模块实现常见的图像增强方法。

由于光照角度和天气等不确定因素,导致图像采集的光环境极其复杂;为了提高目标检测模型的泛化能力,本文采用了几种图像增强方法。

图像增强方法包括

    图像亮度增强和降低水平镜像垂直镜像多角度旋转(90°̘,180°̘,270°̘)高斯噪声

    此外,考虑到图像采集设备在图像采集过程中产生的噪声,以及设备或树枝晃动造成的拍摄图像模糊,在图像中加入方差为0.02的高斯噪声,进行运动模糊处理。

    图像亮度增强和降低

    图像亮度。指数字图像中包含色彩的明暗程度,是人眼对物体本身明暗程度的感觉。

    图像亮度调节可以采用最简单的图像处理算法,通过常见的线性运算即完成亮度调节,这里我们让所有的像素点亮度值乘上一个增强系数>

    # 变暗
    def Darker(image,percetage=0.9):
        image_copy = image.copy()
        w = image.shape[1]
        h = image.shape[0]
        #get darker
        for xi in range(0,w):
            for xj in range(0,h):
                image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
                image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
                image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
        return image_copy
    
    # 明亮
    def Brighter(image, percetage=1.1):
        image_copy = image.copy()
        w = image.shape[1]
        h = image.shape[0]
        #get brighter
        for xi in range(0,w):
            for xj in range(0,h):
                image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
                image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
                image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
        return image_copy
    

    旋转

    本文使用opencv中的使用getRotationMatrix2D()>

    # 旋转,R可控制图片放大缩小
    def Rotate(image, angle=15, scale=1):
        w = image.shape[1]
        h = image.shape[0]
        #rotate matrix
        M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
        #rotate
        image = cv2.warpAffine(image,M,(w,h))
        return image
    

    水平镜像和垂直镜像

    图像镜像(水平和垂直镜像)是通过opencv中的使用flip函数实现的,通过以图像的垂直线为中心变换图像的左侧和右侧来实现水平镜像。垂直镜像是通过以图像的水平中心线为中心变换图像的上下侧来实现的。

    # 水平翻转
    def Horizontal(image):
        return cv2.flip(image,1,dst=None)
     
    # 垂直翻转
    def Vertical(image):
        return cv2.flip(image,0,dst=None)
    

    高斯噪声

    本文使用NumPy中的可以产生符合高斯分布(正态分布)的随机数的>

    def gaussian_noise(image, mean=0, var=0.02):
        # 添加高斯噪声
        # mean : 均值
        # var : 方差
        image = np.array(image / 255, dtype=float)
        noise = np.random.normal(mean, var ** 0.5, image.shape)
        out = image + noise
        if out.min() < 0:
            low_clip = -1.
        else:
            low_clip = 0.
        out = np.clip(out, low_clip, 1.0)
        out = np.uint8(out * 255)
        return out
    

    其它图像增强的方法

    # 放大缩小
    def Scale(image, scale):
        return cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)
    # 平移
    def Move(img,x,y):
        img_info=img.shape
        height=img_info[0]
        width=img_info[1]
     
        mat_translation=np.float32([[2,0,x],[0,2,y]])  #变换矩阵:设置平移变换所需的计算矩阵:2行3列
        #[[1,0,20],[0,1,50]]   表示平移变换:其中x表示水平方向上的平移距离,y表示竖直方向上的平移距离。
        dst=cv2.warpAffine(img,mat_translation,(width,height))  #变换函数
    # 椒盐噪声
    def SaltAndPepper(src,percetage=0.05):
        SP_NoiseImg=src.copy()
        SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
        for i in range(SP_NoiseNum):
            randR=np.random.randint(0,src.shape[0]-1)
            randG=np.random.randint(0,src.shape[1]-1)
            randB=np.random.randint(0,3)
            if np.random.randint(0,1)==0:
                SP_NoiseImg[randR,randG,randB]=0
            else:
                SP_NoiseImg[randR,randG,randB]=255
        return SP_NoiseImg
    #模糊
    def Blur(img):
        blur = cv2.GaussianBlur(img, (7, 7), 1.5)
        # #      cv2.GaussianBlur(图像,卷积核,标准差)
        return blur

    适用于项目的的整体代码

    为了满足项目的使用,我对上述代码进行了了扩充,实现了对单个图片,单个文件夹和多个文件夹中多个图片的图像的增强

    到此这篇关于Python OpenCV实现图像增强操作详解的文章就介绍到这了,更多相关Python OpenCV图像增强内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!