Altera_Forum
Honored Contributor
10 years agoSobelFilter, different results from C and OpenCL Implementations
Hello everyone,
I tried to write an OpenCL code to implement the Sobel Filter. But I got strange results. It does detect the edge, but it looks like a distracted version. http://www.alteraforum.com/forum/attachment.php?attachmentid=10933&stc=1 Then I tried to verfy it in C with the same code (expect for loop, etc.) and got a perfect result. http://www.alteraforum.com/forum/attachment.php?attachmentid=10934&stc=1 The OpenCL version seems to be distracted in pixels and is slightly larger than the C version. Also I got different results when I used differnet data type (float ot int) in kernel. There is a detail when see the OpenCL version in detail. There are a lot of square patterns, which maybe a clue to solve the problem. http://www.alteraforum.com/forum/attachment.php?attachmentid=10935&stc=1 ==========================UPDATE============================ The Kernel code: (the 3 in the code is because for each pixel, 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;
int p10 = (int)src;
int p20 = (int)src;
int p01 = (int)src;
int p21 = (int)src;
int p02 = (int)src;
int p12 = (int)src;
int p22 = (int)src;
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 = (float)g;
}
else{
dst = 0.0f;
}
}
Can any expert help me and tell where may go wrong? Thank you in advance!