Ask Your Question
0

BGR to Lab conversion issue

asked 2018-12-11 08:39:42 -0600

vps gravatar image

updated 2019-01-16 06:58:49 -0600

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;
}

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-01-15 12:52:53 -0600

Does the exact same code work in a loop in C++? I had a quick look and from the docs should

z = z > 0.008856 ? z /= exp(log(z) / 3.0) : (7.787 * z + 16/116);

be

z = z > 0.008856 ? exp(log(z) / 3.0) : (7.787 * z + 16/116);
edit flag offensive delete link more

Comments

I have updated the code in question. Still, results are not matching to opencv cvtcolor function.

vps gravatar imagevps ( 2019-01-16 06:50:32 -0600 )edit

Does the code give the same result in a loop on the CPU? I am asking because it seems unlikely to be a CUDA problem as you are processing each pixel in its own thread using a standard Map idiom. That is there is no shared memory usage, thread cooperation or synchronization so I would expect it to give similar results on both the CPU and GPU.

cudawarped gravatar imagecudawarped ( 2019-01-16 07:36:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-11 08:39:42 -0600

Seen: 763 times

Last updated: Jan 16 '19