亮化 HSV 图像中的暗块

Dark pieces in brightened HSV image

我有一张图片想要更亮。所以我只是将它转换为 HSV 并将 V 值乘以我需要的系数。但是出图有暗块
原图:

出图:

代码:

import cv2
import numpy as np


real_4 = cv2.imread('data/data_4_real_cut.jpg')
k = 5

cv2.imshow('orig 4', real_4)
hsv_4 = cv2.cvtColor(real_4, cv2.COLOR_BGR2HSV)
hsv_4[:, :, 2] *= k
bgr_again = cv2.cvtColor(hsv_4, cv2.COLOR_HSV2BGR)
cv2.imshow('orig 4 high', bgr_again)

cv2.waitKey(0)

据我所知,当我将 V 值乘以所需系数时,它可能会超出 0 - 255 范围。但是我不确定,也没有找到有关它的任何信息。当然,问题是 - 为什么图像中有暗块?谢谢!

图像中的值存储为数据类型np.uint8,它只能存储无符号的 8 位整数 (0-255)。当乘法结果超过 255 时,值将 overflow into actual_result % 256, for example: np.uint8(266) == (266 % 256) == 10. To prevent overflow, the result needs to be clipped to a maximum value of 255, for which you could use the cv2.multiply method:

import cv2

from skimage.io import imread

rgb = imread("https://i.stack.imgur.com/2tcDv.jpg")
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)

k = 5

hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
# safe multiplication without overflow
hsv[..., 2] = cv2.multiply(hsv[..., 2], k)

bgr_again = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow("orig 4 high", bgr_again)

cv2.waitKey(0)

这将给出正确的输出结果: