简体   繁体   中英

How to write a float32 picture to video file in OpenCV2?

I am experiencing a weird problem here. I get back an output image from my neural network, it has dtype float32 . I add a background picture I took from the internet, convert it to float32 from uint8, and then added my output on top of the background picture. Now, I want to save it, but cv2.VideoWriter.write(blahblah) only takes in uint8 . I thought that to convert float32 to uint8, one would just do * 255 , but that is probably wrong because I experience some weird glares (my picture can be found here: OpenCV image shows weird glare after un-normalizing with * 255 ) when I do so, probably because of floating point classic errors or things of that nature. So, how do I safely convert float32 to uint8? Or, how do I write float32 to file?

To be honest, I am very confused about some of the things. I know uint8 is [0, 255] and float32 is [0, 1], and I know that cv2.videowriter.write takes in uint8, that's it. On the side note, a solution appears to be to change everything to .astype(uint8) from the beginning, but I my neural net spits out a float32 so I have to work with that.

Float32 does not set is from 0 till 1 that is mostlikly done by your neuralnetwork (softmax or sigmoid as last activation function). To set something to a different range this can be used:

normalized_array = (array - np.min(array))/(np.max(array) - np.min(array)) # this set the range from 0 till 1
img_array = (normalized_array * 255).astype(np.uint8) # set is to a range from 0 till 255

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM