SobelFilter, different results from C and OpenCL Implementations

Hello Everyone!
I am now to OpenCL.

I tried to use OpenCL to implement the Sobel Filter. It does the work to detect the edge, but it still has problems.

The image below is OpenCL version, it looks distracted in pixles. Therefore, I tried to verify it in C.
[ATTACH=CONFIG]80[/ATTACH]
The following one is C version. It works perfectly. I used the same code in these two implementations.
[ATTACH=CONFIG]81[/ATTACH]
For more details. When the OpenCL version is zoomed in. There are square patterns, which may be the clue to solve the problem.
[ATTACH=CONFIG]82[/ATTACH]
Also, for the implementation of the kernel, I found that the results are also different if different data types (i.e. float or int) are used.

Any expert can help me and tell me where might go wrong?

Thank you in advance!!!

It looks for me you are using different color spaces, though I’m not sure Sobel is even designed to work for non-grayscale images. This will be easier if you post some code of both implementations. You don’t alter the image in place, right?

I didn’t alter it. They are identical. I also detected the edge between black and white area. On the edge, there are four colors (green, red, dark blue and blue). And they are all vertical.
ps. Do you know how to edit the original post, so that I can add part of the code. I cannot find the button.
[ATTACH=CONFIG]83[/ATTACH]

You can’t edit your posts after 15 mins or so.

I didn’t alter it.

No, I mean, do you overwrite the contents of your image, or do you have an input buffer and an output buffer? The first option will bring you incorrect results.

I have two buffers. So I don’t think it was caused by overwrite. :slight_smile:

Good for you, but I still cannot tell anything for sure without a code. :slight_smile:

Ah, Sorry, I forgot. (the 3 in the code is because RGB information is read one by one seperately)
__kernel void SobelFilter(__global float* src, __global float* dst, int width, int height)
{

int x = (int)get_global_id(0);
int y = (int)get_global_id(1);

int W = width; 
int H = height;

if(x > 2 && x < 3*W - 2 && y > 2 && y < H - 2){
    int p00 = (int)src[(x-3)+3*W*(y-1)];
    int p10 = (int)src[( x )+3*W*(y-1)];
    int p20 = (int)src[(x+3)+3*W*(y-1)];
    int p01 = (int)src[(x-3)+3*W*( y )];
    int p21 = (int)src[(x+3)+3*W*( y )];
    int p02 = (int)src[(x-3)+3*W*(y+1)];
    int p12 = (int)src[( x )+3*W*(y+1)];
    int p22 = (int)src[(x+3)+3*W*(y+1)];

    int gx = -p00 + p20 + 2*(p21 - p01) - p02 + p22;
    int gy = -p00 - p20 + 2*(p12 - p10) + p02 + p22;

    int g = sqrt(gx*gx+ gy*gy);

    dst[x+3*W*y] = (float)g;
 }
 
 else{
     dst[x+3*W*y] = 0.0f;
 }

}