|
| 1 | +#coding:utf-8 |
| 2 | +#By:Eastmount CSDN 2020-12-22 |
| 3 | +import cv2 |
| 4 | +import math |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +#读取原始图像 |
| 8 | +img = cv2.imread('nv.png') |
| 9 | + |
| 10 | +#获取图像行和列 |
| 11 | +rows, cols = img.shape[:2] |
| 12 | + |
| 13 | +#设置中心点 |
| 14 | +centerX = rows / 2 |
| 15 | +centerY = cols / 2 |
| 16 | +print(centerX, centerY) |
| 17 | +radius = min(centerX, centerY) |
| 18 | +print(radius) |
| 19 | + |
| 20 | +#设置光照强度 |
| 21 | +strength = 200 |
| 22 | + |
| 23 | +#新建目标图像 |
| 24 | +dst = np.zeros((rows, cols, 3), dtype="uint8") |
| 25 | + |
| 26 | +#图像光照特效 |
| 27 | +for i in range(rows): |
| 28 | + for j in range(cols): |
| 29 | + #计算当前点到光照中心的距离(平面坐标系中两点之间的距离) |
| 30 | + distance = math.pow((centerY-j), 2) + math.pow((centerX-i), 2) |
| 31 | + #获取原始图像 |
| 32 | + B = img[i,j][0] |
| 33 | + G = img[i,j][1] |
| 34 | + R = img[i,j][2] |
| 35 | + if (distance < radius * radius): |
| 36 | + #按照距离大小计算增强的光照值 |
| 37 | + result = (int)(strength*( 1.0 - math.sqrt(distance) / radius )) |
| 38 | + B = img[i,j][0] + result |
| 39 | + G = img[i,j][1] + result |
| 40 | + R = img[i,j][2] + result |
| 41 | + #判断边界 防止越界 |
| 42 | + B = min(255, max(0, B)) |
| 43 | + G = min(255, max(0, G)) |
| 44 | + R = min(255, max(0, R)) |
| 45 | + dst[i,j] = np.uint8((B, G, R)) |
| 46 | + else: |
| 47 | + dst[i,j] = np.uint8((B, G, R)) |
| 48 | + |
| 49 | +#显示图像 |
| 50 | +cv2.imshow('src', img) |
| 51 | +cv2.imshow('dst', dst) |
| 52 | + |
| 53 | +cv2.waitKey() |
| 54 | +cv2.destroyAllWindows() |
| 55 | + |
0 commit comments