Victor Image Processing Library How-to Tips

Edge Detection with Matrix Convolution

The Victor Library matrix convolution function makes it easy to perform edge detection in a Visual Basic application.

This procedure supports grayscale and color images. In general, the steps are:

Designing the Kernel

The kernel size can be any square dimensions from 3 x 3 up to 63 x 63 elements.

The kernel elements should be balanced and arranged to emphasize differences along the direction of the edge to be detected. For example, to detect horizontal lines use
-1  -1  -1
 0   0   0
 1   1   1
a 3 x 3 kernel
       
or
       
                          
-1  -1  -1  -1  -1  -1  -1
-2  -2  -2  -2  -2  -2  -2
-3  -3  -3  -3  -3  -3  -3
 0   0   0   0   0   0   0
 3   3   3   3   3   3   3
 2   2   2   2   2   2   2
 1   1   1   1   1   1   1
a 7 x 7 kernel

In this example we use a very simple kernel of

    -5   0   0
     0   0   0
     0   0   5
to emphasize the diagonal edges. And this is what it looks like:
edge detection
Original After matrix convolution

Notice that the most prominent lines are the diagonals -- horizontal and vertical are not as thick and bright.

Edge Detection with Matrix Convolution - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.

' Function Declaration ........................................... 
Declare Function matrixconvex Lib "VIC32.DLL" (ByVal ksize As Long, ByRef firstelement as byte, ByVal divsr As Long, srcimg As imgdes, resimg As imgdes) As Long

' The Function .................................................. 
Public Function diagonaledge(ByRef myimage As imgdes) As Long
Dim rcode As Long
Dim kernel(0 To 8) As Byte     ' 3 x 3 kernel
Dim kernelvalues As Variant
Dim j As Integer
Dim divisor As Long

divisor = 0

' Kernel values:
'    -5   0   0
'    0    0   0
'    0    0   5

kernelvalues = Array(-5, 0, 0, 0, 0, 0, 0, 0, 5)
    For j = 0 To 8
        If (kernelvalues(j) >= 0) Then
            kernel(j) = kernelvalues(j)
        Else
            kernel(j) = kernelvalues(j) + 256   ' Negative values have to be converted to corresponding positive values 
                                                '     -1 becomes 255 
        End If                                  '     -2 becomes 254, and so on 
        
        divisor = divisor + kernelvalues(j)
    Next j
        
    If (divisor = 0) Then divisor = 1           ' Make sure divisor is not zero  
    rcode = matrixconvex(3, kernel(0), divisor, myimage, myimage)
        
    diagonaledge = rcode

End Function


Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 1995 - 2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.