numpy 对数的一个积极的数字结果在负无穷Python

iyfamqjs  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(60)

图片来源:HI00008918.png
我想应用一个对数函数(f(x) = (1/a)*log(x + 1),其中a = 0.01)在图像上.
这就是代码:

import numpy as np
import matplotlib.pyplot as plt
import skimage.io as io

car = io.imread('HI00008918.png')
# plt.imshow(car, cmap='gray', vmin=0, vmax=255)

a = 0.01

fnLog = lambda x : (1/a)*np.log(x + 1) # logarithmic function

# the original image has white pixels (=0) and black pixels (=255)

carLog = fnLog(car) # Applying the function fnLog

print(car[0][0][-1])

print(carLog[0][0][-1])

print(fnLog(car[0][0][-1]))

字符串
输出:

255
-inf
554.5177444479563


在某个时刻,它会导致-inf,而在其他时刻,它会导致正确的值:(
现在我将展示数组:

carLog =
[[[277.2 277.2 277.2  -inf]
  [289.  289.  289.   -inf]
  [304.5 304.5 304.5  -inf]
  ...
  [423.5 431.8 429.   -inf]
  [422.  434.5 427.8  -inf]
  [437.  450.  440.5  -inf]]

 [[434.5 434.5 434.5  -inf]
  [433.2 433.2 433.2  -inf]
  [430.5 430.5 430.5  -inf]
  ...
  [422.  430.5 427.8  -inf]
  [420.2 429.  426.2  -inf]
  [433.2 444.2 438.2  -inf]]]

car =
[[[ 15  15  15 255]
  [ 17  17  17 255]
  [ 20  20  20 255]
  ...
  [148 138 149 255]
  [138 125 142 255]
  [148 134 151 255]]

 [[ 10  10  10 255]
  [ 14  14  14 255]
  [ 19  19  19 255]
  ...

ffvjumwh

ffvjumwh1#

看起来np.log(x + 1)只在数组中x为255的地方给出-Inf。
因为数组xuint8,所以将1加到255会导致溢出,这会将结果 Package 为0。0的log是-Inf。
在应用函数之前,您可能希望将图像转换为浮点类型:

carLog = fnLog(car.astype(np.float32))

字符串
当您将该函数应用于从图像中提取的值时,您正在使用Python int,它永远不会溢出。

相关问题