You are on page 1of 16

COMPUTER VISION - IMAGE PROCESSING

Reduksi Noise
Reduksi Noise dengan Filter Rata-rata
Desain Form

Listing program
Dim wt(400, 400) As Integer
Dim Nfilter As Integer
Dim h() As Single
Private Sub Command1_Click()
If Option1 Then
Nfilter = 3
ElseIf Option2 Then
Nfilter = 5
Else
Nfilter = 7
End If
ReDim h(Nfilter + 1, Nfilter + 1) As Single
'Randomize Timer
For i = 1 To Nfilter
For j = 1 To Nfilter
h(i, j) = 1 / (Nfilter ^ 2)
Next j
Next i
probNoise = Val(Text1)
dx = 0
sx = 0

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

n1 = 0
For i = 1 To Picture4.ScaleWidth Step 15
n1 = n1 + 1
n2 = 0
For j = 1 To Picture4.ScaleHeight Step 15
warna = Picture4.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)
b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)
wx = Int((r + g + b) / 3)
n2 = n2 + 1
wt(n1, n2) = wx
Picture4.PSet (i, j), RGB(wx, wx, wx)
'Pembangkitan Noise Gaussian
'Menggunakan metode Rejection
sw = 0
While sw = 0
x = 2 * Rnd - 1
y = Rnd
If y < Exp(-x ^ 2) Then
sw = x
End If
Wend
wx1 = Abs(wx + sw * 255 * probNoise)
If wx1 > 255 Then wx1 = 255
Picture2.PSet (i, j), RGB(wx1, wx1, wx1)
nx = nx + Abs(wx1 - wx)
sx = sx + Abs(wx)
Next j
Next i
snr = 10 * Log(sx / nx) / Log(10)
Label1.Caption = snr
End Sub

Private Sub Command2_Click()


Dim xt(400, 400) As Integer
If Option1 Then
Nfilter = 3
ElseIf Option2 Then
Nfilter = 5
Else
Nfilter = 7
End If
ReDim h(Nfilter + 1, Nfilter + 1) As Single
Randomize Timer
For i = 1 To Nfilter
For j = 1 To Nfilter
h(i, j) = 1 / (Nfilter ^ 2)
Next j
Next i
'RGB to
n1 = 0
For i =
n1 = n1
n2 = 0
For

Gray
1 To Picture4.ScaleWidth Step 15
+ 1
j = 1 To Picture4.ScaleHeight Step 15
warna = Picture2.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)


n2 = n2 + 1
wx = Int((r + g + b) / 3)
xt(n1, n2) = wx
Next j
Next i
'Proses Filter dengan Konvolusi
On Error Resume Next
nx = 0
sx = 0
m = Int(Nfilter / 2)
For i = 1 To n1
For j = 1 To n2
z = 0
For u1 = -m To m
For u2 = -m To m
If i + u1 < 0 Or j + u2 < 0 Then ft = 0 Else ft = xt(i + u1, j +
u2)
z = z + h(u1 + m + 1, u2 + m + 1) * ft
Next u2
Next u1
Picture3.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)
nx = nx + Abs(z - wt(i, j))
sx = sx + Abs(wt(i, j))
Next j
Next i
snr = 10 * Log(sx / nx) / Log(10)
Label2.Caption = snr
End Sub

Private Sub cmdClose_Click()


End
End Sub

Private Sub cmdOpen_Click()


CommonDialog1.Filter = "File Gambar(*.bmp)|*.bmp|" & _
"File Gambar(*.jpg)|*.jpg | Semua File Gambar(*.*)|" & _
"*.bmp ;*.jpg"
CommonDialog1.ShowOpen
namafile = CommonDialog1.FileName
Picture1.Picture = LoadPicture(namafile)
End Sub

Private Sub Command3_Click()


Picture2.Cls
Picture3.Cls
Picture4.Cls
Erase h
End Sub

Private Sub Command4_Click()


Dim xt(400, 400) As Integer
Randomize Timer
n1 = 0

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

For i = 1 To Picture1.ScaleWidth Step 15


n1 = n1 + 1
n2 = 0
For j = 1 To Picture1.ScaleHeight Step 15
warna = Picture1.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)
b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)
n2 = n2 + 1
wx = Int((r + g + b) / 3)
xt(n1, n2) = wx
Picture4.PSet (i, j), RGB(wx, wx, wx)
Next j
Next i
End Sub

Private Sub Form_Load()


Randomize Timer
End Sub

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

Reduksi Noise
Reduksi Noise dengan Filter Gaussian
Desain Form

Listing Program
Dim wt(400, 400) As Integer
Dim h() As Single
Dim Nfilter As Integer

Private Sub cmdClose_Click()


End
End Sub

Private Sub cmdOpen_Click()


CommonDialog1.Filter = "File Gambar(*.bmp)|*.bmp|" & _
"File Gambar(*.jpg)|*.jpg | Semua File Gambar(*.*)|" & _
"*.bmp ;*.jpg"
CommonDialog1.ShowOpen
namafile = CommonDialog1.FileName
Picture1.Picture = LoadPicture(namafile)
End Sub

Private Sub Command1_Click()


If Option1 Then
Nfilter = 3
ElseIf Option2 Then

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

Nfilter = 5
Else
Nfilter = 7
End If
ReDim h(Nfilter + 1, Nfilter + 1) As Single
Randomize Timer
'Nfilter = 7
s = 0.5
m = Int(Nfilter / 2) + 1
sh = 0
For i = 1 To Nfilter
t1 = (1 - m) / (m - 1)
For j = 1 To Nfilter
t2 = (j - m) / (m - 1)
h(i, j) = Exp(-(t1 ^ 2 + t2 ^ 2) / (2 * s ^ 2)) / (s ^ 2 * 3.14 ^ 0.5)
sh = sh + h(i, j)
Next j
Next i
For i = 1 To Nfilter
For j = 1 To Nfilter
h(i, j) = h(i, j) / sh
Next j
Next i
probNoise = Val(Text1)
dx = 0
sx = 0
n1 = 0
For i = 1 To Picture4.ScaleWidth Step 15
n1 = n1 + 1
n2 = 0
For j = 1 To Picture4.ScaleHeight Step 15
warna = Picture4.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)
b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)
wx = Int((r + g + b) / 3)
n2 = n2 + 1
wt(n1, n2) = wx
Picture4.PSet (i, j), RGB(wx, wx, wx)
'Pembangkitan Noise Gaussian
'Menggunakan metode Rejection
sw = 0
While sw = 0
x = 2 * Rnd - 1
y = Rnd
If y < Exp(-x ^ 2) Then
sw = x
End If
Wend
wx1 = Abs(wx + sw * 255 * probNoise)
If wx1 > 255 Then wx1 = 255
Picture2.PSet (i, j), RGB(wx1, wx1, wx1)
nx = nx + Abs(wx1 - wx)
sx = sx + Abs(wx)
Next j
Next i
snr = 10 * Log(sx / nx) / Log(10)
Label1.Caption = snr
End Sub

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

Private Sub Command2_Click()


Dim xt(400, 400) As Integer
If Option1 Then
Nfilter = 3
ElseIf Option2 Then
Nfilter = 5
Else
Nfilter = 7
End If
ReDim h(Nfilter + 1, Nfilter + 1) As Single
Randomize Timer
'Nfilter = 7
s = 0.5
m = Int(Nfilter / 2) + 1
sh = 0
For i = 1 To Nfilter
t1 = (i - m) / (m - 1)
For j = 1 To Nfilter
t2 = (j - m) / (m - 1)
h(i, j) = Exp(-(t1 ^ 2 + t2 ^ 2) / (2 * s ^ 2)) / (s ^ 2 * 3.14 ^ 0.5)
sh = sh + h(i, j)
Next j
Next i
For i = 1 To Nfilter
For j = 1 To Nfilter
h(i, j) = h(i, j) / sh
Next j
Next i
'RGB to Gray
m = Int(Nfilter / 2)
n1 = 0
For i = 1 To Picture4.ScaleWidth Step 15
n1 = n1 + 1
n2 = 0
For j = 1 To Picture4.ScaleHeight Step 15
warna = Picture2.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)
b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)
wx = Int((r + g + b) / 3)
n2 = n2 + 1
xt(n1, n2) = wx
Next j
Next i
'Proses Filter dengan Konvolusi
nx = 0
sx = 0
For i = 1 To n1
For j = 1 To n2
z = 0
For u1 = -m To m
For u2 = -m To m
If i + u1 < 0 Or j + u2 < 0 Then ft = 0 Else ft = xt(i + u1, j +
u2)
z = z + h(u1 + m + 1, u2 + m + 1) * ft 'xt(i + u1, j + u2)
Next u2
Next u1
Picture3.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)
nx = nx + Abs(z - wt(i, j))

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

sx = sx + Abs(wt(i, j))
Next j
Next i
snr = 10 * Log(sx / nx) / Log(10)
Label2.Caption = snr
End Sub

Private Sub Command3_Click()


Picture2.Cls
Picture3.Cls
Picture4.Cls
Erase h
End Sub

Private Sub Command4_Click()


Dim xt(400, 400) As Integer
Randomize Timer
n1 = 0
For i = 1 To Picture1.ScaleWidth Step 15
n1 = n1 + 1
n2 = 0
For j = 1 To Picture1.ScaleHeight Step 15
warna = Picture1.Point(i, j)
r = warna And RGB(255, 0, 0)
g = Int((warna And RGB(0, 255, 0)) / 256)
b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)
n2 = n2 + 1
wx = Int((r + g + b) / 3)
xt(n1, n2) = wx
Picture4.PSet (i, j), RGB(wx, wx, wx)
Next j
Next i
End Sub

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

Edge Detection (Deteksi Tepi)


Edge Detection Metode Robert
Desain Form

Listing program
Dim wx(500, 500) As Integer
Dim M As Long, N As Long

Private Sub cmdcitraBiner_Click()


Dim i As Long
Dim j As Long
Dim Warna As Long
Dim r As Long, g As Long, b As Long
Dim x As Long
picCapture.Cls
For i =
M =
N =
For

1 To picAsli.Width Step 15
M + 1
0
j = 1 To picAsli.Height Step 15
Warna = picAsli.Point(i, j)
r = Warna And RGB(255, 0, 0)
g = (Warna And RGB(0, 255, 0)) \ 256
b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256
N = N + 1
x = (r + g + b) \ 3
wx(M, N) = x
picCapture.PSet (i, j), RGB(x, x, x)

Next
Next
End Sub

David, S.Kom., M.Cs |

COMPUTER VISION - IMAGE PROCESSING

Private Sub cmdEdgeDetection_Click()


''Metode Robert
For i = 1 To M
For j = 1 To N
If i = 1 Then wx1 = wx(i, j) Else wx1 = wx(i, j) - wx(i - 1, j)
If j = 1 Then wx2 = wx(i, j) Else wx2 = wx(i, j) - wx(i, j - 1)
w = Abs(wx1) + Abs(wx2)
If w > 255 Then w = 255
picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(w, w, w)
Next
Next
End Sub

Private Sub cmdLoad_Click()


Dim namafile As String
CD.DialogTitle = "Loading Gambar"
CD.Filter = "Jpg file (*.jpg) |*.jpg"
CD.ShowOpen
namafile = CD.FileName
picAsli.Picture = LoadPicture(namafile)
Text1.Text = namafile
End Sub

David, S.Kom., M.Cs |

10

COMPUTER VISION - IMAGE PROCESSING

Edge Detection (Deteksi Tepi)


Edge Detection Metode Prewitt
Desain Form

Listing program
Dim
Dim
Dim
Dim

wx(500, 500) As Integer


M As Long, N As Long
h1(3, 3) As Single
h2(3, 3) As Single

Private Sub cmdcitraBiner_Click()


Dim i As Long
Dim j As Long
Dim Warna As Long
Dim r As Long, g As Long, b As Long
Dim x As Long
picCapture.Cls
For i =
M =
N =
For

1 To picAsli.Width Step 15
M + 1
0
j = 1 To picAsli.Height Step 15
Warna = picAsli.Point(i, j)
r = Warna And RGB(255, 0, 0)
g = (Warna And RGB(0, 255, 0)) \ 256
b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256
N = N + 1
x = (r + g + b) \ 3
wx(M, N) = x
picCapture.PSet (i, j), RGB(x, x, x)

Next
Next

David, S.Kom., M.Cs |

11

COMPUTER VISION - IMAGE PROCESSING

End Sub

Private Sub cmdEdgeDetection_Click()


''Metode Prewitt
For i = 1 To M
For j = 1 To N
z1 = 0
z2 = 0
For u1 = -1 To 1
For u2 = -1 To 1
z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
Next
Next
z = Int(Abs(z1 + z2))
If z > 255 Then z = 255
picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)
Next
Next
End Sub

Private Sub cmdLoad_Click()


Dim namafile As String
CD.DialogTitle = "Loading Gambar"
CD.Filter = "jpg file (*.jpg) |*.jpg"
CD.ShowOpen
namafile = CD.FileName
picAsli.Picture = LoadPicture(namafile)
Text1.Text = namafile
End Sub

Private Sub Form_Load()


h1(1, 1) = -1: h1(1, 2) = 0: h1(1, 3) = 1
h1(2, 1) = -1: h1(2, 2) = 0: h1(2, 3) = 1
h1(3, 1) = -1: h1(3, 2) = 0: h1(3, 3) = 1
For i = 1 To 3
For j = 1 To 3
h2(i, j) = h1(j, i)
Next
Next
End Sub

David, S.Kom., M.Cs |

12

COMPUTER VISION - IMAGE PROCESSING

Edge Detection (Deteksi Tepi)


Edge Detection Metode Sobel
Desain Form

Listing Program
Dim
Dim
Dim
Dim

wx(500, 500) As Integer


M As Long, N As Long
h1(3, 3) As Single
h2(3, 3) As Single

Private Sub cmdcitraBiner_Click()


Dim i As Long
Dim j As Long
Dim Warna As Long
Dim r As Long, g As Long, b As Long
Dim x As Long
picCapture.Cls
For i =
M =
N =
For

1 To picAsli.Width Step 15
M + 1
0
j = 1 To picAsli.Height Step 15
Warna = picAsli.Point(i, j)
r = Warna And RGB(255, 0, 0)
g = (Warna And RGB(0, 255, 0)) \ 256
b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256
N = N + 1
x = (r + g + b) \ 3
wx(M, N) = x
picCapture.PSet (i, j), RGB(x, x, x)

Next
Next

David, S.Kom., M.Cs |

13

COMPUTER VISION - IMAGE PROCESSING

End Sub

Private Sub cmdEdgeDetection_Click()


''Metode Sobel
For i = 1 To M
For j = 1 To N
z1 = 0
z2 = 0
For u1 = -1 To 1
For u2 = -1 To 1
z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
Next
Next
z = Int(Abs(z1 + z2))
If z > 255 Then z = 255
picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)
Next
Next
End Sub

Private Sub cmdLoad_Click()


Dim namafile As String
CD.DialogTitle = "Loading Gambar"
CD.Filter = "jpg file (*.jpg) |*.jpg"
CD.ShowOpen
namafile = CD.FileName
picAsli.Picture = LoadPicture(namafile)
Text1.Text = namafile
End Sub

Private Sub Form_Load()


h1(1, 1) = -1: h1(1, 2) = 0: h1(1, 3) = 1
h1(2, 1) = -2: h1(2, 2) = 0: h1(2, 3) = 2
h1(3, 1) = -1: h1(3, 2) = 0: h1(3, 3) = 1
For i = 1 To 3
For j = 1 To 3
h2(i, j) = h1(j, i)
Next
Next
End Sub

David, S.Kom., M.Cs |

14

COMPUTER VISION - IMAGE PROCESSING

Edge Detection (Deteksi Tepi)


Edge Detection Metode Filter Laplacian
Desain Form

Listing Program
Dim
Dim
Dim
Dim

wx(500, 500) As Integer


M As Long, N As Long
h1(3, 3) As Single
h2(3, 3) As Single

Private Sub cmdcitraBiner_Click()


Dim i As Long
Dim j As Long
Dim Warna As Long
Dim r As Long, g As Long, b As Long
Dim x As Long
picCapture.Cls
For i =
M =
N =
For

1 To picAsli.Width Step 15
M + 1
0
j = 1 To picAsli.Height Step 15
Warna = picAsli.Point(i, j)
r = Warna And RGB(255, 0, 0)
g = (Warna And RGB(0, 255, 0)) \ 256
b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256
N = N + 1
x = (r + g + b) \ 3
wx(M, N) = x
picCapture.PSet (i, j), RGB(x, x, x)

Next
Next

David, S.Kom., M.Cs |

15

COMPUTER VISION - IMAGE PROCESSING

End Sub

Private Sub cmdEdgeDetection_Click()


''Metode Sobel
For i = 1 To M
For j = 1 To N
z1 = 0
z2 = 0
For u1 = -1 To 1
For u2 = -1 To 1
z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)
Next
Next
z = Int(Abs(z1 + z2))
If z > 255 Then z = 255
picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)
Next
Next
End Sub

Private Sub cmdLoad_Click()


Dim namafile As String
CD.DialogTitle = "Loading Gambar"
CD.Filter = "jpg file (*.jpg) |*.jpg"
CD.ShowOpen
namafile = CD.FileName
picAsli.Picture = LoadPicture(namafile)
Text1.Text = namafile
End Sub

Private Sub Form_Load()


h1(1, 1) = -1: h1(1, 2) = -1: h1(1, 3) = -1
h1(2, 1) = -1: h1(2, 2) = 8: h1(2, 3) = -1
h1(3, 1) = -1: h1(3, 2) = -1: h1(3, 3) = -1
For i = 1 To 3
For j = 1 To 3
h2(i, j) = h1(j, i)
Next
Next
End Sub

David, S.Kom., M.Cs |

16

You might also like