エッジ関係


Imports System.Drawing.Imaging
Public Class Form1
 Private Bmp As Bitmap = New Bitmap(“C:\aaa.jpg”)
 Private eBmp As Bitmap

 Private Sub Picture_Box(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
  If eBmp Is Nothing Then Exit Sub
  e.Graphics.DrawImage(eBmp, 0, 0)
 End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  EdgeDetection()
  PictureBox1.Invalidate()
 End Sub

 Private Sub EdgeDetection()
  GrayScale(Bmp)
  eBmp = New Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb)
  Dim Col(8) As Color
  Dim newR As Integer, newG As Integer, newB As Integer
  Dim skew As Integer = -0
  Dim around As Integer = -5
  Dim center As Integer = 24
  Dim s() As Single = {skew, around, skew, _
        around, center, around, _
        skew, around, skew}
  Dim wa As Integer
  For i As Integer = 0 To UBound(s)
   wa += s(i)
  Next
  For y As Integer = 1 To bmp.Height – 2
   For x As Integer = 1 To bmp.Width – 2
    Col(0) = bmp.GetPixel(x – 1, y – 1)
    Col(1) = bmp.GetPixel(x, y – 1)
    Col(2) = bmp.GetPixel(x + 1, y – 1)
    Col(3) = bmp.GetPixel(x – 1, y)
    Col(4) = bmp.GetPixel(x, y)
    Col(5) = bmp.GetPixel(x + 1, y)
    Col(6) = bmp.GetPixel(x – 1, y + 1)
    Col(7) = bmp.GetPixel(x, y + 1)
    Col(8) = bmp.GetPixel(x + 1, y + 1)

    For k As Integer = 0 To 8
     newR += Col(k).R * s(k)
     newG += Col(k).G * s(k)
     newB += Col(k).B * s(k)
    Next
    newR = Math.Min(Math.Max(newR, 0), 255)
    newG = Math.Min(Math.Max(newG, 0), 255)
    newB = Math.Min(Math.Max(newB, 0), 255)

    eBmp.SetPixel(x, y, Color.FromArgb(newR, newG, newB))
   Next
  Next
 End Sub

 Public Sub GrayScale(ByRef srcBmp As Bitmap)
  Dim col As Color
  Dim int As Integer
  For y As Integer = 0 To srcBmp.Height – 1
   For x As Integer = 0 To srcBmp.Width – 1
    col = srcBmp.GetPixel(x, y)
    int = Math.Min(col.R * 0.299 + col.G * 0.587 + col.B * 0.114, 255)
    srcBmp.SetPixel(x, y, Color.FromArgb(int, int, int))
   Next
  Next
 End Sub
End Class



システム開発のためのVB.NETプログラミング関係一覧に戻る


Google検索の結果

system-development