Convert a 3D array to RGB valued array [closed]

It’s not so clear what you’re asking, but here are some options to get you started:

Manual Min-max transform

You could min-max transform so they are all in the range of [0,1], and then multiply by 255. Then, all your values are in the range of 0 to 255, proportional to your original array (i.e. your minimum value will be 0 and your maximum value will be 255).

(arr-np.min(arr))/(np.max(arr)-np.min(arr))*255

This would give you:

array([[[ 33.25036151, 204.95570265,  31.63134374],
        [ 44.98801296,  61.73433955, 251.40292398],
        [  0.31901231,   0.        ,   2.66884725]],

       [[213.36859086,   7.97712711,  57.10866111],
        [ 50.88124979,  28.211362  ,  77.03237453],
        [ 11.12661552,   6.97460364,  62.52914112]],

       [[141.35580825,  10.20414837,  49.44266198],
        [ 37.83388908,  25.34995505,  71.00722671],
        [ 44.95101723, 255.        , 162.13102979]]])

cv2 options:

As you tagged this opencv, you could also use their normalize function:

import cv2

cv2.normalize(arr,0,255)

Also gives you an array between 0 and 255:

array([[[ 16,  96,  15],
        [ 21,  29, 117],
        [  0,   0,   1]],

       [[100,   4,  27],
        [ 24,  13,  36],
        [  5,   3,  29]],

       [[ 66,   5,  23],
        [ 18,  12,  33],
        [ 21, 119,  76]]], dtype=int32)

Note: you can get a min-max transform, as I showed manually above, using cv2 as well:

cv2.normalize(arr,0,255, norm_type=cv2.NORM_MINMAX, dtype=5)

array([[[ 33.250362 , 204.9557   ,  31.631344 ],
        [ 44.988014 ,  61.73434  , 251.40292  ],
        [  0.3190123,   0.       ,   2.6688473]],

       [[213.36859  ,   7.977127 ,  57.10866  ],
        [ 50.88125  ,  28.211363 ,  77.03237  ],
        [ 11.126616 ,   6.9746037,  62.52914  ]],

       [[141.3558   ,  10.204148 ,  49.44266  ],
        [ 37.83389  ,  25.349955 ,  71.007225 ],
        [ 44.951015 , 255.       , 162.13103  ]]], dtype=float32)

Leave a Comment