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


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

    # 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
  • 相关阅读:
    压力测试+接口测试
    【ArcGIS Pro二次开发】(66):三调规程_土地利用现状分类面积汇总表
    Kotlin协程Flow浅析
    全局光照RSM
    RK3568平台开发系列讲解(调试篇)系统运行相关频率设置
    【深度学习】attention机制
    学习Oracle数据库相关基本操作(一)
    杂货配送服务公司Instacart申请1亿美元纳斯达克IPO上市
    基于目标检测的无人机航拍场景下小目标检测实践
    【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
  • 原文地址:https://blog.csdn.net/vibration_xu/article/details/134456510