The purpose of this code is to implement an image processing filter using the filter matrix (with vb.net) I use as document the filter chapter of the GIMP documentation. (GIMP is actually the tool I usually use for some assignments in the webdesign).
here is the link: http://docs.gimp.org/en/plug-in-convmatrix.html
so it's actually not too difficult to implement. here the code:
'get the library
Imports System.Drawing.Bitmap
...
' Create a Bitmap object from a file.
Dim myBitmap As New Bitmap ("imagepath") ' example ("C:\whatever\ ...")
' Create a bitmap object result
Dim mybmpResult As Bitmap
'set kernel matrix (this very one is an edge detector)
'you can change the values to obtain a blur effect, or sharpen or whatever
'check the kernel values in the link
Dim k11, k12, k13, k21, k22, k23, k31, k32, k33 As Double
k11 = 0
k12 = -1
k13 = 0
k21 = -1
k22 = 3 'here I use 3 instead of 4
k23 = -1
k31 = 0
k32 = -1
k33 = 0
'Now it's actually a simple matrix calculation of the pixels in 2 loops
'You can check the link to see how it s done.
Dim Ycount As Integer
For Ycount = 1 To myBitmap.Height - 2
Dim Xcount As Integer
For Xcount = 1 To myBitmap.Width - 2
'you need 9 matrix elements, and for each of those,
'you need the RED, GREEN AND BLUE value
'to form 1 pixel
'values of first row in RGB
Dim x11R, x12R, x13R As Integer
Dim x11G, x12G, x13G As Integer
Dim x11B, x12B, x13B As Integer
'values of second row in RGB
Dim x21R, x22R, x23R As Integer
Dim x21G, x22G, x23G As Integer
Dim x21B, x22B, x23B As Integer
'values of third row in RGB
Dim x31R, x32R, x33R As Integer
Dim x31G, x32G, x33G As Integer
Dim x31B, x32B, x33B As Integer
'assign all those values according to the bitmap object
x11R = myBitmap.GetPixel(Xcount - 1, Ycount - 1).R
x11G = myBitmap.GetPixel(Xcount - 1, Ycount - 1).G
x11B = myBitmap.GetPixel(Xcount - 1, Ycount - 1).B
x12R = myBitmap.GetPixel(Xcount, Ycount - 1).R
x12G = myBitmap.GetPixel(Xcount, Ycount - 1).G
x12B = myBitmap.GetPixel(Xcount, Ycount - 1).B
x13R = myBitmap.GetPixel(Xcount + 1, Ycount - 1).R
x13G = myBitmap.GetPixel(Xcount + 1, Ycount - 1).G
x13B = myBitmap.GetPixel(Xcount + 1, Ycount - 1).B
'---
x21R = myBitmap.GetPixel(Xcount - 1, Ycount).R
x21G = myBitmap.GetPixel(Xcount - 1, Ycount).G
x21B = myBitmap.GetPixel(Xcount - 1, Ycount).B
x22R = myBitmap.GetPixel(Xcount, Ycount).R
x22G = myBitmap.GetPixel(Xcount, Ycount).G
x22B = myBitmap.GetPixel(Xcount, Ycount).B
x23R = myBitmap.GetPixel(Xcount + 1, Ycount).R
x23G = myBitmap.GetPixel(Xcount + 1, Ycount).G
x23B = myBitmap.GetPixel(Xcount + 1, Ycount).B
'---
x31R = myBitmap.GetPixel(Xcount - 1, Ycount + 1).R
x31G = myBitmap.GetPixel(Xcount - 1, Ycount + 1).G
x31B = myBitmap.GetPixel(Xcount - 1, Ycount + 1).B
x32R = myBitmap.GetPixel(Xcount, Ycount + 1).R
x32G = myBitmap.GetPixel(Xcount, Ycount + 1).G
x32B = myBitmap.GetPixel(Xcount, Ycount + 1).B
x33R = myBitmap.GetPixel(Xcount + 1, Ycount + 1).R
x33G = myBitmap.GetPixel(Xcount + 1, Ycount + 1).G
x33B = myBitmap.GetPixel(Xcount + 1, Ycount + 1).B
'matrix calculation of R
x22R = (x11R * k11 + x12R * k21 + x13R * k31) + (x21R * k12 + x22R * k22 + x23R * k32) + (x31R * k13 + x32R * k23 + x33R * k33)
'matrix calculation of G
x22G = (x11G * k11 + x12G * k21 + x13G * k31) + (x21G * k12 + x22G * k22 + x23G * k32) + (x31G * k13 + x32G * k23 + x33G * k33)
'matrix calculation of B
x22B = (x11B * k11 + x12B * k21 + x13B * k31) + (x21B * k12 + x22B * k22 + x23B * k32) + (x31B * k13 + x32B * k23 + x33B * k33)
'if it happens, that the calculation result is negativ or greater than 255, then
'keep it in the range
If x22R < 0 Then
x22R = 0
End If
If x22R > 255 Then
x22R = 255
End If
If x22G < 0 Then
x22G = 0
End If
If x22G > 255 Then
x22G = 255
End If
If x22B < 0 Then
x22B = 0
End If
If x22B > 255 Then
x22B = 255
End If
'then for each pixel result, set the result in the new bitmap object
mybmpResult.SetPixel(Xcount - 1, Ycount, Color.FromArgb(x22R, x22G, x22B))
'#############################################
Next Xcount
Next Ycount
'You can also view the result in a pictureBox, you just drag and drop in visual basic, and then
PictureBox1.Image = mybmp