Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract one channel image from RGB image with opencV

Tags:

opencv

qt

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.

like image 399
YousriD Avatar asked Jan 28 '12 15:01

YousriD


2 Answers

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

like image 151
dantswain Avatar answered Nov 13 '22 03:11

dantswain


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

like image 45
Unapiedra Avatar answered Nov 13 '22 03:11

Unapiedra