使用 CV2 为图像添加光晕效果
Adding bloom effect to an image using CV2
我目前正在后端使用 CV2 开发一个简单的照片编辑器,我想知道是否可以使用 CV2 向图像添加 bloom effect。
感谢您的帮助!
可以在Python/OpenCV实现bloom效果如下:
- Read the input
- Convert to HSV colorspace as floats and separate channels
- Invert the saturation channel and multiply with the value channel, since to find white, we need low saturation and high brightness
- Blur the product and convert to 3 channels
- Blend the blurred image with the original applying gain to the blurred image
- Save the result
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('barn.jpg')
# set arguments
thresh_value = 245 # threshold to find white
blur_value = 50 # bloom smoothness
gain = 6 # bloom gain in intensity
# convert image to hsv colorspace as floats
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float64)
h, s, v = cv2.split(hsv)
# Desire low saturation and high brightness for white
# So invert saturation and multiply with brightness
sv = ((255-s) * v / 255).clip(0,255).astype(np.uint8)
# threshold
thresh = cv2.threshold(sv, thresh_value, 255, cv2.THRESH_BINARY)[1]
# blur and make 3 channels
blur = cv2.GaussianBlur(thresh, (0,0), sigmaX=blur_value, sigmaY=blur_value)
blur = cv2.cvtColor(blur, cv2.COLOR_GRAY2BGR)
# blend blur and image using gain on blur
result = cv2.addWeighted(img, 1, blur, gain, 0)
# save output image
cv2.imwrite('barn_bloom.jpg', result)
# display IN and OUT images
cv2.imshow('image', img)
cv2.imshow('sv', sv)
cv2.imshow('thresh', thresh)
cv2.imshow('blur', blur)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
我目前正在后端使用 CV2 开发一个简单的照片编辑器,我想知道是否可以使用 CV2 向图像添加 bloom effect。
感谢您的帮助!
可以在Python/OpenCV实现bloom效果如下:
- Read the input
- Convert to HSV colorspace as floats and separate channels
- Invert the saturation channel and multiply with the value channel, since to find white, we need low saturation and high brightness
- Blur the product and convert to 3 channels
- Blend the blurred image with the original applying gain to the blurred image
- Save the result
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('barn.jpg')
# set arguments
thresh_value = 245 # threshold to find white
blur_value = 50 # bloom smoothness
gain = 6 # bloom gain in intensity
# convert image to hsv colorspace as floats
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float64)
h, s, v = cv2.split(hsv)
# Desire low saturation and high brightness for white
# So invert saturation and multiply with brightness
sv = ((255-s) * v / 255).clip(0,255).astype(np.uint8)
# threshold
thresh = cv2.threshold(sv, thresh_value, 255, cv2.THRESH_BINARY)[1]
# blur and make 3 channels
blur = cv2.GaussianBlur(thresh, (0,0), sigmaX=blur_value, sigmaY=blur_value)
blur = cv2.cvtColor(blur, cv2.COLOR_GRAY2BGR)
# blend blur and image using gain on blur
result = cv2.addWeighted(img, 1, blur, gain, 0)
# save output image
cv2.imwrite('barn_bloom.jpg', result)
# display IN and OUT images
cv2.imshow('image', img)
cv2.imshow('sv', sv)
cv2.imshow('thresh', thresh)
cv2.imshow('blur', blur)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: