0

I'm trying to read an RGB 8-bit image, converting it into Lab and save it. The result is always interpreted as an RGB image and it's very strange (saturated).

I tried not to normalize values or to use RGB2Lab or to convert the images to 32FC but nothing works.

Mat image = imread(path);
cvtColor(image , image , COLOR_BGR2Lab);

Mat imageSplitted[3];
split(image, imageSplitted);
Mat* imageNormalized = normalizeLabValues(imageSplitted);
Mat imageMerged;
merge(imageNormalized, 3, imageMerged);
imwrite(newPathFiltered, imageMerged);

The normalizeLabValues is a function that provides to solve the problem of 8-bit images converted to Lab:

Mat* normalizeLabValues(Mat image[]) {
int rows = image[0].rows;
int cols = image[0].cols;

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        // L
        Scalar intensity = image[0].at<uchar>(i, j);
        Scalar normalized_intensity = intensity.val[0] * 100 / 256;
        image[0].at<uchar>(i, j) = normalized_intensity.val[0];

        // a
        intensity = image[1].at<uchar>(i, j);
        normalized_intensity = intensity.val[0] - 126;
        image[1].at<uchar>(i, j) = normalized_intensity.val[0];

        // b
        intensity = image[2].at<uchar>(i, j);
        normalized_intensity = intensity.val[0] - 126;
        image[2].at<uchar>(i, j) = normalized_intensity.val[0];
    }
}


 return image;
}

I'd like to obtain a Lab image but if I try to open the results with Photoshop it's RGB and highly saturated.

Davide577
  • 31
  • 5
  • What's the value of `newPathFiltered`? – Mark Setchell Jan 18 '19 at 16:37
  • string newPathFiltered = basePath + "compressed/" + to_string(i) + "/" + explode(fileName, '_')[0] + "_B" + to_string(intensity/10) + ".tif"; – Davide577 Jan 18 '19 at 18:16
  • AFAIK, TIFF format is unable to store **Lab** colourspace images, so it is going to interpret whatever you put in there as RGB. If you normalise images they will tend to occupy the full range so they will tend to apoear saturated. Maybe time to step back and re-think what you are trying to achieve. – Mark Setchell Jan 18 '19 at 19:08
  • @MarkSetchell: TIFF do not restrict to RGB. Every channel is handled separately, and one could save not just 1 or 3 channels, but also much more channels. If a program can interpret the coding of the channel is an other question. (Usually no: one will choose the image representation (and colours) via TIFF tools). – Giacomo Catenazzi Jan 19 '19 at 16:42

1 Answers1

0

It's not strange. imwrite saves everything as a RGB image on hard disk, that means your L channels would be considered as a R channel on hard disk (so in Photoshop too). For windows, IMAGE means RGB. If you want to save Lab images, you have to save it as a kind of a file, like txt, xml, yml, binary file and so on.

MeiH
  • 1,763
  • 11
  • 17