Sobel edge-detection, weird output


I´m trying to implement a Sobel algorithm for edge-detection for a YUV camera stream. Initially it seems quite easy but I´m not sure if this approach is correct:

  • I´m applying the filter just to the Y pixel component and doing U and V = 0 (black and white image).
  • After, and in order to check the result, I´m sending frames through the serial port, but before I convert the image from YUV to jpg.

The black and white image works perfectly and I can see it on the PC application which I wrote, but when I´m applying the Sobel filter to Y component I´m getting this: Sobel output

the code:

    #define index(xx, yy)  ((yy * width + xx) * 2) & 0xFFFFFFFE  // address multiple of 2


for (y=1, y < height-1; y++){
    for (x=1, y < width-1; y++){
      for (j= -1; j<2; j++){
         for (i= -1; i<2; i++){

            offset= index(x+1, y+1);
            pixel_valueY_h += (sobel_h[j + 1][i + 1])* input[offset+1]; //offset+1=> Y component
            pixel_valueY_v += (sobel_v[j + 1][i + 1])* input[offset+1]; 

        offset = index(x,y);
        pixel_value= sqrt1((pixel_valueY_h * pixel_valueY_h)+(pixel_valueY_v * pixel_valueY_v));

        if (pixel_value > 255) pixel_value=255;
        if (pixel_value < 0) pixel_value=0;

        //output frame
        output[offset] &=0x00; //U and V components = 0
        output[offset+1] &=(255- (unsigned char)pixel_value );


Any clue about what is happening? Thanks in advance.

asked on Stack Overflow Jul 5, 2016 by joe • edited Jul 6, 2016 by joe

1 Answer


Finally I've got it working, the problem was the memory addressing using the macro: #define index(xx, yy) ((yy * width + xx) * 2) & 0xFFFFFFFE which for some reason was giving incorrect addresses. Instead I added the line ( ((yy * width + xx) * 2) & 0xFFFFFFFE) in the code and like that (without modifications) is working perfectly.


answered on Stack Overflow Jul 6, 2016 by joe

User contributions licensed under CC BY-SA 3.0