extract one channel image from RGB image with opencV

I think cvSplit is what you're looking for (docs). You can use it, for example, to split RGB into R, G, and B:

/* assuming src is your source image */
CvSize s = cvSize(src->width, src->height);
int d = src->depth;
IplImage* R = cvCreateImage(s, d, 1);
IplImage* G = cvCreateImage(s, d, 1);
IplImage* B = cvCreateImage(s, d, 1);
cvSplit(src, R, G, B, null);

Note you'll need to be careful about the ordering; make sure that the original image is actually ordered as R, G, B (there's a decent chance it's B, G, R).


Since this is tagged qt I'll give a C++ answer.

    // Create Windows
    namedWindow("Red",1);
    namedWindow("Green",1);
    namedWindow("Blue",1);

    // Create Matrices (make sure there is an image in input!)
    Mat input;
    Mat channel[3];

    // The actual splitting.
    split(input, channel);

    // Display
    imshow("Blue", channel[0]);
    imshow("Green", channel[1]);
    imshow("Red", channel[2]);

Tested on OpenCV 2.4.5


Another way is to use extractChannel:

cv::Mat channel;
//image is already loaded
cv::extractChannel(image, channel, 2);

This will extract the 3rd channel from image and save the result in channel. A particular channel can be extracted as well with extractImageCOI and mixChannels .

Tags:

Qt

Opencv