【问题标题】:Difference between Python's 'img_as_ubyte' and Matlab's 'im2uint8'Python 的“img_as_ubyte”和 Matlab 的“im2uint8”之间的区别
【发布时间】:2021-01-14 13:56:08
【问题描述】:

如果我理解得很好,Python 中 Matlab 的 im2uint8 对应的是 img_as_ubyte 来自 scikit-image

但是下面给出了不同的结果

Python 3.7:
--------
import skimage
from skimage.util import img_as_ubyte

img_as_ubyte([0.3]) # returns 76
print(skimage.__version__) # prints 0.16.2



Matlab (2017b):
--------
im2uint8([0.3]) % returns 77

如果重要的话,这是在 Windows 10 下

有什么想法吗?

【问题讨论】:

    标签: python matlab image-processing scikit-image


    【解决方案1】:

    我不确定 Matlab 在做什么,但 scikit-image 乘以 255(uint8 的最大值),然后使用NumPy's default rounding 进行四舍五入,即"round to nearest, ties to even"。这里有一些实验应该可以澄清正在发生的事情:

    In [10]: from skimage import util
    
    In [11]: 0.3 * 255
    Out[11]: 76.5
    
    In [12]: np.round(76.5)
    Out[12]: 76.0
    
    In [13]: 77.5 / 255
    Out[13]: 0.30392156862745096
    
    In [14]: 0.30392156862745096 * 255
    Out[14]: 77.5
    
    In [15]: np.round(0.30392156862745096 * 255)
    Out[15]: 78.0
    
    In [16]: util.img_as_ubyte([0.30392156862745096])
    Out[16]: array([78], dtype=uint8)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 2016-08-04
      • 2021-06-10
      • 2016-11-06
      • 2012-12-09
      • 2021-01-02
      • 2016-03-26
      • 2011-03-24
      相关资源
      最近更新 更多