Weird results for image subtraction

Hi, I’m a complete beginner concerning cuda. I wrote two basic programs that do pixel by pixel subtraction between to images (ie from a movie), one with opencv and the other with cuda, to compare their speed. However, I have some weird results concerning the one in cuda :


To compare, with opencv, I get this :


Here is my kernel :



texture<float, 2, cudaReadModeElementType> tex1; // First frame loaded as a texture

texture<float, 2, cudaReadModeElementType> tex2; // Second one

__global__ void

diffKernel( float* g_odata, int width, int height) 


  Â unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;

 Â  Â unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;

  Â g_odata[y*width + x] = tex2D(tex1, x, y)-tex2D(tex2, x, y);



Thank you in advance for your help.

(PS : Sorry for my poor English)

Wow, that’s odd. Have you tried, for comparison, to read out of regular float arrays instead of the texture? Just to check the data.

Add an fabsf statement, otherwise you will get negative values.

Good catch

Thanks a lot ! (I can’t beleive I spent that much time looking for the problem without seeing that !!!)

I got that error so many times. :P
It’s very common, you just have to learn how to recognize it.
Actually your image displayed the correct result, but whereas it got negative values it produces an overflow, hence causing high intensity pixels to be displayed in place of the correct result.

Hi…am trying to bring in images into cuda program…seems you have done it so well…can you provide me help in how you did your image subtraction using cuda…