簡體   English   中英

對數變換使暗區變亮的顏色問題。 為什么以及如何解決?

[英]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+1np.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM