Ask Your Question

How could I extract layers for differet color systems?

asked 2015-06-15 03:39:51 -0500

Jalal Jaberi gravatar image

Hi I am new to opencv. I wrote a program to extract RGB layers from an 3channels jpg image. Then I used cvtColor to convert RGB image ro other color systems (CIE XYZ, YCrCb, HSV, HLS, ...).

But when I used same algorithm (as RGB layer extraction) the result was wrong. for XYZ layers, I got something near RGB layers. for YCrCb I got 3 color layers (R, G, B). What is wrong with my code? How can I extract YCrCb, HSV, HLS and Lab* layers?

Here is my sample code for XYZ layers

cvtColor(orig_image, image, CV_RGB2XYZ);
Mat xlayer, ylayer, zlayer;
xlayer = image.clone();
ylayer = image.clone();
zlayer = image.clone();

unsigned int channels = image.channels();
unsigned int rows = image.rows;
unsigned int cols = image.cols * channels;
unsigned char *xrow, *yrow, *zrow;

    cols *= rows;
    rows = 1;
for(unsigned int i=0; i<rows; i++)
    xrow = xlayer.ptr<unsigned char>(i);
    yrow = ylayer.ptr<unsigned char>(i);
    zrow = zlayer.ptr<unsigned char>(i);
    for(unsigned int j = 0; j < cols; j+=3)
        xrow[j] = xrow[j+1] = 0;
        yrow[j] = yrow[j+2] = 0;
        zrow[j+1] = zrow[j+2] = 0;

namedWindow("X Layer Window", WINDOW_AUTOSIZE);
namedWindow("Y Layer Window", WINDOW_AUTOSIZE);
namedWindow("Z Layer Window", WINDOW_AUTOSIZE);
imshow("X Layer Window", xlayer);
imshow("Y Layer Window", ylayer);
imshow("Z Layer Window", zlayer);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-15 04:10:27 -0500

To extract each layer into a single image, use the function split. I should be faster (and hopefully bug-free...).

edit flag offensive delete link more


Thank you for your help. Using the split function gives me grayscale layers for all XYZ, YCrCb and HSV color systems. Is that true?

Jalal Jaberi gravatar imageJalal Jaberi ( 2015-06-15 04:27:18 -0500 )edit

It gives you each channel independently. Then, if you display them, they will look like grayscale images (only one channel) but it doesn't mean they are the 8U type if it's what you mean.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2015-06-15 07:57:40 -0500 )edit

Question Tools

1 follower


Asked: 2015-06-15 03:39:51 -0500

Seen: 153 times

Last updated: Jun 15 '15