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.
__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 nPixel = src(py, px);
float blue = (float)nPixel.x / 255.0;
float green = (float)nPixel.y / 255.0;
float red = (float)nPixel.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 /= 0.950456;
float y3 = exp(log(y) / 3.0);
z /= 1.088754;
float l, a, b;
x = x > 0.008856 ? exp(log(x) / 3.0) : (7.787 * x + 16/116);
y = y > 0.008856 ? y3 : 7.787 * y + 16/116;
z = z > 0.008856 ? z /= exp(log(z) / 3.0) : (7.787 * z + 16/116);
l = y > 0.008856 ? (116.0 * y3 - 16.0) : 903.3 * y;
a = (x - y) * 500.0;
b = (y - z) * 200.0;
uchar3 fPixel;
fPixel.x = static_cast<uchar>(l * 255 / 100) ;
fPixel.y = static_cast<uchar>(a + 128);
fPixel.z = static_cast<uchar>(b + 128);
dst(py, px) = fPixel;
}
}