• 获得不同干扰程度的模糊图像


    同时对一共父级文件夹遍历。获得对应不同干扰程度的模糊图像

    # This is
    
    import cv2
    import numpy as np
    
    def reduce_resolution(image, factor):
    
        height, width, _ = image.shape    # 获取原始图像的宽度和高度
    
        new_width = int(width / factor) # 计算新的宽度和高度
        new_height = int(height / factor)
    
        # 使用resize方法来缩小图像分辨率,保持尺寸不变
        resized_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
        return resized_image
    
    def motion_blur(image, degree=12, angle=45):
        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
    
    
    img = cv2.imread(r'H:\LRFRcode\acrface-bubb\arcface-pytorch-main\ForPaper1_Images_Crop\N010\N010_0008__Gaus2.jpg')
    
    idex = 15
    Image_perturbation_Gaussian_idex = 1.02*idex
    Image_perturbation_Motion_idex = 3*idex
    Image_perturbation_reduceresolution_idex = 1.1*idex
    
    #
    Gaussian_blurred_image = cv2.GaussianBlur(img, ksize=(0,0), sigmaX=Image_perturbation_Gaussian_idex) # ksize 必须是一个正奇数,可以通过 (0, 0) 来自动计算核的大小
    Motion_blurred_image = motion_blur(img, degree=Image_perturbation_Motion_idex, angle=45)
    reduce_resolution_image = reduce_resolution(img, factor=Image_perturbation_reduceresolution_idex)
    
    # cv2.imshow('Original', img)
    # cv2.imshow('Gaussian Filter', Gaussian_blurred_image)
    # cv2.imshow('Motion Filter', Motion_blurred_image)
    # cv2.imshow('reduce_resolution Filter', reduce_resolution_image)
    #
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    
    
    # 保存图像
    cv2.imwrite("save/Gaussian_blurred_image" + str(Image_perturbation_Gaussian_idex) +".jpg", Gaussian_blurred_image)  # 保存降低分辨率后的图像
    cv2.imwrite("save/Motion_blurred_image" + str(Image_perturbation_Motion_idex) +".jpg", Motion_blurred_image)  # 保存降低分辨率后的图像
    cv2.imwrite("save/reduced_resolution_image_" + str(Image_perturbation_reduceresolution_idex) +".jpg", reduce_resolution_image)  # 保存降低分辨率后的图像
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    Unity学习——平台发布过程(windows)
    K8S安装过程十一:istio 服务网格与 Ingress 部署
    【Linux操作系统】——安装VMware
    网络编程_bind函数
    从零搭建Vue项目
    植物大战僵尸各种僵尸攻略
    acwing KMP算法java版实现
    ruoyi前后端分离3.8.6版本,把用户昵称设置到Vuex中,使任何组件都能直接获取用户的昵称
    用HTML+CSS+JS做一个漂亮简单的游戏网页——全屏游戏美术大赛作品(4个滚动页面)
    [PSQL] 窗口函数、GROUPING运算符
  • 原文地址:https://blog.csdn.net/vibration_xu/article/details/134456510