YousriD
YousriD

Reputation: 93

extract one channel image from RGB image with opencV

I'm working with QT and OpenCV, I have this square that I need to extract but I need to use conversion from RGB to one channel (RED basically). Any advice will be more than welcome, please feel free to advice which functions to use. Thanks in advance.

Upvotes: 6

Views: 35264

Answers (4)

Nikola Obreshkov
Nikola Obreshkov

Reputation: 1748

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 .

Upvotes: 17

Unapiedra
Unapiedra

Reputation: 16197

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

Upvotes: 12

dantswain
dantswain

Reputation: 5467

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).

Upvotes: 12

C--
C--

Reputation: 16558

As far as I know a call to,

cvtColor(src, bwsrc, CV_RGB2GRAY);

Can do that, where src is the multi channel source image and the third parameter represents the number of channels in the destination. So, you can do that in OpenCV and display the image on your Qt interface.

On the other hand, you can split the channels into separate single channel arrays using appropriate split() method.

http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#split

Upvotes: 2

Related Questions