Skip to content

Instantly share code, notes, and snippets.

@HViktorTsoi
Last active March 13, 2023 02:15
Show Gist options
  • Save HViktorTsoi/118186d169cf5c26f4a26e220431cdfc to your computer and use it in GitHub Desktop.
Save HViktorTsoi/118186d169cf5c26f4a26e220431cdfc to your computer and use it in GitHub Desktop.
A simple implementation of image devignetting.
def devignetting(image, radius_sigma=0.3, amount=0.7, power=1.1):
"""
:param image: input image
:param radius_sigma: vignetting radius = radius_sigma * image width
:param amount: the amount of exposure increment for devignetting
:param power: increase this will add extra exposure increment on image border
:return:
"""
tic = time.time()
height, width = image.shape[:2]
# generating vignette mask using Gaussian kernels
# 这里由于镜头是圆形 所以宽高一致
kernel_x = cv2.getGaussianKernel(width, width * radius_sigma).astype(np.float32)
kernel_y = cv2.getGaussianKernel(height, width * radius_sigma).astype(np.float32)
kernel = kernel_y * kernel_x.T
mask_not_norm = 255 * kernel / np.linalg.norm(kernel)
mask_norm = mask_not_norm / mask_not_norm.max()
# mask_scale = 1 / np.minimum(np.power(1.4 * mask_norm, 1.1), 1)
# mask_scale = np.maximum(amount * 1 / np.power(mask_norm, power), 1)
mask_scale = np.maximum(amount * 1 / np.power(mask_norm, power), 0.7)
# mask_scale = cv2.GaussianBlur(mask_scale, ksize=(55, 55), sigmaX=1)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# rst = np.minimum((image * mask_scale), 255) / 255 ## scale pixel values up or down for channel 1(Lightness)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# hsv = np.array(hsv, dtype=np.float64)
hsv[:, :, 1] = np.minimum(hsv[:, :, 1] * mask_scale, 255) ## scale pixel values up or down for channel 1(Lightness)
hsv[:, :, 2] = np.minimum(hsv[:, :, 2] * mask_scale, 255) ## scale pixel values up or down for channel 1(Lightness)
hsv = np.array(hsv, dtype=np.uint8)
rst = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
toc = time.time()
print(toc - tic)
return rst, mask_scale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment