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

标签 python numpy opencv image-processing image-enhancement

所以我尝试通过对其应用对数变换来增强该图像 original 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)

还有一个警告:RuntimeWarning:日志中遇到除零

我尝试使用其他类型的日志(例如 log2、log10...),但它仍然显示相同的结果。我尝试更改 dtype = np.uint32 但它会导致错误。

最佳答案

两个问题的原因相同

就是这一行

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

image+1 是一个 np.uint8 数组,就像 image 一样。但如果图像中有 255 个组件,则 image+1 会溢出。 256 被转换为 0。这导致 np.log(imag+1) 此时变为 log(0)。因此出现了错误。 因此,最亮的部分具有奇怪的颜色,因为它们包含 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)

enter image description here

关于python - 对数变换提亮暗区的颜色问题。为什么以及如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74523327/

相关文章:

python - 小绿色 "+"按钮不再显示在 Django 管理中

python - 填充现有 numpy 数组和创建新数组之间的性能差异

python - 如何使用生成器初始化 numpy 数组?

C++ 应用程序控制台未退出

c++ - 将一行 cv::Mat 转换为 std::vector

python - Pandas:对 DataFrame 的每一列进行 nansum 系列

python - 元组对列表中的唯一单个值?

python - 如何在 Django Rest 框架中设置 View

python - numba 中的性能嵌套循环

python - 什么是 OpenCV 中最好的头部级联分类器