Skip to content
Advertisement

numpy image float array, to int 0..255 value

I got 3 NumPy data arrays r,g,b represented as a 2D float64 array (720×1024)
Essentially per row, each channel is a bunch of floats:

JavaScript

What I would like to to do is making it a channel that I can use in cv2.merge((r,g,b))
So that the float64 values per row get multiplied by 255 and something that cv2.merge() accepts.
I think NumPy can do such things but I’m confused, is there a way to do this?.

Advertisement

Answer

lets consider your three arrays to be r and g and b respectively. In such a case the following code should be what you need-

Edit-1: based on Mark’s comment, I suggest the following changes

JavaScript

Edit-2: I may be wrong, but I’m not convinced that multiplying with 255.99999 is better. Doing so will result in many other pixel values getting bumped to the next value than they are originally or were supposed to be, here is small example I could come up with

JavaScript

Additionally, I tied another small experiment with all possible pixel values and this surprisingly tells that both the methods are accurate

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement