2009年1月4日 星期日

Equalization using VB













←開檔













←依序為原圖、灰階、灰階均化、轉回彩色















←存檔


' 背景需要先拉入OpenFileDialog與SaveFileDialog物件

' RGB 轉成 YIQ 並且顯示灰階
  Dim i, j, r, g, b, Y, imgW, imgH As Integer
  Dim bmp As New Bitmap(PictureBox1.Image)
  imgW = bmp.Width
  imgH = bmp.Height
  Dim bmp1 As New Bitmap(imgW, imgH)

  For i = 0 To imgW - 1
   For j = 0 To imgH - 1
    r = bmp.GetPixel(i, j).R
    g = bmp.GetPixel(i, j).G
    b = bmp.GetPixel(i, j).B
    Y = (0.299 * r + 0.587 * g + 0.114 * b)
    If Y < 0 Then
     Y = 0
    ElseIf Y > 255 Then
     Y = 255
    End If
    bmp1.SetPixel(i, j, Color.FromArgb(Y, Y, Y))
   Next
  Next

  PictureBox2.SizeMode = PictureBox1.SizeMode
  PictureBox2.Image = bmp1

' 對灰階影像做亮度均化
  Dim imgW, imgH, i, j, Y As Integer
  Dim bmp As New Bitmap(PictureBox2.Image)

  imgW = bmp.Width
  imgH = bmp.Height
  Dim bmp1 As New Bitmap(imgW, imgH)
  Dim N(255) As Integer

  For i = 0 To imgW - 1
   For j = 0 To imgH - 1
    Y = bmp.GetPixel(i, j).R
    N(Y) = N(Y) + 1
   Next
  Next

  For i = 1 To 255
   N(i) = N(i - 1) + N(i)
  Next

  j = imgW * imgH

  For i = 0 To 255
   ' 四捨五入
   If (N(i) * 255 / j * 10) Mod 10 < 5 Then
    N(i) = N(i) * 255 / j
   Else
    N(i) = N(i) * 255 / j + 1
   End If
   If N(i) > 255 Then
    N(i) = 255
   ElseIf N(i) < 0 Then
   N(i) = 0
   End If
  Next

  For i = 0 To imgW - 1
   For j = 0 To imgH - 1
    Y = N(bmp.GetPixel(i, j).R)
    bmp1.SetPixel(i, j, Color.FromArgb(Y, Y, Y))
   Next
  Next

  PictureBox3.SizeMode = PictureBox2.SizeMode
  PictureBox3.Image = bmp1

' 將均化過的Y與沒變的IQ轉回RGB
  Dim i, j, r, g, b, Y, AI, Q, imgW, imgH As Integer
  Dim bmp As New Bitmap(PictureBox1.Image)
  Dim bmp1 As New Bitmap(PictureBox3.Image)
  imgW = bmp.Width
  imgH = bmp.Height
  Dim bmp2 As New Bitmap(imgW, imgH)

  For i = 0 To imgW - 1
   For j = 0 To imgH - 1
    r = bmp.GetPixel(i, j).R
    g = bmp.GetPixel(i, j).G
    b = bmp.GetPixel(i, j).B
    Y = bmp1.GetPixel(i, j).R
    AI = (0.596 * r - 0.274 * g - 0.322 * b)
    Q = (0.211 * r - 0.522 * g + 0.311 * b)

    r = (1 * Y + 0.956 * AI + 0.632 * Q)
    If r < 0 Then
     r = 0
    ElseIf r > 255 Then
     r = 255
    End If
    g = (1 * Y - 0.272 * AI - 0.648 * Q)
    If g < 0 Then
     g = 0
    ElseIf g > 255 Then
     g = 255
    End If
    b = (1 * Y - 1.105 * AI + 0.705 * Q)
    If b < 0 Then
     b = 0
    ElseIf b > 255 Then
     b = 255
    End If
    bmp2.SetPixel(i, j, Color.FromArgb(r, g, b))
   Next
  Next

  PictureBox4.SizeMode = PictureBox3.SizeMode
  PictureBox4.Image = bmp2

' 讀檔
  Dim openFileDialog1 As New OpenFileDialog()
  openFileDialog1.Filter = "Bmp Files|*.bmp"
  openFileDialog1.Title = "Select a Bitmap File"

  If openFileDialog1.ShowDialog() = DialogResult.OK Then
   PictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
   Button1.Enabled = True
   Label1.Visible = True
  End If

' 存檔
  Dim saveFileDialog1 As New SaveFileDialog()
  saveFileDialog1.Filter = "Bitmap Image|*.bmp"
  saveFileDialog1.Title = "Save an Bitmap File"
  saveFileDialog1.ShowDialog()

  ' If the file name is not an empty string open it for saving.
  If saveFileDialog1.FileName <> "" Then
   ' Saves the Image via a FileStream created by the OpenFile method.
   Dim fs As System.IO.FileStream = CType _ (saveFileDialog1.OpenFile(), System.IO.FileStream)
   ' Saves the Image in the appropriate ImageFormat based upon the
   ' file type selected in the dialog box.
   ' NOTE that the FilterIndex property is one-based.
   Me.PictureBox2.Image.Save(fs, _ System.Drawing.Imaging.ImageFormat.Bmp)

   fs.Close()
  End If

沒有留言: