所以我尝试通过对其应用对数变换来增强该图像 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)
关于python - 对数变换提亮暗区的颜色问题。为什么以及如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74523327/