praktikum kecerdasan buatan lanjutan-1

Upload: selfia

Post on 28-Feb-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    1/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 1

    Reduksi Noise

    Reduksi Noise dengan Filter Rata-rata

    Desain Form

    Listing program

    Dim wt(400, 400) As IntegerDim Nfilter As IntegerDim h() As Single

    Private Sub Command1_Click()If Option1 Then

    Nfilter = 3ElseIf Option2 Then

    Nfilter = 5

    ElseNfilter = 7End If

    ReDim h(Nfilter + 1, Nfilter + 1) As Single

    'Randomize TimerFor i = 1 To Nfilter

    For j = 1 To Nfilterh(i, j) = 1 / (Nfilter ^ 2)

    Next jNext i

    probNoise = Val(Text1)dx = 0

    sx = 0

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    2/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 2

    n1 = 0For i = 1 To Picture4.ScaleWidth Step 15

    n1 = n1 + 1n2 = 0

    For j = 1 To Picture4.ScaleHeight Step 15warna = 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 + 1wt(n1, n2) = wxPicture4.PSet (i, j), RGB(wx, wx, wx)

    'Pembangkitan Noise Gaussian'Menggunakan metode Rejection

    sw = 0While sw = 0

    x = 2 * Rnd - 1y = RndIf y < Exp(-x ^ 2) Then

    sw = xEnd If

    Wendwx1 = 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 isnr = 10 * Log(sx / nx) / Log(10)Label1.Caption = snrEnd Sub

    Private Sub Command2_Click()Dim xt(400, 400) As IntegerIf Option1 Then

    Nfilter = 3ElseIf Option2 Then

    Nfilter = 5Else

    Nfilter = 7End If

    ReDim h(Nfilter + 1, Nfilter + 1) As Single

    Randomize TimerFor i = 1 To Nfilter

    For j = 1 To Nfilterh(i, j) = 1 / (Nfilter ^ 2)

    Next jNext i

    'RGB to Grayn1 = 0For i = 1 To Picture4.ScaleWidth Step 15n1 = n1 + 1n2 = 0

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

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    3/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 3

    b = Int(Int((warna And RGB(0, 0, 255)) / 256) / 256)n2 = n2 + 1wx = Int((r + g + b) / 3)xt(n1, n2) = wx

    Next jNext i'Proses Filter dengan KonvolusiOn Error Resume Next

    nx = 0sx = 0m = Int(Nfilter / 2)For i = 1 To n1

    For j = 1 To n2z = 0For u1 = -m To m

    For u2 = -m To mIf 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 u2Next u1Picture3.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 jNext isnr = 10 * Log(sx / nx) / Log(10)Label2.Caption = snrEnd Sub

    Private Sub cmdClose_Click()EndEnd Sub

    Private Sub cmdOpen_Click()CommonDialog1.Filter = "File Gambar(*.bmp)|*.bmp|" & _

    "File Gambar(*.jpg)|*.jpg | Semua File Gambar(*.*)|" & _"*.bmp ;*.jpg"CommonDialog1.ShowOpennamafile = CommonDialog1.FileNamePicture1.Picture = LoadPicture(namafile)

    End Sub

    Private Sub Command3_Click()Picture2.ClsPicture3.ClsPicture4.ClsErase h

    End Sub

    Private Sub Command4_Click()Dim xt(400, 400) As Integer

    Randomize Timern1 = 0

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    4/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 4

    For i = 1 To Picture1.ScaleWidth Step 15n1 = n1 + 1n2 = 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 + 1wx = Int((r + g + b) / 3)xt(n1, n2) = wxPicture4.PSet (i, j), RGB(wx, wx, wx)

    Next jNext iEnd Sub

    Private Sub Form_Load()

    Randomize TimerEnd Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    5/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 5

    Reduksi Noise

    Reduksi Noise dengan Filter Gaussian

    Desain Form

    Listing Program

    Dim wt(400, 400) As IntegerDim h() As SingleDim Nfilter As Integer

    Private Sub cmdClose_Click()EndEnd Sub

    Private Sub cmdOpen_Click()CommonDialog1.Filter = "File Gambar(*.bmp)|*.bmp|" & _

    "File Gambar(*.jpg)|*.jpg | Semua File Gambar(*.*)|" & _"*.bmp ;*.jpg"CommonDialog1.ShowOpennamafile = CommonDialog1.FileNamePicture1.Picture = LoadPicture(namafile)

    End Sub

    Private Sub Command1_Click()If Option1 Then

    Nfilter = 3

    ElseIf Option2 Then

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    6/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 6

    Nfilter = 5Else

    Nfilter = 7End If

    ReDim h(Nfilter + 1, Nfilter + 1) As Single

    Randomize Timer'Nfilter = 7s = 0.5m = Int(Nfilter / 2) + 1sh = 0For 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 jNext iFor i = 1 To Nfilter

    For j = 1 To Nfilterh(i, j) = h(i, j) / sh

    Next jNext i

    probNoise = Val(Text1)dx = 0sx = 0n1 = 0For i = 1 To Picture4.ScaleWidth Step 15

    n1 = n1 + 1n2 = 0

    For j = 1 To Picture4.ScaleHeight Step 15warna = 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 + 1wt(n1, n2) = wxPicture4.PSet (i, j), RGB(wx, wx, wx)

    'Pembangkitan Noise Gaussian'Menggunakan metode Rejection

    sw = 0While sw = 0

    x = 2 * Rnd - 1y = Rnd

    If y < Exp(-x ^ 2) Thensw = x

    End IfWendwx1 = 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 isnr = 10 * Log(sx / nx) / Log(10)Label1.Caption = snrEnd Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    7/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 7

    Private Sub Command2_Click()Dim xt(400, 400) As Integer

    If Option1 Then

    Nfilter = 3ElseIf Option2 Then

    Nfilter = 5Else

    Nfilter = 7End If

    ReDim h(Nfilter + 1, Nfilter + 1) As Single

    Randomize Timer'Nfilter = 7s = 0.5m = Int(Nfilter / 2) + 1sh = 0For 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 jNext iFor i = 1 To Nfilter

    For j = 1 To Nfilterh(i, j) = h(i, j) / sh

    Next jNext i

    'RGB to Graym = Int(Nfilter / 2)n1 = 0For i = 1 To Picture4.ScaleWidth Step 15n1 = n1 + 1n2 = 0

    For j = 1 To Picture4.ScaleHeight Step 15warna = 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 + 1xt(n1, n2) = wx

    Next j

    Next i'Proses Filter dengan Konvolusinx = 0sx = 0For i = 1 To n1

    For j = 1 To n2z = 0For u1 = -m To m

    For u2 = -m To mIf 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 u2Next u1

    Picture3.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)nx = nx + Abs(z - wt(i, j))

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    8/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 8

    sx = sx + Abs(wt(i, j))Next j

    Next isnr = 10 * Log(sx / nx) / Log(10)

    Label2.Caption = snrEnd Sub

    Private Sub Command3_Click()Picture2.ClsPicture3.ClsPicture4.ClsErase hEnd Sub

    Private Sub Command4_Click()

    Dim xt(400, 400) As Integer

    Randomize Timern1 = 0For i = 1 To Picture1.ScaleWidth Step 15n1 = n1 + 1n2 = 0

    For j = 1 To Picture1.ScaleHeight Step 15warna = 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 + 1wx = Int((r + g + b) / 3)xt(n1, n2) = wxPicture4.PSet (i, j), RGB(wx, wx, wx)

    Next jNext iEnd Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    9/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 9

    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 LongDim j As LongDim Warna As LongDim r As Long, g As Long, b As LongDim x As Long

    picCapture.Cls

    For i = 1 To picAsli.Width Step 15M = M + 1

    N = 0For 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)) \ 256b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256N = N + 1x = (r + g + b) \ 3

    wx(M, N) = xpicCapture.PSet (i, j), RGB(x, x, x)

    NextNextEnd Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    10/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 10

    Private Sub cmdEdgeDetection_Click()''Metode RobertFor 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 = 255picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(w, w, w)

    NextNextEnd Sub

    Private Sub cmdLoad_Click()Dim namafile As String

    CD.DialogTitle = "Loading Gambar"CD.Filter = "Jpg file (*.jpg) |*.jpg"

    CD.ShowOpennamafile = CD.FileName

    picAsli.Picture = LoadPicture(namafile)Text1.Text = namafile

    End Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    11/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 11

    Edge Detection (Deteksi Tepi)

    Edge Detection Metode Prewitt

    Desain Form

    Listing program

    Dim wx(500, 500) As IntegerDim M As Long, N As LongDim h1(3, 3) As SingleDim h2(3, 3) As Single

    Private Sub cmdcitraBiner_Click()Dim i As LongDim j As LongDim Warna As LongDim r As Long, g As Long, b As LongDim x As Long

    picCapture.Cls

    For i = 1 To picAsli.Width Step 15M = M + 1N = 0For 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)) \ 256b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256N = N + 1x = (r + g + b) \ 3

    wx(M, N) = xpicCapture.PSet (i, j), RGB(x, x, x)

    NextNext

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    12/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 12

    End Sub

    Private Sub cmdEdgeDetection_Click()''Metode PrewittFor i = 1 To M

    For j = 1 To Nz1 = 0z2 = 0For u1 = -1 To 1

    For u2 = -1 To 1z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)

    NextNextz = Int(Abs(z1 + z2))If z > 255 Then z = 255picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)

    NextNextEnd Sub

    Private Sub cmdLoad_Click()Dim namafile As String

    CD.DialogTitle = "Loading Gambar"CD.Filter = "jpg file (*.jpg) |*.jpg"

    CD.ShowOpennamafile = 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) = 1h1(2, 1) = -1: h1(2, 2) = 0: h1(2, 3) = 1h1(3, 1) = -1: h1(3, 2) = 0: h1(3, 3) = 1

    For i = 1 To 3For j = 1 To 3

    h2(i, j) = h1(j, i)NextNextEnd Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    13/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 13

    Edge Detection (Deteksi Tepi)

    Edge Detection Metode Sobel

    Desain Form

    Listing Program

    Dim wx(500, 500) As Integer

    Dim M As Long, N As LongDim h1(3, 3) As SingleDim h2(3, 3) As Single

    Private Sub cmdcitraBiner_Click()Dim i As LongDim j As LongDim Warna As LongDim r As Long, g As Long, b As LongDim x As Long

    picCapture.Cls

    For i = 1 To picAsli.Width Step 15M = M + 1N = 0For 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)) \ 256b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256N = N + 1x = (r + g + b) \ 3

    wx(M, N) = xpicCapture.PSet (i, j), RGB(x, x, x)

    Next

    Next

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    14/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 14

    End Sub

    Private Sub cmdEdgeDetection_Click()''Metode SobelFor i = 1 To M

    For j = 1 To Nz1 = 0z2 = 0For u1 = -1 To 1

    For u2 = -1 To 1z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)

    NextNextz = Int(Abs(z1 + z2))If z > 255 Then z = 255picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)

    NextNextEnd Sub

    Private Sub cmdLoad_Click()Dim namafile As String

    CD.DialogTitle = "Loading Gambar"CD.Filter = "jpg file (*.jpg) |*.jpg"

    CD.ShowOpennamafile = 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) = 1h1(2, 1) = -2: h1(2, 2) = 0: h1(2, 3) = 2h1(3, 1) = -1: h1(3, 2) = 0: h1(3, 3) = 1

    For i = 1 To 3For j = 1 To 3

    h2(i, j) = h1(j, i)NextNext

    End Sub

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    15/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 15

    Edge Detection (Deteksi Tepi)

    Edge Detection Metode Filter Laplacian

    Desain Form

    Listing Program

    Dim wx(500, 500) As Integer

    Dim M As Long, N As LongDim h1(3, 3) As SingleDim h2(3, 3) As Single

    Private Sub cmdcitraBiner_Click()Dim i As LongDim j As LongDim Warna As LongDim r As Long, g As Long, b As LongDim x As Long

    picCapture.Cls

    For i = 1 To picAsli.Width Step 15M = M + 1N = 0For 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)) \ 256b = ((Warna And RGB(0, 0, 255)) \ 256) \ 256N = N + 1x = (r + g + b) \ 3

    wx(M, N) = xpicCapture.PSet (i, j), RGB(x, x, x)

    Next

    Next

  • 7/25/2019 Praktikum Kecerdasan Buatan Lanjutan-1

    16/16

    COMPUTER VISION - IMAGE PROCESSING

    David, S.Kom., M.Cs| 16

    End Sub

    Private Sub cmdEdgeDetection_Click()''Metode SobelFor i = 1 To M

    For j = 1 To Nz1 = 0z2 = 0For u1 = -1 To 1

    For u2 = -1 To 1z1 = z1 + h1(u1 + 2, u2 + 2) * wx(i + u1, j + u2)z2 = z2 + h2(u1 + 2, u2 + 2) * wx(i + u1, j + u2)

    NextNextz = Int(Abs(z1 + z2))If z > 255 Then z = 255picTepi.PSet ((i - 1) * 15 + 1, (j - 1) * 15 + 1), RGB(z, z, z)

    NextNextEnd Sub

    Private Sub cmdLoad_Click()Dim namafile As String

    CD.DialogTitle = "Loading Gambar"CD.Filter = "jpg file (*.jpg) |*.jpg"

    CD.ShowOpennamafile = 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) = -1h1(2, 1) = -1: h1(2, 2) = 8: h1(2, 3) = -1h1(3, 1) = -1: h1(3, 2) = -1: h1(3, 3) = -1

    For i = 1 To 3For j = 1 To 3

    h2(i, j) = h1(j, i)NextNext

    End Sub