重写下光照代码,原文链接https://blog.csdn.net/donkey_1993/article/details/107034847
原代码:
rows, cols = img.shape[:2]
#设置中心点
centerX = rows / 2
centerY = cols / 2
print (centerX, centerY)
radius = min(centerX, centerY)
print (radius)
#设置光照强度
strength = 200
for i in range(rows):
for j in range(cols):
# 计算当前点到光照中心距离(平面坐标系中两点之间的距离)
distance = math.pow((centerY - j), 2) + math.pow((centerX - i), 2)
# 获取原始图像
B, G, R = img[i, j]
if (distance < radius * radius):
# 按照距离大小计算增强的光照值
result = (int)(strength * (1.0 - math.sqrt(distance) / radius))
B = img[i, j][0] + result
G = img[i, j][1] + result
R = img[i, j][2] + result
# 判断边界 防止越界
B = min(255, max(0, B))
G = min(255, max(0, G))
R = min(255, max(0, R))
img[i, j] = np.uint8((B, G, R))
else:
img[i, j] = np.uint8((B, G, R))
我用了numpy重写了下循环那部分的代码 稍微快了点 就是不易读
c_point = np.array([centerX,centerY])
y= np.arange(0,rows)
x = np.arange(0,cols)
res = np.meshgrid(x,y)
grid = np.concatenate((np.expand_dims(res[1],-1),np.expand_dims(res[0],-1)), axis=2)
distance = np.linalg.norm(c_point - grid,axis=2)
result_ = (strength*(1 - distance / radius))
result_ = np.where(result_>0,result_,0).astype(np.int)
img = np.clip(img+np.expand_dims(result_,-1),0,255).astype(np.uint8)
再附一个 运动模糊的链接