Ask Your Question

Why gray to BGR conversion giving distorted results

asked 2017-12-21 23:52:03 -0500

Nilushika gravatar image

I used cvtColor for converting int16 2D numpy array into BGR color image. But when view it via imshow() it giving distorted picture. Here is the code snippet i made conversion.

import cv2 as cv

color_image = cv.cvtColor(np.float32(ct_scan), cv.COLOR_GRAY2BGR)


The source image, ct_scan is a int16 2D numpy array. The conversion of ct_scan into float32 was done to compatible with the cvtColor function. The results are showing in figure 1-The converted color image and figure 2-source image converted to float32 dtype

But as you can see the converted image is distorted. Can you please help to identify the error behind, or suggest a method to convert the 2D numpy array to a color image.

image description First image image description Second image

edit retag flag offensive close merge delete


note, that your image is still in the 16bit range, while matplotlib probably expects something in [0..1] for float images. try to divide by 0xffff before that

berak gravatar imageberak ( 2017-12-22 02:49:07 -0500 )edit

Thanks berak

Nilushika gravatar imageNilushika ( 2017-12-22 22:21:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-22 00:39:15 -0500

moHe gravatar image

Firstly, you'd better know the gray transformation between gray and rgb: Gray_value = 0.299R + 0.587G + 0.114B, so the transformation is not reversible, the reverse convertion is probably that each channel take the 1/3 of gray value.

Then the convertion may cause information loss due to the format transformation, as I thought.

Hope this can inspire you:)

edit flag offensive delete link more


Thanks moHe. But the same conversion not giving bad results for images like Cameraman

Nilushika gravatar imageNilushika ( 2017-12-22 02:13:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-21 23:52:03 -0500

Seen: 362 times

Last updated: Dec 22 '17