python实现图片径向模糊

网上找不到 拿C的代码改了改,因为速度太慢 ,用了numba加速

# -*- coding: utf-8 -*-
# @Author  : XinZhe Xie
# @University  : ZheJiang University

import math
import cv2
import numpy as np
from numba import njit
@njit(cache=True)
def radial_blur(img, num=30):
    height, width, _ = img.shape
    center = (int(width / 2), int(height / 2))

    img_blur = np.copy(img)

    weight = np.linspace(1, 1 / num, num)
    weight = weight / weight.sum()

    for y in range(height):
        for x in range(width):

            r = math.sqrt((x - center[0])**2 + (y - center[1])**2)
            angle = math.atan2(y - center[1], x - center[0])

            tmp = [0, 0, 0]
            for i in range(num):
                new_r = max(0, r - i)
                new_x = int(new_r * math.cos(angle) + center[0])
                new_y = int(new_r * math.sin(angle) + center[1])

                new_x = min(new_x, width-1)
                new_y = min(new_y, height-1)

                tmp[0] += img[new_y, new_x, 0]*weight[i]
                tmp[1] += img[new_y, new_x, 1]*weight[i]
                tmp[2] += img[new_y, new_x, 2]*weight[i]

            img_blur[y, x, 0] = int(tmp[0])
            img_blur[y, x, 1] = int(tmp[1])
            img_blur[y, x, 2] = int(tmp[2])

    return img_blur
#
# # 使用示例:
#
# img = cv2.imread(r'E:\pycharmproject\3dcon_fusion\img\3.jpg')
# blurred_img = radial_blur(img,num=20)
# cv2.imshow('blurred', blurred_img)
# cv2.waitKey()
# cv2.destroyAllWindows()

效果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值