在CV2 Python中实现辉光过滤器

2024-06-26 13:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

目标是使用Python3在cv2中实现GLOW实现,这个站点:https://pinetools.com/glow-effect-image

当前的实现是用javascript编写的,可以在以下repo中找到: ^发光下的{a2}

我对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)

示例pict

example pict

期望结果

desired result

我的成绩

my result

但正如你所看到的那样,结果还远远不够。有人能帮忙吗

编辑-接受答案的解释

谢谢阿克斯和阿伦。我测试了你的两种解决方案,结果可以在这里找到(左Aron,右AKX)。AKX的解与示例中的解完全相同。谢谢你们两位

enter image description here


Tags: pathorgimage示例目标imgifany
2条回答

原始代码使图像模糊,然后将模糊版本添加到原始图像中

这可以使用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)

使用矩阵乘法。希望这有助于:

## 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)

请参见此链接中的结果: result

相关问题 更多 >