Rotasi (Rotation)



Rumus rotasi citra:

x’ = x cos(q) – y sin(q)
y’ = x sin(q) + y cos(q)
yang dalam hal ini, q = sudut rotasi berlawanan arah jarum jam (lihat gambar dibawah ini).
Model rotasi citra

Jika citra semula adalah A dan citra hasil rotasi adalah B, maka rotasi citra dari A ke B :

B[x’][y’] = B[x cos(q) – y sin(q)][x cos(q) + y cos(q)] = A[x][y]

Jika sudut rotasinya 90°, maka implementasinya lebih mudah dilakukan dengan cara menyalin pixel-pixel baris ke pixel-pixel kolom pada arah rotasi. Rotasi 180° diimplementasikan dengan melakukan rotasi 90° dua kali.
Implementasi di VB Net :

Public Class Form1
    Dim bmap As Bitmap
    Dim PicAda As Boolean
   
    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.Filter = "Images|*.GIF;*.TIF;*.JPG;*.BMP"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Then Exit Sub
        'else
        PicAda = True
        PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub SimpanToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem1.Click
        Dim i As Integer
        Dim str As String
        For i = 1 To 100
            If i < 10 Then str = "Picture_00" & i.ToString & ".jpg"
            If i > 9 And i < 100 Then str = "Picture_0" & i.ToString & ".jpg"
            If i > 99 Then str = "Picture_" & i.ToString & ".jpg"
            If i > 900 Then MsgBox("Gambar Telah Melebihi Kapasitas , Tolong Periksa dan Hapus Salah Satu Gambar")
            If Not System.IO.File.Exists(str) Then
                Try
                    PictureBox1.Image.Save(str, System.Drawing.Imaging.ImageFormat.Jpeg) 'Cropped
                    MsgBox("Gambar Berhasil Disimpan", MsgBoxStyle.OkOnly, "Simpan Berhasil")
                Catch Ex As Exception
                    MsgBox("Tidak Dapat Disimpan Kelokasi yang dituju")


                End Try
                Exit For
            End If
        Next
    End Sub

    Private Sub HapusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        PictureBox1.Image = Nothing
    End Sub

    Private Sub MonokromToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonochromToolStripMenuItem.Click
        If PicAda = False Then

            MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses")
            Exit Sub
        End If
        bmap = New Bitmap(PictureBox1.Image) 'Gambar asli dijadikan gambar Bitmap
        PictureBox2.Image = bmap
        Dim tempbmp As New Bitmap(PictureBox2.Image) 'deklarasi gambar Bitmap dari gambar asli untuk diproses
        Dim DX As Integer = 1
        Dim DY As Integer = 1
        Dim Red As Integer, Green As Integer, Blue As Integer, Grey As Integer
        Dim X, Y As Integer
        ProgressBar1.Width = PictureBox2.Width
        ProgressBar1.Show()
        With tempbmp
            For X = DX To .Height - DX - 1
                For Y = DY To .Width - DY - 1
                    Red = CInt(.GetPixel(Y, X).R) 'ambil nilai warna merah (Red) pada pixel(Y,X)
                    Green = CInt(.GetPixel(Y, X).G) 'ambil nilai warna hijau (Green) pada pixel(Y,X)
                    Blue = CInt(.GetPixel(Y, X).B) 'ambil nilai warna biru (Blue) pada pixel(Y,X)
                    Grey = (Red + Green + Blue) / 3 'konversi warna pada pixel Y,X ke grey 
                    If (Grey < 128) Then
                        Red = 0
                        Green = 0
                        Blue = 0
                    Else
                        Red = 255
                        Blue = 255
                        Green = 255
                    End If
                    bmap.SetPixel(Y, X, Color.FromArgb(Red, Green, Blue)) 'simpan warna baru pada pixel(Y,X)
                Next
                If X Mod 10 = 0 Then
                    PictureBox1.Invalidate()
                    Me.Text = "Progres Proses Monokrom : " & Int(100 * X / (PictureBox2.Image.Height - 2)).ToString & "%"
                    ProgressBar1.Value = Int(100 * X / (PictureBox2.Image.Height - 2))
                    PictureBox2.Refresh()
                End If
            Next
        End With
        ProgressBar1.Hide()
        PictureBox2.Refresh()
        Me.Text = "Pengolahan Citra : Proses Monokrom berhasil"

    End Sub
    
    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        PictureBox2.Image = Nothing
        PictureBox1.Image = Nothing
    End Sub

    Private Sub KontrasToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContrastToolStripMenuItem.Click
        HScrollBar1.Enabled = True
        Label1.Show()
        HScrollBar1.Show()
        If PicAda = False Then
            MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses")
            Exit Sub
        End If
        bmap = New Bitmap(PictureBox1.Image) 'Gambar asli dijadikan gambar Bitmap
        PictureBox2.Image = bmap
    End Sub

    Private Sub GrayscaleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrayscaleToolStripMenuItem.Click
        If PicAda = False Then
            MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses")
            Exit Sub
        End If
        bmap = New Bitmap(PictureBox1.Image) 'Gambar asli dijadikan gambar Bitmap

        Dim tempbmp As New Bitmap(PictureBox1.Image) 'deklarasi gambar Bitmap dari gambar asli untuk diproses
        Dim DX As Integer = 1
        Dim DY As Integer = 1
        Dim Red As Integer, Green As Integer, Blue As Integer
        Dim X, Y As Integer
        Dim Grey(,) As Double
        ReDim Grey(tempbmp.Width, tempbmp.Height)
        ProgressBar1.Width = PictureBox2.Width
        ProgressBar1.Show()
        With tempbmp
            For X = DX To .Height - DX - 1
                For Y = DY To .Width - DY - 1
                    Red = CInt(.GetPixel(Y, X).R) 'ambil nilai warna merah (Red) pada pixel(Y,X)
                    Green = CInt(.GetPixel(Y, X).G) 'ambil nilai warna hijau (Green) pada pixel(Y,X)
                    Blue = CInt(.GetPixel(Y, X).B) 'ambil nilai warna biru (Blue) pada pixel(Y,X)
                    Grey(Y, X) = (Red + Green + Blue) / 3 'konversi warna pada pixel Y,X ke grey 
                    bmap.SetPixel(Y, X, Color.FromArgb(Grey(Y, X), Grey(Y, X), Grey(Y, X))) 'simpan warna baru pada pixel(Y,X)
                Next
                If X Mod 10 = 0 Then
                    Me.Text = "Progres Proses Grey Scale : " & Int(100 * X / (PictureBox1.Image.Height - 2)).ToString & "%"
                    ProgressBar1.Value = Int(100 * X / (PictureBox1.Image.Height - 2))
                End If
            Next
        End With
        ProgressBar1.Hide()
        PictureBox2.Image = bmap
        Me.Text = "Pengolahan Citra : Proses Grey Scale berhasil"
    End Sub

    Private Sub Rotasi(ByVal s As RotateFlipType)
        If PicAda = False Then
            MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses")
            Exit Sub
        End If
        bmap = New Bitmap(PictureBox1.Image) 'Gambar asli dijadikan gambar Bitmap
        PictureBox2.Image = bmap
        bmap.RotateFlip(s)
        PictureBox2.Image = bmap
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Rotasi(RotateFlipType.Rotate90FlipNone)
    End Sub

    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        Rotasi(RotateFlipType.Rotate180FlipNone)
    End Sub

    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        Rotasi(RotateFlipType.Rotate270FlipNone)
    End Sub

    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HScrollBar1.Hide()
        ProgressBar1.Hide()
        HScrollBar1.Maximum = 100
        HScrollBar1.Minimum = -100
        HScrollBar1.Value = 0
        Label1.Hide()
        Label1.Text = "0"
    End Sub

    
    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Label1.Text = HScrollBar1.Value
        If (e.Type = ScrollEventType.EndScroll) Then
            If PicAda = False Then
                MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses")
                Exit Sub
            End If
            bmap = New Bitmap(PictureBox1.Image) 'Gambar asli dijadikan gambar Bitmap
            PictureBox2.Image = bmap
            Dim tempbmp As New Bitmap(PictureBox2.Image) 'deklarasi gambar Bitmap dari gambar asli untuk diproses
            Dim DX As Integer = 1
            Dim DY As Integer = 1
            Dim Red As Integer, Green As Integer, Blue As Integer ',Grey As Integer
            Dim X, Y As Integer
            Dim tb As Integer
            tb = HScrollBar1.Value()
            ProgressBar1.Width = PictureBox2.Width
            ProgressBar1.Show()
            With tempbmp
                For X = DX To .Height - DX - 1
                    For Y = DY To .Width - DY - 1
                        Red = CInt(.GetPixel(Y, X).R) 'ambil nilai warna merah (Red) pada pixel(Y,X)
                        Green = CInt(.GetPixel(Y, X).G) 'ambil nilai warna hijau (Green) pada pixel(Y,X)
                        Blue = CInt(.GetPixel(Y, X).B) 'ambil nilai warna biru (Blue) pada pixel(Y,X)
                        'Grey = (Red + Green + Blue) / 3 'konversi warna pada pixel Y,X ke grey 
                        Red = Red + tb
                        Green = Green + tb
                        Blue = Blue + tb
                        If (Red > 255) Then
                            Red = 255
                        End If
                        If (Blue > 255) Then
                            Blue = 255
                        End If
                        If (Green > 255) Then
                            Green = 255
                        End If
                        bmap.SetPixel(Y, X, Color.FromArgb(Red, Green, Blue)) 'simpan warna baru pada pixel(Y,X)
                    Next
                    If X Mod 10 = 0 Then
                        PictureBox2.Invalidate()
                        Me.Text = "Progres Contrast : " & Int(100 * X / (PictureBox2.Image.Height - 2)).ToString & "%"
                        ProgressBar1.Value = Int(100 * X / (PictureBox2.Image.Height - 2))
                        PictureBox2.Refresh()
                    End If
                Next
            End With
            ProgressBar1.Hide()
            PictureBox2.Refresh()
            Me.Text = "Pengolahan Citra : Proses Contrast berhasil"
        End If
    End Sub
End Class



Hasil Eksekusi :

 


Komentar