Search code examples
pythonmatlabmin

convert min(min(r, g), b) from matlab to python


i have code in matlab like this :

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);

the result is get the small value between r/g/b, like below :

result

i try in python using function min(), but it get error : 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()

maybe there is way in python to get result like in matlab? thankyou


Solution

  • You can use numpy.array() and numpy.min() as follows:

    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)
    
    

    The result is a numpy array:

    [[10 22 23 18 27]
     [14 21 29 12 30]
     [79 10 48 12 24]]