Ask Your Question
0

how to convert the gray image to color image

asked 2019-07-09 02:35:58 -0600

akramnarejo gravatar image

updated 2019-07-09 21:35:02 -0600

supra56 gravatar image

code:

image=cv2.imread("C:/Users/ma/Dropbox/FYP/sofopy/frames/frame99.jpg",cv2.IMREAD_GRAYSCALE)
cimage=cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
cv2.namedWindow("image",cv2.WINDOW_NORMAL)
cv2.imshow("image",cimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2019-07-09 03:02:39 -0600

Witek gravatar image

You just did that. But if you expected to (magically) recover original colors, you were wrong. The grayscale image has no information about the colors, but only their weighted average values so it is impossible to tell what the original three color components were, as more than one combination of these will result in the same averaged value. So, your result image has three color channels, but each of them has the same value, so the image still appears as grayscale. However you can draw a colored circle in this image, for example, as opposed to the grayscale one. There are methods for coloring grayscale images based on deep networks and although the results will look realistic, these methods cannot retrieve original colors either. https://www.learnopencv.com/convoluti...

edit flag offensive delete link more
0

answered 2019-07-09 02:51:06 -0600

berak gravatar image

you can't.

converting a color image to grayscale only keeps the luminance information, all color gets discarded (forever !).

in your case, you probably should read in a color image, keep it around, and work on a converted grayscale copy:

bgr = cv2.imread("C:/Users/ma/Dropbox/FYP/sofopy/frames/frame99.jpg", cv2.IMREAD_COLOR)   
gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)

p.s. there are cnn's nowadays, that try to "guess" the original color from a grayscale image, but that's another story ...

edit flag offensive delete link more

Comments

what's the difference between BGR and RGB?

akramnarejo gravatar imageakramnarejo ( 2019-07-09 03:34:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-07-09 02:35:58 -0600

Seen: 15,138 times

Last updated: Jul 09 '19