Search code examples
image-processingconvolutionedge-detection

Does the position of the pixels in an image govern the edge detection?


Referring this video by andrew ng https://youtu.be/XuD4C8vJzEQ?list=PLkDaE6sCZn6Gl29AoE31iwdVwSG-KnDzF

From this video I conclude that for detecting vertical edges in an image there should be some BRIGHTER followed by DARKER regions starting from the left side, then only this [[1,0,-1],[1,0,-1],[1,0,-1]] will act as a vertical edge detector otherwise not.

Is my conclusion correct ?

and

Is the vice versa will also be true ?


Solution

  • If you think about the filter:

    1 0 -1
    1 0 -1
    1 0 -1
    

    you will see that it is just subtracting the pixels to the right from the pixels to the left at each location, i.e. finding the horizontal differences.

    As such, it is capable of finding transitions from light to dark and dark to light, it's just that the differences will show up with an opposite sign (plus or minus). So, if you transition from a bright area on the left to a darker area on the right, you will have a large number (bright) minus a small number (dark) and the difference will be positive. Conversely, if you transition from a dark area on the left (small number) to a brighter area on the right (large number), you will end up with a negative difference.

    Here is an example, just done in Terminal with ImageMagick. Start with this image:

    enter image description here

    Apply the filter you are talking about:

    magick input.png -morphology convolve '3x3: 1,0,-1 1,0,-1 1,0,-1' result.png
    

    enter image description here

    And you can see it finds only dark-to-light edges.

    If you want to detect edges from light to dark and dark to light, you need to either:

    • use a signed number (as opposed to unsigned) so you can hold negative results, or
    • add a "bias" to your convolution.

    If your data was unsigned 8-bit, you could add a 50% bias by dividing all your current values by 2 and adding 127 before convolving, for example.

    So, applying a bias, your filter now finds dark-to-light and light-to-dark edges:

    magick input.png -define convolve:scale='50%!' -bias 50% -morphology convolve '3x3: 1,0,-1 1,0,-1 1,0,-1' result.png
    

    enter image description here


    If you now want to detect horizontal edges transitioning from light-to-dark, rotate the filter to this:

    -1 -1 -1
     0  0  0
     1  1  1
    

    And apply:

    magick input.png -morphology convolve '3x3: -1,-1,-1 0,0,0 1,1,1' result.png
    

    enter image description here

    Or, if you want to find horizontal edges transitioning from dark-to-light, use:

     1  1  1
     0  0  0
    -1 -1 -1
    
    magick input.png -morphology convolve '3x3: 1,1,1 0,0,0 -1,-1,-1' result.png
    

    enter image description here

    And the same again, but with a bias so we can find both light-to-dark and dark-to-light transitions in one fell swoop:

    magick image.png -define convolve:scale='50%!' -bias 50% -morphology convolve '3x3: -1,-1,-1 0,0,0 1,1,1' result.png
    

    enter image description here


    Anthony Thyssen provides more excellent information about convolution than you could ever hope to need in a very approachable style here.