1 | initial version |
You are splitting each channel into a new image, but don't expect your pictures will be red, green or blue, they all will be in grayscale. It's because you are using a single channel to display them (which seems reasonable...). If you want to display the red channel as red, the blue as blue and the green as green, you have to create a 3-channels image, for each of them, and copy the single channel extracted into the right channel (the other channels remain at 0). See the function mixChannels for that.
cv::Mat red = cv::Mat::zeros(im.rows, im.cols, CV_8UC3 );
cv::Mat green = cv::Mat::zeros(im.rows, im.cols, CV_8UC3 );
cv::Mat blue = cv::Mat::zeros(im.rows, im.cols, CV_8UC3 );
cv::Mat channels[] = { red, green, blue };
int from_to[] = {0,2, 1,4, 2,6 };
cv::mixChannels( &im, 1, channels, 3, from_to, 3);
You create 3 new images (one for each channel), and you copy the origin image (called 'im') into each corresponding channel: im[0] -> red[2], im[1] -> green[1], im[2] -> blue[0]. Channels are continuous, so green[1] is (red.channels()-1) + 2
(red.channels() is the blue channel of green image, does it make sense? Have a look at the documentation, and make some tries...)