Why I get different results from a cpu and gsgl rendered sobel filter by using the same algorithm?

78 Views Asked by At

If I use this code:

QImage gradient_image(image_ptr->width(),image_ptr->height(),QImage::Format_ARGB32 );
//gradient_image.fill(Qt::red);

//sobel algorythm
double S_x [3] [3] = {{-1,0,1},{-2,0,2},{-1,0,[enter image description here][1]1}};
double S_y [3] [3] = {{-1,-2,-1},{0,0,0},{1,2,1}};
QPoint source_point(0,0);
double G_x = 0;
double G_y = 0;
int G = 0;
QColor source_color;
QRgb gradient_color;

for(int x= 0;x < image_ptr->width()-2;x++)
{

    for (int y = 0; y < image_ptr->height() - 2; y++)            
    {
    source_point.setX(x); source_point.setY(y);
    source_color = image_ptr->pixelColor(x,y);
    G_x = (S_x[0] [0] * image_ptr->pixelColor(x,y).red()) +
          (S_x [0] [1] * image_ptr->pixelColor(x+1,y).red()) +
          (S_x [0] [2] * image_ptr->pixelColor(x+2,y).red())+
          (S_x [1] [0] * image_ptr->pixelColor(x,y+1).red()) +
          (S_x [1] [1] * image_ptr->pixelColor(x+1,y+1).red()) +
          (S_x [1] [2] * image_ptr->pixelColor(x+2,y+1).red()) +
          (S_x [2] [0] * image_ptr->pixelColor(x,y+2).red()) +
          (S_x [2] [1] * image_ptr->pixelColor(x+1,y+2).red()) +
          (S_x [2] [2] * image_ptr->pixelColor(x+2,y+2).red());


    G_y =   (S_y[0] [0] * image_ptr->pixelColor(x,y).red()) +
            (S_y [0] [1] * image_ptr->pixelColor(x+1,y).red()) +
            (S_y [0] [2] * image_ptr->pixelColor(x+2,y).red())+
            (S_y [1] [0] * image_ptr->pixelColor(x,y+1).red()) +
            (S_y [1] [1] * image_ptr->pixelColor(x+1,y+1).red()) +
            (S_y [1] [2] * image_ptr->pixelColor(x+2,y+1).red()) +
            (S_y [2] [0] * image_ptr->pixelColor(x,y+2).red()) +
            (S_y [2] [1] * image_ptr->pixelColor(x+1,y+2).red()) +
            (S_y [2] [2] * image_ptr->pixelColor(x+2,y+2).red());

    G = (int) qSqrt((G_x * G_x) + (G_y * G_y));
    //std::cout << "G is: " << G;
    gradient_color = qRgb(G,G,G);
    gradient_image.setPixel(x,y,gradient_color);


    }

I get this result: enter image description here

I transfered the algorithm into gsgl:

uniform sampler2D   texture;
uniform float       width;
uniform float       height;
uniform float           threshold;
varying vec2 vTexCoord;


void main(void)
{
 mat3 Sx;
 mat3 Sy;
 Sx[0].xyz = vec3(1,0,-1);  Sx[1].xyz = vec3(2,0,-2); Sx[2].xyz = vec3(1,0,-1);
 Sy[0].xyz = vec3(1,2,1);   Sy[1].xyz = vec3(0,0,0);  Sy[2].xyz =vec3(-1,-2,-1);

 float w = 1.0 / width;
 float h = 1.0 / height;

 vec4 pos[9];
 pos[0] = texture2D(texture, vTexCoord);//pixel
 pos[1] = texture2D(texture, vTexCoord + vec2(w,0));//x+1 y
 pos[2] = texture2D(texture, vTexCoord + vec2(2*w,0));//x+2  y
 pos[3] = texture2D(texture, vTexCoord + vec2(0,h));//y+1
 pos[4] = texture2D(texture, vTexCoord + vec2(w,h));//x+1 und  y+1
 pos[5] = texture2D(texture, vTexCoord + vec2(2*w,h));//x+2 y+1
 pos[6] = texture2D(texture, vTexCoord + vec2(0,2*h));//x   y+2
 pos[7] = texture2D(texture, vTexCoord + vec2(w,2*h));//x+1 y+2
 pos[8] = texture2D(texture, vTexCoord + vec2(2*w,2*h));//x+2 y+2


 //vec3 c;//c = colour from texture pixel.
 //c = texture2D(texture,vTexCoord);

 float Gx;
 float Gy;

  Gx = (Sx[0].x   * pos[0].r) + (Sx[0].y  * pos[1].r) + (Sx[0].z * pos[2].r) +
       (Sx[1].x * pos[3].r )  + (Sx[1].y * pos[4].r )+   (Sx[1].z * pos[5].r )+
       (Sx[2].x * pos[6].r )  + (Sx[2].y * pos[7].r ) +  (Sx[2].z * pos[8].r )  ;

  Gy = (Sy[0].x   * pos[0].r) + (Sy[0].y  * pos[1].r) + (Sy[0].z * pos[2].r) +
          (Sy[1].x * pos[3].r )  + (Sy[1].y * pos[4].r )+   (Sy[1].z * pos[5].r )+
          (Sy[2].x * pos[6].r )  + (Sy[2].y * pos[7].r ) +  (Sy[2].z * pos[8].r )  ;

   float sobel = sqrt((Gx*Gx) + (Gy * Gy));

 
   gl_FragColor = vec4( sobel,sobel,sobel,1.0 );



}

The result is:

enter image description here

I want that the glsl result looks like the first (the cpu) result.

What was my mistake ?

0

There are 0 best solutions below