将 min(min(r, g), b) 从 matlab 转换为 python

问题描述 投票:0回答:1

我在 matlab 中有这样的代码:

r = [10 30 23 50 27; 99 21 38 44 62; 90 22 64 12 56];
g = [92 74 91 37 33; 14 82 44 22 49; 88 10 98 74 33];
b = [73 22 94 18 34; 88 37 29 12 30; 79 39 48 95 24];

result = min(min(r, g), b);

结果是得到r/g/b之间的小值,如下所示:

我尝试在 python 中使用函数 min(),但出现错误:AttributeError: 'tuple' object has no attribute 'min'

rgb = cv2.normalize(temp_rgb.astype('float'), None, 0.0, 1.0, 
cv2.NORM_MINMAX)
r = rgb[:, :, 0]
g = rgb[:, :, 1]
b = rgb[:, :, 2]
result = ((r, g).min(),b).min()

也许在 python 中有办法像在 matlab 中那样获得结果?谢谢

python matlab min
1个回答
0
投票

您可以按如下方式使用

numpy.array()
numpy.min()

import numpy as np

# R, G, B data
r = np.array([[10, 30, 23, 50, 27],
              [99, 21, 38, 44, 62],
              [90, 22, 64, 12, 56]])

g = np.array([[92, 74, 91, 37, 33],
              [14, 82, 44, 22, 49],
              [88, 10, 98, 74, 33]])

b = np.array([[73, 22, 94, 18, 34],
              [88, 37, 29, 12, 30],
              [79, 39, 48, 95, 24]])

# Result
result = np.min(np.array([r, g, b]), axis=0)
print(result)

结果是一个

numpy
数组:

[[10 22 23 18 27]
 [14 21 29 12 30]
 [79 10 48 12 24]]
© www.soinside.com 2019 - 2024. All rights reserved.