Ask Your Question
3

how do I separate the channels of an RGB image and save each one, using the 2.4.9 version of OpenCV?

asked 2015-04-10 21:36:32 -0600

Roberto F. gravatar image

how do I separate the channels of an RGB image and save each one, using the 2.4.9 version of OpenCV?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2015-04-10 23:03:35 -0600

Haris gravatar image

updated 2016-04-22 23:38:18 -0600

Use Mat::split, which splits multi-channel image into several single-channel arrays.

Example:

Mat src = imread("img.png",CV_LOAD_IMAGE_COLOR); //load  image

Mat bgr[3];   //destination array
split(src,bgr);//split source  

//Note: OpenCV uses BGR color order
imwrite("blue.png",bgr[0]); //blue channel
imwrite("green.png",bgr[1]); //green channel
imwrite("red.png",bgr[2]); //red channel
edit flag offensive delete link more

Comments

1

Hi. Your answer definitely helped me, but I think there is a tiny mistake in the first line. The Mat should be called src, since afterwards you use src in split.

benT gravatar imagebenT ( 2016-04-22 12:08:51 -0600 )edit

You are right, thanks for noticing :). I will edit it.

Haris gravatar imageHaris ( 2016-04-22 23:37:29 -0600 )edit

The code really helped me. I just want to ask about end results of channels, they are really red, green and blue channels images like in matlab or in grayscale(three same images but different ray level)? I am asking this because I get the color of channels iin grayscale instead of red, green and blue channel image respectively, thanks in advance.

khatri.kelash gravatar imagekhatri.kelash ( 2017-01-24 08:46:22 -0600 )edit

You will get three grayscale images representing the colors in the original images. They should definitly not be the same images except if the original image was grayscale itself

benT gravatar imagebenT ( 2017-02-26 08:26:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-10 21:36:32 -0600

Seen: 107,365 times

Last updated: Apr 22 '16