在 CV2 中实现辉光滤镜 Python

Implement glow filter in CV2 Python

目标是使用来自此站点的 Python3 在 cv2 中实现 GLOW 实现:https://pinetools.com/glow-effect-image.

当前的实现是在 javascript 中编写的,可以在这个 repo 中找到: https://github.com/jseidelin/pixastic/blob/fa4b9283f0a57c677d0f6762933bdfe5b0714242/pixastic.effects.js#L587 发光。

我对javascript的了解非常有限,所以我不明白算法的步骤是什么。希望有人能帮忙。

我试过了:

IMAGE_PATH = 'path'

img_org = cv2.imread(IMAGE_PATH, 1)
gaussian = cv2.GaussianBlur(img_org,(3,3),1)


b,g,r = cv2.split(gaussian)
b_n = b + 0*100
g_n = g + 1*100
r_n = r + 2*100

if r_n.any() > 255:
    r_n = 255
if g_n.any() > 255:
    g_n = 255
if b_n.any() > 255:
    b_n= 255

merged = cv2.merge([b_n, g_n, r_n])
concatted = np.concatenate((img_org, merged), axis=1)

cv2.imshow('final', concatted)
cv2.waitKey(0)

示例图片

想要的结果

我的结果

但如您所见,结果相去甚远。有人可以帮忙吗?

编辑 - 已接受答案的解释

感谢 AKX 和 Aron。我测试了你们的两个解决方案,结果可以在这里找到(左 Aron,右 AK​​X)。 AKX 的解决方案与示例中的解决方案完全相同。谢谢你们。

使用矩阵乘法。希望这会有所帮助:

## Import packages
import numpy as np
import matplotlib.pyplot as plt
import cv2
%matplotlib inline 

## Parameters
IMAGE_PATH = 'plate.jpg'
param_resize = 2000 

# Read the image
img_org = cv2.imread(IMAGE_PATH)

# Resize based on the parameters
img_org = cv2.resize(img_org, (param_resize,param_resize))

# Gausian
gaussian = cv2.GaussianBlur(img_org,(3,3),1)

## Split the gausian and the orginal image
b_gaus,g_gaus,r_gaus = cv2.split(gaussian)
b,g,r = cv2.split(img_org)

## Matrix multiplication 
##### When reading in with cv2 then the type is uint8, which means the range is max 255
calculated_b_array = b_gaus.astype(int) + b.astype(int)
calculated_g_array = g_gaus.astype(int) + g.astype(int)
calculated_r_array = r_gaus.astype(int) + r.astype(int)

## If the pixelvalue is higher than 255, set it to 255
calculated_b_array[calculated_b_array > 255] = 255
calculated_g_array[calculated_g_array > 255] = 255
calculated_r_array[calculated_r_array > 255] = 255

## Merge for visualization purposes
merged = cv2.merge([calculated_b_array, calculated_g_array, calculated_r_array])
concatted = np.concatenate((img_org, merged), axis=1)

## Save the image
cv2.imwrite('plate_output.jpeg', concatted)

查看此 link 中的结果: result

原始代码对图像进行模糊处理,然后将模糊版本添加到原始图像中。

这可以使用 OpenCV 在几行中完成。

您可以使用给定的变量调整发光强度和半径。

import cv2

source_path = '4wJqC.jpg'
glow_strength = 1  # 0: no glow, no maximum
glow_radius = 25  # blur radius

img = cv2.imread(source_path, cv2.IMREAD_COLOR)
img_blurred = cv2.GaussianBlur(img, (glow_radius, glow_radius), 1)
img_blended = cv2.addWeighted(img, 1, img_blurred, glow_strength, 0)

cv2.imwrite('test.jpg', img_blended)