# BGR to Lab conversion issue

Hi all, I wrote the code for RGB to lab color conversion for cuda by using mathematical formulae which are mentioned in the OpenCV documentation but when I compare the l ,a ,b values from the cvColor function and my cuda function, there is a difference of 5 to 15. It will be great, if you suggest me the way. I have attached screenshot.

Gpu labframe = conversion by using mathematical equations. Cpu labframe = conversion by using cvtcolor function

__global__ void kRgb2CIELab(PtrStepSz<uchar3> src, PtrStepSz<uchar3> dst, int width, int height) {

```
int px = blockIdx.x * blockDim.x + threadIdx.x;
int py = blockIdx.y * blockDim.y + threadIdx.y;
if (px < width && py < height)
{
uchar3 pixel = src(py, px);
float blue = (float)pixel.x / 255.0;
float green = (float)pixel.y / 255.0;
float red = (float)pixel.z / 255.0;
float x = red * 0.412453 + green * 0.357580 + blue * 0.180423;
float y = red * 0.212671 + green * 0.715160 + blue * 0.072169;
float z = red * 0.019334 + green * 0.119193 + blue * 0.950227;
x = x / 0.950456;
z = z / 1.088754;
float l, a, b;
float fx = x > 0.008856 ? cbrt(x) : (7.787 * x + 16. / 116.);
float fy = y > 0.008856 ? cbrt(y) : 7.787 * y + 16. / 116.;
float fz = z > 0.008856 ? cbrt(z) : (7.787 * z + 16. / 116.);
l = y > 0.008856 ? (116.0 * cbrt(y) - 16.0) : 903.3 * y;
a = (fx - fy) * 500.0;
b = (fy - fz) * 200.0;
uchar3 uPixel;
uPixel.x = static_cast<uchar>(l * 255. / 100.);
uPixel.y = static_cast<uchar>(a + 128);
uPixel.z = static_cast<uchar>(b + 128);
dst(py, px) = uPixel;
}
```

}