Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/52c8441900d9b3391a55 to your computer and use it in GitHub Desktop.
Save lettergram/52c8441900d9b3391a55 to your computer and use it in GitHub Desktop.
/* version 2.4 of openCV */
using namespace cv;
/**
* Takes in a RGB image and outputs
* a Lab space image.
**/
Mat BGR2LAB(Mat const &imgRGB){
Mat imgLab;
cvtColor(imgRGB, imgLab, CV_RGB2Lab);
return imgLab;
}
/**
* Takes in image in the LAB space and splits
* out the a* stream. Returning it as a matrix,
* to be saved out to file or manipulated
**/
Mat alphaLAB(Mat const &imgLab){
Mat alphaLab;
cvtColor(imgLab, alphaLab, CV_Lab2RGB);
Size s = alphaLab.size();
int steps = imgLab.step;
int channels = imgLab.channels();
for(int y = 0; y < s.height; y++){
for(int x = 0; x < s.width; x++){
(*alphaLab.ptr<Point3_<uchar> >(y,x)).x = imgLab.data[steps*y + channels * x + 1];
(*alphaLab.ptr<Point3_<uchar> >(y,x)).y = imgLab.data[steps*y + channels * x + 1];
(*alphaLab.ptr<Point3_<uchar> >(y,x)).z = imgLab.data[steps*y + channels * x + 1];
}
}
return alphaLab;
}
/**
* Takes in image in the LAB space and splits
* out the b* stream. Returning it as a matrix,
* to be saved out to file or manipulated
**/
Mat betaLAB(Mat &imgLab){
Mat betaLab;
cvtColor(imgLab, betaLab, CV_Lab2RGB);
Size s = betaLab.size();
int steps = imgLab.step;
int channels = imgLab.channels();
for(int y = 0; y < s.height; y++){
for(int x = 0; x < s.width; x++){
(*betaLab.ptr<Point3_<uchar> >(y,x)).x = imgLab.data[steps*y + channels * x + 2];
(*betaLab.ptr<Point3_<uchar> >(y,x)).y = imgLab.data[steps*y + channels * x + 2];
(*betaLab.ptr<Point3_<uchar> >(y,x)).z = imgLab.data[steps*y + channels * x + 2];
}
}
return betaLab;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment