0

Good Day!

I can't seem to find the right terms for what I want done in OpenCV. Here's the situation:

I have a grayscale image and color (BGR).

I want to 'apply' the color to the grayscale image but keeping the luma and save it afterwards.

My original thought process was:

  1. Convert BGR color to Luv
  2. Replace L from grayscale image
  3. Convert final image to BGR and save

This is what I've done so far:

        cv::Mat pixelColor(1, 1, CV_8UC3, cv::Scalar(0));
        pixelColor.at<cv::Vec3b>(1, 1) = cv::Vec3b(128, 255, 0); // currently hard-coded but it actually comes from another source
        cv::cvtColor(pixelColor, pixelColor, CV_BGR2Luv);
        pixelColor.at<cv::Vec3b>(1, 1)[0] = image.at<unsigned char>(y, x);
        cv::cvtColor(pixelColor, pixelColor, CV_Luv2BGR);

I iterate over all y and x of my grayscale image. The actual color comes elsewhere but it's guaranteed to be BGR.

My questions are: (1) what's the proper term for this process? (2) where am I going wrong?

  • How is this working/not working? `(2)` asks 'where am I going wrong", however, you fail to specify what errors you are receiving. – Lilith Daemon Apr 18 '16 at 22:15
  • not errors, rather, not the whole color spectrum is properly placed. My specific case, i'm coloring a grayscale image using 3 colors. the criteria for colorization comes from a probability measure field which I already computed. depending on the color space, only 2 of the 3 colors are properly placed. sometimes, the details are lost. i'm not sure what i'm missing. – Francis Joseph Seriña Apr 19 '16 at 01:50

1 Answers1

0

I suppose you could refer to this process as channel swapping or channel replacing? You shouldn't need to explicitly iterate over the pixels in your images.

It would probably be easiest to split() your LUV image into a vector of Mats.

vector<Mat> channels;
cv::split(LUVMAT,channels);

This gives you 3, single channel Mats L, U, and V in your vector.

Then replace the 'L' Mat in the vector with the grayscale (single-channel) Mat using standard vector operations. Finally, merge the result back into a 3 channel Mat.

Mat newLuma;
cv::merge(temp,newLuma);

Then convert "newLuma" back to BGR.

BHawk
  • 2,382
  • 1
  • 16
  • 24
  • i will try that! that seems to be very logical. the reason why i'm doing it per pixel is because i select the based on a probability field (computed in a prior run). – Francis Joseph Seriña Apr 19 '16 at 01:51