Monday, November 5, 2007

Programs For Medical Image Processing in Visual Basic 6

Note: I was surprised to see such a old blog of mine still getting traffic from google and VB forums. Do note that the executables are on the left box, source code still compiles !!
You may like my new site and blog
Aaditya Prakash
Blog of Aaditya



Edge Detection By Prewitts in VB


Private Sub Command1_Click()
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
Picture3.ScaleMode = 3
Picture4.ScaleMode = 3
Dim a(256, 256) As Long
Dim b(256, 256) As Long
Dim c(256, 256) As Long
Dim d(256, 256) As Long
Dim l, f As Long
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
Next j
Next i
For i = 1 To 253
For j = 1 To 253
l = 0
f = 0
' X direction
For k = i To i
For m = j To j + 2
l = l + a(i + 2, m)
f = f + a(i, m)
Next m
b(i + 1, j + 1) = Abs(l - f)
Next k
l = 0
f = 0
'Y direction
For k = i To i + 2
For m = j To j
l = l + a(k, j + 2)
f = f + a(k, j)
Next m
c(i + 1, j + 1) = Abs(l - f)
Next k
Next j
Next i
'X-Y Direction
For i = 1 To 256
For j = 1 To 256
d(i, j) = b(i, j) + c(i, j)
Next j
Next i
For i = 0 To 256
For j = 0 To 256
Picture2.PSet (i, j), b(i, j)
Picture3.PSet (i, j), c(i, j)
Picture4.PSet (i, j), d(i, j)
Next j
Next i
End Sub



Edge Detection By Sobel  in VB


Private Sub Command1_Click()
Dim a(256, 256) As Long
Dim b(256, 256) As Long
Dim c(256, 256) As Long
Dim d(256, 256) As Long
Dim a1(256, 256) As Long
Dim a2(256, 256) As Long
Dim a3(256, 256) As Long
Dim l, f As Long
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
Picture3.ScaleMode = 3
Picture4.ScaleMode = 3
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
Next j
Next i
For i = 1 To 250
For j = 1 To 250
l = 0
f = 0
' x direction
l = a(i + 2, j) + 2 * a(i + 2, j + 1) + a(i + 2, j + 2)
'MsgBox (f)
f = a(i, j) + 2 * a(i, j + 1) + a(i, j + 2)
b(i + 1, j + 1) = Abs(l - f)
Next j
Next i
For i = 1 To 253
For j = 1 To 253
l = 0
f = 0
' y direction
l = a(i, j + 2) + 2 * a(i + 1, j + 2) + a(i + 2, j + 2)
f = a(i, j) + 2 * a(i + 1, j) + a(i + 2, j)
c(i + 1, j + 1) = Abs(l - f)
Next j
Next i
For i = 1 To 256
For j = 1 To 256
d(i, j) = b(i, j) + c(i, j)
Next j
Next i
For i = 1 To 256
For j = 1 To 256
Picture2.PSet (i, j), b(i, j)
Picture3.PSet (i, j), c(i, j)
Picture4.PSet (i, j), d(i, j)
Next j
Next i
End Sub


Median Filtering in VB

Private Sub Command1_Click()
Dim a(256, 256) As Long
Dim b(256, 256) As Long
Dim ii, jj As Integer
Dim k, m, l As Integer
Dim total, avg As Long
Dim sum(9) As Long
Dim i, j As Integer
Dim t, med As Long
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
Next j
Next i
For i = 0 To 253
For j = 0 To 253
total = 0
l = 0
For k = i To i + 2
For m = j To j + 2
l = l + 1
sum(l) = a(k, m)
total = total + sum(l)
Next m
Next k
For ii = 1 To l - 1
For jj = ii + 1 To l
If (sum(ii) > sum(jj)) Then
t = sum(ii)
sum(ii) = sum(jj)
sum(jj) = t
End If
Next jj
Next ii
med = sum(5)
For k = i To i + 2
For m = j To j + 2
If (a(k, m) < med) Then
b(k, m) = med
Else: b(k, m) = a(i, j)
End If
Next m
Next k
Next j
Next i
For i = 1 To 256
For j = 1 To 256
Picture2.PSet (i, j), b(i, j)
Next j
Next i
MsgBox ("Done")
End Sub



Average Filtering  in VB


Private Sub Command1_Click()
Dim a(256, 256) As Long
Dim b(256, 256) As Long
Dim k, m, l As Integer
Dim total, avg As Long

Dim i, j As Integer
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
Next j
Next i
For i = 0 To 253
For j = 0 To 253
total = 0
l = 0
For k = i To i + 2
For m = j To j + 2
l = l + 1
total = total + a(k, m)
Next m
Next k
avg = total / l
For k = i To i + 2
For m = j To j + 2
If (a(k, m) < avg) Then
b(k, m) = avg
Else: b(k, m) = a(i, j)
End If
Next m
Next k
Next j
Next i
For i = 1 To 256
For j = 1 To 256
Picture2.PSet (i, j), b(i, j)
Next j
Next i
MsgBox ("Done")
End Sub



Negative of Image  in VB


Private Sub Command1_Click()
Dim a(256, 256) As Long
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
Dim i, j As Integer
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
a(i, j) = 16777215 - a(i, j)
Picture2.PSet (i, j), a(i, j)
Next j
Next i
End Sub



Conversion of Image to pixel  in VB



Private Sub command1_click()
Dim a(256, 256) As Long
Picture1.ScaleMode = 3
Open "C:\Documents and Settings\student\My Documents\output.txt" For Output As #1
Dim i, j As Integer
For i = 0 To 256
For j = 0 To 256
a(i, j) = Picture1.Point(i, j)
Write #1, a(i, j)
Next j
Next i
Close #1
MsgBox ("Done")
End Sub



Conversion of pixel of image  in VB


Private Sub command1_click()

Dim a(256, 256) As Long
Picture1.ScaleMode = 3
Open "C:\Documents and Settings\student\My Documents\output.txt" For Input As #1
Dim i, j As Integer
For i = 0 To 256
For j = 0 To 256
Input #1, a(i, j)
Picture1.PSet (i, j), a(i, j)
Next j
Next i
Close #1
MsgBox ("Done")
End Sub

3 comments:

Adi said...

For any doubts pleas feel free to leave the comment here.. or email me at iam@aditya.com

Madhusudan Pareek said...

hi aditya.
i m final year bioinfo student in BIHER. i was expecting something good on java over here as the name of the blog suggest.

shirleywhirley said...

Great article about image processing with vb. Well written and very useful. Thanks a lot.

Check this out too:
Medisoft Program