Ask Your Question

What's the best way to create a 3-channel grayscale from BGR?

asked 2015-06-17 11:52:30 -0500

AwfulWaffle gravatar image


I'm trying to implement a generic algorithm that works with images in fourier space. Grayscale images are normally mxn while bgr/rgb are mxnx3 for each of the color channels. I've tried calling cvtColor in python with dstCn=3, but this still produces a mxn matrix. Is there an opencv/pythonic way to create an mxnx3 grayscale matrix from bgr format?


edit retag flag offensive close merge delete



First, you must understand that a MxNx3 in greyscale doesn't exist. I mean, the concept of greyscale is that you have one channel describing the intensity on a gradual scale between black and white. So, it is not clear why would you need a 3 channels greyscale image, but if you do, I suggest that you take the value of each pixel of your 1 channel greyscale image and that you copy it three times, one on each channel of a BGR image. When a BGR image has the same value on each channel, it appears to be grey. I don't program in Python so I can't provide a direct code...

Doomb0t gravatar imageDoomb0t ( 2015-06-17 15:25:29 -0500 )edit

Thanks. That was the answer I was looking for. I'm just trying to make some image code compatible with n-dimensional matrices, as odd as that might seem.

AwfulWaffle gravatar imageAwfulWaffle ( 2015-06-17 15:41:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-17 19:05:52 -0500

unxnut gravatar image

updated 2015-06-17 19:21:12 -0500

There are instances when you want to change a grayscale image into color. In my case, I am working with an ROI on which I found contours. The contours are grayscale image (actually just binary but appear as 8-bit). I want to superimpose the contours into the original color image. So, I use the C++ command to convert cv::cvtColor ( contours, display.operator()(contour_area), CV_GRAY2RGB );. The equivalent of cvtColor in python is cv2.cvtColor.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-17 11:51:37 -0500

Seen: 5,576 times

Last updated: Jun 17 '15