python - Scipy 的 misc bytescale 方法的替代方法

标签 python scipy

我有一段旧代码,它说它已经被弃用了。实现这行代码的目的有哪些可能的替代方案?

img = misc.bytescale(img, high=16, low=0)

最佳答案

我遇到了同样的问题,但没有找到解决方案。 我在此处复制了 source code 的略微修改版本(只是 numpy 进口):

import numpy as np

# Returns a byte-scaled image
def bytescale(data, cmin=None, cmax=None, high=255, low=0):
    """
    Byte scales an array (image).

    Byte scaling means converting the input image to uint8 dtype and scaling
    the range to ``(low, high)`` (default 0-255).
    If the input image already has dtype uint8, no scaling is done.

    Parameters
    ----------
    data : ndarray
        PIL image data array.
    cmin : scalar, optional
        Bias scaling of small values. Default is ``data.min()``.
    cmax : scalar, optional
        Bias scaling of large values. Default is ``data.max()``.
    high : scalar, optional
        Scale max value to `high`.  Default is 255.
    low : scalar, optional
        Scale min value to `low`.  Default is 0.

    Returns
    -------
    img_array : uint8 ndarray
        The byte-scaled array.

    Examples
    --------
    >>> img = array([[ 91.06794177,   3.39058326,  84.4221549 ],
                     [ 73.88003259,  80.91433048,   4.88878881],
                     [ 51.53875334,  34.45808177,  27.5873488 ]])
    >>> bytescale(img)
    array([[255,   0, 236],
           [205, 225,   4],
           [140,  90,  70]], dtype=uint8)
    >>> bytescale(img, high=200, low=100)
    array([[200, 100, 192],
           [180, 188, 102],
           [155, 135, 128]], dtype=uint8)
    >>> bytescale(img, cmin=0, cmax=255)
    array([[91,  3, 84],
           [74, 81,  5],
           [52, 34, 28]], dtype=uint8)

    """
    if data.dtype == np.uint8:
        return data

    if high < low:
        raise ValueError("`high` should be larger than `low`.")

    if cmin is None:
        cmin = data.min()
    if cmax is None:
        cmax = data.max()

    cscale = cmax - cmin
    if cscale < 0:
        raise ValueError("`cmax` should be larger than `cmin`.")
    elif cscale == 0:
        cscale = 1

    scale = float(high - low) / cscale
    bytedata = (data * 1.0 - cmin) * scale + 0.4999
    bytedata[bytedata > high] = high
    bytedata[bytedata < 0] = 0
    return np.cast[np.uint8](bytedata) + np.cast[np.uint8](low)

#example
img = np.array([[ 91.06794177,   3.39058326,  84.4221549 ],
                [ 73.88003259,  80.91433048,   4.88878881],
                [ 51.53875334,  34.45808177,  27.5873488 ]])
print(img)
print(bytescale(img))

返回

[[91.06794177  3.39058326 84.4221549 ]
 [73.88003259 80.91433048  4.88878881]
 [51.53875334 34.45808177 27.5873488 ]]
[[255   0 236]
 [205 225   4]
 [140  90  70]]

关于python - Scipy 的 misc bytescale 方法的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705804/

相关文章:

python - 翻转列表中的单词

python - 为什么 scipy.optimize.curve_fit 不适合数据?

python - Python中基于中值的线性回归

python - 如何使用 scipy 设置而不是拟合样条插值的系数?

python - 为什么这个 log gamma numba 函数对于大型数组比 scipy 慢,但对于单个值却比 scipy 慢?

python - scipy.ndimage.zoom 结果取决于图像大小

python - Python 中的 Ruby LESS gem 等效项

python - 宏程序不起作用

python - 为什么在 Python 中 open() 优于 file()?

python - matplotlib 中不同宽度和颜色的条形图(Bar Mekko 图)