2019年3月18日 星期一

(VB)影像處理(I)-彩色轉灰階、黑白、互補色





先編輯GUI(Graphical User Interface)介面,需要的元件皆是從「工具箱」內選取,此影像處理程式所需要的元件如下所示:

標籤(Label)x2

  • Label1: 設定Text為Origin,用來表示原始圖片。
  • Label2: 設定Text為Modified,用來表示處理過的圖片。




影像盒子(PictureBox)x2

  • PictureBox1: 設定name為OriginPic,表示原始圖片;設定Size為400, 400,SizeMode為StretchImage。
  • PictureBox2: 設定name為ModifiedPic,表示處理過的圖片;設定Size為400, 400,SizeMode為StretchImage。

按鈕(Button)x7

  • Button1: 設定Text為Browse,Name為BrowsBtn,Size為100, 40,Font為12pt。
  • Button2: 設定Text為Save,Name為SaveBtn,Size為100, 40,Font為12pt。
  • Button3: 設定Text為Reset,Name為ResetBtn,Size為100, 40,Font為12pt。
  • Button4: 設定Text為About,Name為AboutBtn,Size為100, 40,Font為12pt。
  • Button5: 設定Text為GrayScale,Name為GrayscaleBtn,Size為100, 60,Font為12pt。
  • Button6: 設定Text為BW Scale,Name為BWBtn,Size為100, 60,Font為12pt。
  • Button7: 設定Text為Inverse,Name為InverseBtn,Size為100, 60,Font為12pt。

開啟檔案對話框(OpenFileDialog)x1

  • OpenFileDialog1: 直接放入,會顯示在GUI介面之外。

儲存檔案對話框(SaveFileDialog)x1

  • SaveFileDialog1: 直接放入,會顯示在GUI介面之外。

進度條(ProgressBar)x1

  • ProgressBar1: 放置在PictureBox2的中間,Size為360, 25。

程式碼都是針對按鈕(Button)撰寫,從Button1-->Button7依序點開來撰寫(我用不同顏色區分不同按鈕的程式),程式碼如下:



Public Class Form1

    Private Sub BrowsBtn_Click(sender As Object, e As EventArgs) Handles BrowsBtn.Click
        OpenFileDialog1.InitialDirectory = "D"
        OpenFileDialog1.Title = "Open Iamge"
        OpenFileDialog1.filter = "Gamber |*.bmp; *.jpg; *.gif; *.jpeg"
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            OriginPic.Image = Image.FromFile(OpenFileDialog1.FileName)
            Exit Sub
        End If
    End Sub

    Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
        SaveFileDialog1.FileName = "Save Image"
        SaveFileDialog1.Filter = "Gamber |*.bmp; *.jpg; *.gif; *.jpeg"
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            ModifiedPic.Image.Save(SaveFileDialog1.FileName)
        End If
    End Sub

    Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click
        ModifiedPic.Image = OriginPic.Image
    End Sub

    Private Sub AboutBtn_Click(sender As Object, e As EventArgs) Handles AboutBtn.Click
        MsgBox("Created by Jasonkyon, 2019", MsgBoxStyle.OkOnly, "About Program")
    End Sub

    Private Sub GrayscaleBtn_Click(sender As Object, e As EventArgs) Handles GrayscaleBtn.Click
        Dim Gambar As New Bitmap(OriginPic.Image)
        ModifiedPic.Image = Gambar
        Dim w, h As Integer
        Dim red, green, blue, abu2 As Integer

        ProgressBar1.Show()
        For w = 0 To Gambar.Width - 1
            For h = 0 To Gambar.Height - 1
                red = Gambar.GetPixel(w, h).R
                green = Gambar.GetPixel(w, h).G
                blue = Gambar.GetPixel(w, h).B

                abu2 = CInt((red + green + blue) / 3)
                Gambar.SetPixel(w, h, Color.FromArgb(abu2, abu2, abu2))
            Next
            If w Mod 10 = 0 Then
                Me.Text = "Proses Graysacle: " & Int(100 * w / Gambar.Width).ToString & "%"
                ProgressBar1.Value = Int(100 * w / Gambar.Width)
            End If
        Next
        Me.Text = "Proses Berhasil"
        ProgressBar1.Hide()
        ModifiedPic.Refresh()
    End Sub

    Private Sub BWBtn_Click(sender As Object, e As EventArgs) Handles BWBtn.Click
        Dim Gambar As New Bitmap(OriginPic.Image)
        ModifiedPic.Image = Gambar
        Dim w, h As Integer
        Dim red, green, blue, abu2 As Integer

        ProgressBar1.Show()
        For w = 0 To Gambar.Width - 1
            For h = 0 To Gambar.Height - 1
                red = Gambar.GetPixel(w, h).R
                green = Gambar.GetPixel(w, h).G
                blue = Gambar.GetPixel(w, h).B

                abu2 = CInt((red + green + blue) / 3)

                If abu2 < 128 Then
                    Gambar.SetPixel(w, h, Color.FromArgb(0, 0, 0))
                Else
                    Gambar.SetPixel(w, h, Color.FromArgb(255, 255, 255))
                End If
            Next
            If w Mod 10 = 0 Then
                Me.Text = "Proses BW: " & Int(100 * w / Gambar.Width).ToString & "%"
                ProgressBar1.Value = Int(100 * w / Gambar.Width)
            End If
        Next
        Me.Text = "Proses Berhasil"
        ProgressBar1.Hide()
        ModifiedPic.Refresh()
    End Sub

    Private Sub InverseBtn_Click(sender As Object, e As EventArgs) Handles InverseBtn.Click
        Dim Gambar As New Bitmap(OriginPic.Image)
        ModifiedPic.Image = Gambar
        Dim w, h As Integer
        Dim red, green, blue As Integer

        ProgressBar1.Show()
        For w = 0 To Gambar.Width - 1
            For h = 0 To Gambar.Height - 1
                red = Gambar.GetPixel(w, h).R
                green = Gambar.GetPixel(w, h).G
                blue = Gambar.GetPixel(w, h).B

                Gambar.SetPixel(w, h, Color.FromArgb(255 - red, 255 - green, 255 - blue))

            Next
            If w Mod 10 = 0 Then
                Me.Text = "Proses Inverse: " & Int(100 * w / Gambar.Width).ToString & "%"
                ProgressBar1.Value = Int(100 * w / Gambar.Width)
            End If
        Next
        Me.Text = "Proses Berhasil"
        ProgressBar1.Hide()
        ModifiedPic.Refresh()
    End Sub
End Class

大功告成!!

沒有留言:

張貼留言