numpy - 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)
我希望这是正确的!?有没有更快的方法来做到这一点?有什么可以改进的?
解决方案
您可以使用较少的行数来做到这一点。
min_ = np.min(I)
max_ = np.max(I)
GI = (255 * (I-min_) / (max_-min_)).astype(np.uint8)
推荐阅读
- camunda - Camunda“接收任务”打开“异步之后”会导致“无法关联消息”错误
- django - django ModelAdmin autocomplete_fields 中的 ID 保存在哪里?
- git - 如何恢复从一个分支合并的所有提交
- python - Python中的更多阈值算法?
- c# - 在 ItemsSourceUpdated 之后关注 DataGridRow
- java - 如何理解“应用程序的状态应存储在文件中”?
- firebase - Firebase 实时数据库 - 如何使用 REST API 管理数据库规则
- r - R中混合数据的DBSCAN
- javascript - 如何从子组件调用 React Hook 以刷新结果
- asp.net-core - 为多租户运行多个数据库的 ABP IO 代码示例