首页 > 解决方案 > Python:将 numpy 矩阵转换为灰度图像

问题描述

我想将类型为正值负值numpy的矩阵转换为像素从. 我想尽快做到这一点。np.float[0,255]

现在我正在做以下事情:

import numpy as np
n = 1000
I = np.random.randn(n,n)
I_min = np.min(I)
I = I + np.abs(I_min)
I_max = np.max(I)
I = 255 * (I / I_max)
I = I.astype(np.uint8)

我希望这是正确的!?有没有更快的方法来做到这一点?有什么可以改进的?

标签: numpyimage-processing

解决方案


您可以使用较少的行数来做到这一点。

min_ = np.min(I)
max_ = np.max(I)
GI = (255 * (I-min_) / (max_-min_)).astype(np.uint8)

推荐阅读