This procedure supports grayscale and color images. In general, the steps are:
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 1a 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 1a 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:
|
|
| Original | After matrix convolution |
' 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 homepage | Victor Product Summary | more source code