简体   繁体   中英

Save BGR image with cv2.imwrite

I have a 2D numpy array which it's values are float between [-4, 3]. cv2.imshow shows this array as a BGR Image, but when I saved it with cv2.imwrite it was completely black. Then I found that I have to multiply the array to 255 to save it with imwrite , but in this case the image saved in RGB format, but I want to save the BGR image which shown by cv2.imshow . What should I do?

So, basically you want to convert your image from RGB to BGR image.

This can be done by using cv2.cvtColor() function.

result_BGR = cv2.cvtColor(RGB_image, cv2.COLOR_RGB2BGR)
cv2.imwrite('PATH', result_BGR)

First of all you have to adjust the value of all arrays. The pixels are between -4 and 3 so you have to do this:

img = img - min_val
img = img*255.0/(max_val - min_val)

which in your case it would be like:

img = img+4
img = img*255/7.0

then convert your img to 8bit unsinged int and save it with imwrite (no need to mess with BGR or RGB, opencv handles it by itself)

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