[英]Color problem with Log transform to brighten dark area. Why and how to fix?
因此,我尝试通过对原始图像应用对数变换来增强该图像。亮白色区域在增强图像上变成蓝色。 增强图像
path = '...JPG'
image = cv2.imread(path)
c = 255 / np.log(1 + np.max(image))
log_image = c * (np.log(image + 1))
# Specify the data type so that
# float value will be converted to int
log_image = np.array(log_image, dtype = np.uint8)
cv2.imwrite('img.JPG', log_image)
还有一个警告:RuntimeWarning: divide by zero encountered in log
我尝试使用其他类型的日志(例如 log2、log10...),但它仍然显示相同的结果。 我尝试更改 dtype = np.uint32 但它会导致错误。
两个问题的原因相同
即这一行
log_image = c * (np.log(image + 1))
image+1
是np.uint8
的数组,就像image
一样。 但是如果 image 有 255 个分量,那么image+1
就会溢出。 256
变为 0。这导致np.log(imag+1)
此时为log(0)
。 因此错误。 因此,最亮的部分有奇怪的 colors,因为它们包含255
所以,由于 log 无论如何都必须使用 float,所以在调用 log 之前自己转换为 float
path = '...JPG'
image = cv2.imread(path)
c = 255 / np.log(1 + np.max(image))
log_image = c * (np.log(image.astype(float) + 1))
# Specify the data type so that
# float value will be converted to int
log_image = np.array(log_image, dtype = np.uint8)
cv2.imwrite('img.JPG', log_image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.