//=========================================================================== /// RGB2LAB //=========================================================================== void SLIC::RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval) { //------------------------ // sRGB to XYZ conversion //------------------------ double X, Y, Z; RGB2XYZ(sR, sG, sB, X, Y, Z); //------------------------ // XYZ to LAB conversion //------------------------ double epsilon = 0.008856; //actual CIE standard double kappa = 903.3; //actual CIE standard double Xr = 0.950456; //reference white double Yr = 1.0; //reference white double Zr = 1.088754; //reference white double xr = X/Xr; double yr = Y/Yr; double zr = Z/Zr; double fx, fy, fz; if(xr > epsilon) fx = pow(xr, 1.0/3.0); else fx = (kappa*xr + 16.0)/116.0; if(yr > epsilon) fy = pow(yr, 1.0/3.0); else fy = (kappa*yr + 16.0)/116.0; if(zr > epsilon) fz = pow(zr, 1.0/3.0); else fz = (kappa*zr + 16.0)/116.0; lval = 116.0*fy-16.0; aval = 500.0*(fx-fy); bval = 200.0*(fy-fz); }
void RGB2Lab3ub( unsigned char R, unsigned char G, unsigned char B, unsigned char& L, unsigned char& a, unsigned char& b ) { float x, y, z; float lt, at, bt; RGB2XYZ(R, G, B, x, y, z); XYZ2Lab(x, y, z, lt, at, bt); L = lt; a = at + 128; b = bt + 128; }
void RGB2LUVf(int R, int G, int B, float& L, float& U, float& V, bool scaled) { float X, Y, Z; RGB2XYZ(R, G, B, X, Y, Z); if (Y > 0.008856) L = 116.0f * pow(Y, 1.0f / 3.0f) - 16.0; else L = 903.3f * Y; double sum = X + 15 * Y + 3 * Z; if(sum != 0) U = 4 * X / sum, V = 9 * Y / sum; else U = 4.0, V = 9.0 / 15.0; U = 13 * L * (U - 0.19784977571475); V = 13 * L * (V - 0.46834507665248); }
CImage *CImageRGBtoXYZ(CImage *cimg) { CImage *ncimg=NULL; int p,n,i; ncimg = CreateCImage(cimg->C[0]->ncols,cimg->C[0]->nrows); n = ncimg->C[0]->ncols*ncimg->C[0]->nrows; for (p=0; p < n; p++){ i = triplet(cimg->C[0]->val[p],cimg->C[1]->val[p],cimg->C[2]->val[p]); i = RGB2XYZ(i); ncimg->C[0]->val[p]=t0(i); ncimg->C[1]->val[p]=t1(i); ncimg->C[2]->val[p]=t2(i); } return(ncimg); }
void RGB2Lab3f(int R, int G, int B, float& L, float& a, float& b) { float x, y, z; RGB2XYZ(R, G, B, x, y, z); XYZ2Lab(x, y, z, L, a, b); }
LchColor RGB2Lch(const Color& rgb) { XYZColor xyz = RGB2XYZ(rgb); LabColor lab = XYZ2Lab(xyz); return Lab2Lch(lab); }