简体   繁体   English

对数变换使暗区变亮的颜色问题。 为什么以及如何解决?

[英]Color problem with Log transform to brighten dark area. Why and how to fix?

So I try to enhance this image by applying log transform on it original image The area where there are bright white color turns into color blue on the enhanced image.因此,我尝试通过对原始图像应用对数变换来增强该图像。亮白色区域在增强图像上变成蓝色。 enhanced image增强图像

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)

There's also a warning: RuntimeWarning: divide by zero encountered in log还有一个警告:RuntimeWarning: divide by zero encountered in log

I tried using other type of log (eg log2, log10...) but it still show the same result.我尝试使用其他类型的日志(例如 log2、log10...),但它仍然显示相同的结果。 I tried changing dtype = np.uint32 but it causes error.我尝试更改 dtype = np.uint32 但它会导致错误。

Same cause for the two problems两个问题的原因相同

Namely this line即这一行

log_image = c * (np.log(image + 1))

image+1 is an array of np.uint8 , as image is. image+1np.uint8的数组,就像image一样。 But if there are 255 components in image, then image+1 overflows.但是如果 image 有 255 个分量,那么image+1就会溢出。 256 are turned into 0. Which lead to np.log(imag+1) to be log(0) at this points. 256变为 0。这导致np.log(imag+1)此时为log(0) Hence the error.因此错误。 And hence the fact that brightest parts have strange colors, since they are the ones containing 255因此,最亮的部分有奇怪的 colors,因为它们包含255

So, since log will have to work with floats anyway, just convert to float yourself before calling log所以,由于 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM