我正在尝试用源图像创建霓虹灯效果。我已经包括了三张图片,来源,我目前的尝试和目标。该程序获取图像,找到白色边缘,并计算每个像素到最近的白色边缘的距离(这些部分都很好用);从那里,我努力寻找正确的饱和度和值参数来创建霓虹灯发光。
从目标图像来看,我需要的基本上是白色边缘上的饱和度为0,然后显著增加它离边缘越远;对于值,我需要白色边缘上的饱和度为1,然后急剧减小。我找不出处理Distance_Image(它保存每个像素到最近的白色边缘的距离)的最佳方法,比如使用饱和度和值来实现这两个结果。
from PIL import Image
import cv2
import numpy as np
from scipy.ndimage import binary_erosion
from scipy.spatial import KDTree
def find_closest_distance(img):
white_pixel_points = np.array(np.where(img))
tree = KDTree(white_pixel_points.T)
img_meshgrid = np.array(np.meshgrid(np.arange(img.shape[0]),
np.arange(img.shape[1]))).T
distances, _ = tree.query(img_meshgrid)
return distances
def find_edges(img):
img_np = np.array(img)
kernel = np.ones((3,3))
return img_np - binary_erosion(img_np, kernel)*255
img = Image.open('a.png').convert('L')
edge_image = find_edges(img)
distance_image = find_closest_distance(edge_image)
max_dist = np.max(distance_image)
distance_image = distance_image / max_dist
hue = np.full(distance_image.shape, 0.44*180)
saturation = distance_image * 255
value = np.power(distance_image, 0.2)
value = 255 * (1 - value**2)
new_tups = np.dstack((hue, saturation, value)).astype('uint8')
new_tups = cv2.cvtColor(new_tups, cv2.COLOR_HSV2BGR)
new_img = Image.fromarray(new_tups, 'RGB').save('out.png')
下图显示了源数据(左)、当前结果(中)和所需结果(右)。
2条答案
按热度按时间nhaq1z211#
我想我会用convolution来做这件事。将图像与Gaussian kernel进行卷积是blur an image的常见方法。您可以使用多种方法来完成此操作,但可能最容易使用的是
scipy.ndimage.gaussian_filter
。这里有一种实现所有这些的方法,看看你是否喜欢结果。这是你的第一张图片,白色长方形。
现在,我将制作这些轮廓,进行模糊处理,创建彩色图像,然后将它们组合在一起:
然后看看它:
您需要调整高斯的
sigma
(其宽度)、颜色、模糊强度等。希望能有所帮助。1u4esq0p2#
以下是在Python/OpenCV中执行此操作的一种方法。
输入:
边(反转):
拉伸距离变换:
衰减距离变换:
光晕结果: