Ask Your Question

Revision history [back]

How could I extract layers for differet color systems?

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;

if(image.isContinuous())
{
    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);