mhmmdrz92
mhmmdrz92

Reputation: 57

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?

Upvotes: 2

Views: 5216

Answers (2)

MeiH
MeiH

Reputation: 1855

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)

Upvotes: 0

Chetan Goyal
Chetan Goyal

Reputation: 474

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)

Upvotes: 4

Related Questions