もじゅ2


' 画像加工
Module Module02_image_processing


    Friend kaneru1() As Integer = {0, 1, 0,
                                  1, -4, 1,
                                   0, 1, 0} 'カーネル係数

    Friend kaneru2() As Integer = {1, 1, 1,
                                   1, -8, 1,
                                   1, 1, 1} 'カーネル係数

 


    ' ガウスぼかし処理(From GaussianBlur)
    Public Function GaussianBlurXY(ByVal src_bmp As Bitmap, ByVal zone As Integer) As Bitmap 'ソース画像 ,ぼかし度?

        Dim range As Integer = zone * 3

        Dim w As Integer = src_bmp.Width

        Dim h As Integer = src_bmp.Height

        ' 短辺の1/5より大きい場合は無視する

        If range <= 0 Or range >= Math.Min(w, h) \ 5 Then

            Return Nothing

        End If

        Dim gf(range) As Double

        For i As Integer = 0 To range

            gf(i) = Math.Exp(-i * i / (2 * zone * zone))

        Next

        Dim sum, sa, sr, sg, sb, gauss As Double

        Dim tmp_bmp As Bitmap = New Bitmap(src_bmp)

        Dim dis_bmp As Bitmap = New Bitmap(src_bmp)

 

        Dim rect As Rectangle = New Rectangle(0, 0, w, h)

 

        Dim src_bmpData As Imaging.BitmapData = src_bmp.LockBits(rect, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)

        Dim src_ptr As IntPtr = src_bmpData.Scan0

 

        Dim tmp_bmpData As Imaging.BitmapData = tmp_bmp.LockBits(rect, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)

        Dim tmp_ptr As IntPtr = tmp_bmpData.Scan0

 

        Dim dis_bmpData As Imaging.BitmapData = dis_bmp.LockBits(rect, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)

        Dim dis_ptr As IntPtr = dis_bmpData.Scan0

        Dim stride As Integer = dis_bmpData.Stride

        Dim rgba_step As Integer = CInt(stride / dis_bmpData.Width) ' RGBの時は3,RGBAの時は4

        Dim bytes As Integer = stride * h

 

        Dim dis_argb(bytes - 1) As Byte

        Dim tmp_argb(bytes - 1) As Byte

        Dim src_argb(bytes - 1) As Byte

        System.Runtime.InteropServices.Marshal.Copy(dis_ptr, dis_argb, 0, bytes)

        System.Runtime.InteropServices.Marshal.Copy(tmp_ptr, tmp_argb, 0, bytes)

        System.Runtime.InteropServices.Marshal.Copy(src_ptr, src_argb, 0, bytes)

 

        ' 水平方向に平均化(src_argb->tmp_argb)

        For y As Integer = 0 To h - 1

            For x As Integer = 0 To w - 1

                sum = 0

                sa = 0

                sr = 0

                sg = 0

                sb = 0

                Dim offset As Integer

                For ix As Integer = x - range To x + range

                    If ix < 0 Or ix >= w Then Continue For

                    offset = stride * y + ix * rgba_step

                    gauss = gf(Math.Abs(ix - x))

                    sa += src_argb(offset + 3) * gauss

                    sr += src_argb(offset + 2) * gauss * src_argb(offset + 3)

                    sg += src_argb(offset + 1) * gauss * src_argb(offset + 3)

                    sb += src_argb(offset + 0) * gauss * src_argb(offset + 3)

                    sum += gauss

                Next

                offset = stride * y + x * rgba_step

                If sa > 0 Then

                    tmp_argb(offset + 3) = AdjustByte(sa / sum)

                    tmp_argb(offset + 2) = AdjustByte(sr / sa)

                    tmp_argb(offset + 1) = AdjustByte(sg / sa)

                    tmp_argb(offset + 0) = AdjustByte(sb / sa)

                Else

                    tmp_argb(offset + 3) = 0

                    tmp_argb(offset + 2) = 0

                    tmp_argb(offset + 1) = 0

                    tmp_argb(offset + 0) = 0

                End If

            Next

        Next

 

        ' 垂直方向に平均化(tmp_argb->dis_argb)

        For y As Integer = 0 To h - 1

            For x As Integer = 0 To w - 1

                sum = 0

                sa = 0

                sr = 0

                sg = 0

                sb = 0

                Dim offset As Integer

                For iy As Integer = y - range To y + range

                    If iy < 0 Or iy >= h Then Continue For

                    offset = stride * iy + x * rgba_step

                    gauss = gf(Math.Abs(iy - y))

                    sa += tmp_argb(offset + 3) * gauss

                    sr += tmp_argb(offset + 2) * gauss * tmp_argb(offset + 3)

                    sg += tmp_argb(offset + 1) * gauss * tmp_argb(offset + 3)

                    sb += tmp_argb(offset + 0) * gauss * tmp_argb(offset + 3)

                    sum += gauss

                Next

                offset = stride * y + x * rgba_step

                If sa > 0 Then

                    dis_argb(offset + 3) = AdjustByte(sa / sum)

                    dis_argb(offset + 2) = AdjustByte(sr / sa)

                    dis_argb(offset + 1) = AdjustByte(sg / sa)

                    dis_argb(offset + 0) = AdjustByte(sb / sa)

                Else

                    dis_argb(offset + 3) = 0

                    dis_argb(offset + 2) = 0

                    dis_argb(offset + 1) = 0

                    dis_argb(offset + 0) = 0

                End If

            Next

        Next

 

        ' 処理結果を配列からコピー

        System.Runtime.InteropServices.Marshal.Copy(dis_argb, 0, dis_ptr, bytes)

        System.Runtime.InteropServices.Marshal.Copy(tmp_argb, 0, tmp_ptr, bytes)

        System.Runtime.InteropServices.Marshal.Copy(src_argb, 0, src_ptr, bytes)

        dis_bmp.UnlockBits(dis_bmpData)

        tmp_bmp.UnlockBits(tmp_bmpData)

        src_bmp.UnlockBits(src_bmpData)

        tmp_bmp.Dispose()

        tmp_bmp = Nothing

        ' src_bmpは呼び出し元で準備したものなので、操作しない

        Return dis_bmp

    End Function

    ' カラーコードを8ビット値に入るように調整する

    Public Function AdjustByte(ByVal value As Integer) As Byte

        If value < 0 Then

            Return 0

        ElseIf value > 255 Then

            Return 255

        End If

        Return value

    End Function

    Public Function AdjustByte(ByVal value As Double) As Byte

        If value < 0 Then

            Return 0

        ElseIf value > 255 Then

            Return 255

        End If

        Return value

    End Function

 

 

 

    'グレーを算出
    Public Function Gray_Calculation(r As Integer, g As Integer, b As Integer) '赤緑青

        Dim re_color As Integer = r * 0.3 + g * 0.59 + b * 0.11
        If re_color > 255 Then
            re_color = 255
        End If

        Return re_color

    End Function

 

    'グレー画像を返す
    Public Function RGB_to_Gray(n_bmp As Bitmap) '画像

        Dim Re_bmp As Bitmap = n_bmp.Clone

        ' Bitmap処理の高速化開始
        Dim bmpP As Module01_BitmapPlus = New Module01_BitmapPlus(Re_bmp)
        bmpP.BeginAccess()
        For i As Integer = 0 To Re_bmp.Width - 1
            For j As Integer = 0 To Re_bmp.Height - 1

                'グレーを算出
                Dim n_Gray As Integer = Gray_Calculation(bmpP.GetPixel(i, j).R, bmpP.GetPixel(i, j).G, bmpP.GetPixel(i, j).B) '赤緑青

                bmpP.SetPixel(i, j, Color.FromArgb(255, n_Gray, n_Gray, n_Gray)) 'アルファ(透明度)、赤、緑、青

            Next
        Next
        ' Bitmap処理の高速化終了
        bmpP.EndAccess()

        Return Re_bmp

    End Function

 


    '白黒
    Public Function siro_kuro(n_bmp As Bitmap, border As Integer) '画像,白黒のしきい値

        Dim Re_bmp As Bitmap = n_bmp.Clone

        ' Bitmap処理の高速化開始
        Dim bmpP As Module01_BitmapPlus = New Module01_BitmapPlus(Re_bmp)
        bmpP.BeginAccess()
        For i As Integer = 0 To Re_bmp.Width - 1
            For j As Integer = 0 To Re_bmp.Height - 1

                'グレーの色を返す
                Dim n_Gray As Integer = Gray_Calculation(bmpP.GetPixel(i, j).R, bmpP.GetPixel(i, j).G, bmpP.GetPixel(i, j).B) '赤緑青

                '白黒
                If n_Gray >= border Then 'しきい値
                    n_Gray = 255
                Else
                    n_Gray = 0
                End If

                bmpP.SetPixel(i, j, Color.FromArgb(255, n_Gray, n_Gray, n_Gray)) 'アルファ(透明度)、赤、緑、青

            Next
        Next
        ' Bitmap処理の高速化終了
        bmpP.EndAccess()

        Return Re_bmp

    End Function

 

 


    'エッジ算出
    Public Function rinkak_Calculation(n_arr(,) As Integer, a1() As Integer) '画像の2次元 値はグレースケール,倍率1-9 カーネル係数

        Dim len0 As Integer = n_arr.GetLength(0)    ' 0次元目の要素数を取得 
        Dim len1 As Integer = n_arr.GetLength(1)    ' 1次元目の要素数を取得
        Dim re_arr(len0, len1) As Integer

        For i As Integer = 0 To len0 - 1 '横
            For j As Integer = 0 To len1 - 1 '縦
                If i = 0 Or i = len0 - 1 Or j = 0 Or j = len1 - 1 Then ' 端
                    re_arr(i, j) = 0 '輪郭なしとします
                Else
                    re_arr(i, j) += n_arr(i - 1, j - 1) * a1(0) '左上
                    re_arr(i, j) += n_arr(i, j - 1) * a1(1) '上
                    re_arr(i, j) += n_arr(i + 1, j - 1) * a1(2) '右上
                    '
                    re_arr(i, j) += n_arr(i - 1, j) * a1(3) '左
                    re_arr(i, j) += n_arr(i, j) * a1(4) '真ん中
                    re_arr(i, j) += n_arr(i + 1, j) * a1(5) '右
                    '
                    re_arr(i, j) += n_arr(i - 1, j + 1) * a1(6) '左下
                    re_arr(i, j) += n_arr(i, j + 1) * a1(7) '下
                    re_arr(i, j) += n_arr(i + 1, j + 1) * a1(8) '右下
                End If
                If re_arr(i, j) > 255 Then
                    re_arr(i, j) = 255
                ElseIf re_arr(i, j) < 0 Then
                    re_arr(i, j) = 0
                End If

            Next
        Next

        Return re_arr

    End Function


    'エッジ画像を返す
    Public Function rinkak_ga(n_bmp As Bitmap, Black_white As Integer, kaneru() As Integer) '画像 , 白黒しきい値 ,倍率1-9 カーネル係数

        Dim Re_bmp As Bitmap = n_bmp.Clone

        ' Bitmap処理の高速化開始
        Dim bmpP1 As Module01_BitmapPlus = New Module01_BitmapPlus(Re_bmp)

        bmpP1.BeginAccess()

        Dim len0 As Integer = Re_bmp.Width - 1    ' 0次元目の要素数を取得 
        Dim len1 As Integer = Re_bmp.Height - 1    ' 1次元目の要素数を取得
        Dim n_arr1(len0, len1) As Integer
        Dim n_arr2(len0, len1) As Integer

        For i As Integer = 0 To len0 - 1
            For j As Integer = 0 To len1 - 1
                'グレーの色を返す
                Dim n_Gray As Integer = Gray_Calculation(bmpP1.GetPixel(i, j).R, bmpP1.GetPixel(i, j).G, bmpP1.GetPixel(i, j).B) '赤緑青

                '白黒
                If n_Gray >= Black_white Then 'しきい値
                    n_Gray = 255
                Else
                    n_Gray = 0
                End If
                n_arr1(i, j) = n_Gray
            Next
        Next

        n_arr2 = rinkak_Calculation(n_arr1, kaneru) '画像の2次元 値はグレースケール,倍率1-9

        For i As Integer = 0 To len0 - 1
            For j As Integer = 0 To len1 - 1

                bmpP1.SetPixel(i, j, Color.FromArgb(255, n_arr2(i, j), n_arr2(i, j), n_arr2(i, j))) 'アルファ(透明度)、赤、緑、青

            Next
        Next

        ' Bitmap処理の高速化終了
        bmpP1.EndAccess()


        Return Re_bmp

    End Function

 

 

 

End Module

ff3のなかみ

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form03_rinkaku
    Inherits System.Windows.Forms.Form

    'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Windows フォーム デザイナーで必要です。
    Private components As System.ComponentModel.IContainer

    'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。
    'Windows フォーム デザイナーを使用して変更できます。  
    'コード エディターを使って変更しないでください。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.PictureBox2 = New System.Windows.Forms.PictureBox()
        Me.PictureBox3 = New System.Windows.Forms.PictureBox()
        Me.TrackBar1 = New System.Windows.Forms.TrackBar()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.PictureBox4 = New System.Windows.Forms.PictureBox()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.PictureBox5 = New System.Windows.Forms.PictureBox()
        Me.Button4 = New System.Windows.Forms.Button()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox5, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(65, 98)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(175, 154)
        Me.PictureBox1.TabIndex = 1
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(271, 54)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(93, 23)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "グレー"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(476, 54)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(93, 23)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "白黒"
        Me.Button2.UseVisualStyleBackColor = True
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(271, 98)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(175, 154)
        Me.PictureBox2.TabIndex = 5
        Me.PictureBox2.TabStop = False
        '
        'PictureBox3
        '
        Me.PictureBox3.Location = New System.Drawing.Point(476, 98)
        Me.PictureBox3.Name = "PictureBox3"
        Me.PictureBox3.Size = New System.Drawing.Size(175, 154)
        Me.PictureBox3.TabIndex = 6
        Me.PictureBox3.TabStop = False
        '
        'TrackBar1
        '
        Me.TrackBar1.Location = New System.Drawing.Point(476, 258)
        Me.TrackBar1.Maximum = 255
        Me.TrackBar1.Name = "TrackBar1"
        Me.TrackBar1.Size = New System.Drawing.Size(175, 45)
        Me.TrackBar1.TabIndex = 8
        Me.TrackBar1.Value = 120
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(531, 291)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(38, 12)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Label1"
        '
        'PictureBox4
        '
        Me.PictureBox4.Location = New System.Drawing.Point(65, 337)
        Me.PictureBox4.Name = "PictureBox4"
        Me.PictureBox4.Size = New System.Drawing.Size(175, 154)
        Me.PictureBox4.TabIndex = 10
        Me.PictureBox4.TabStop = False
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(65, 308)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(93, 23)
        Me.Button3.TabIndex = 11
        Me.Button3.Text = "輪郭"
        Me.Button3.UseVisualStyleBackColor = True
        '
        'PictureBox5
        '
        Me.PictureBox5.Location = New System.Drawing.Point(271, 337)
        Me.PictureBox5.Name = "PictureBox5"
        Me.PictureBox5.Size = New System.Drawing.Size(175, 154)
        Me.PictureBox5.TabIndex = 12
        Me.PictureBox5.TabStop = False
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(271, 308)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(93, 23)
        Me.Button4.TabIndex = 13
        Me.Button4.Text = "エッジ消去"
        Me.Button4.UseVisualStyleBackColor = True
        '
        'Form03_rinkaku
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 559)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.PictureBox5)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.PictureBox4)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TrackBar1)
        Me.Controls.Add(Me.PictureBox3)
        Me.Controls.Add(Me.PictureBox2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form03_rinkaku"
        Me.Text = "Form03_rinkaku"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox5, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents PictureBox1 As PictureBox
    Friend WithEvents Button1 As Button
    Friend WithEvents Button2 As Button
    Friend WithEvents PictureBox2 As PictureBox
    Friend WithEvents PictureBox3 As PictureBox
    Friend WithEvents TrackBar1 As TrackBar
    Friend WithEvents Label1 As Label
    Friend WithEvents PictureBox4 As PictureBox
    Friend WithEvents Button3 As Button
    Friend WithEvents PictureBox5 As PictureBox
    Friend WithEvents Button4 As Button
End Class

ff3


'AForge.Imaging.Filters をインポートします。

Public Class Form03_rinkaku


    Dim Black_white As Integer = 0 'しきい値


    Private Sub Form03_rinkaku_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        Dim FilePath As String = "./test2.bmp"
        PictureBox1.Image = System.Drawing.Image.FromFile(FilePath)

        Black_white = CInt(TrackBar1.Value) 'しきい値
        Label1.Text = TrackBar1.Value

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'グレー画像を返す
        PictureBox2.Image = RGB_to_Gray(PictureBox1.Image.Clone) '画像

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        '白黒
        PictureBox3.Image = siro_kuro(PictureBox1.Image.Clone, Black_white) '画像,白黒のしきい値

    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Black_white = CInt(TrackBar1.Value) 'しきい値
        Label1.Text = TrackBar1.Value
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim kaneru(8) As Integer 'カーネル係数

        kaneru = {0, 1, 0,
                  1, -4, 1,
                  0, 1, 0} 'カーネル係数

        kaneru = {1, 1, 1,
                  1, -8, 1,
                  1, 1, 1} 'カーネル係数
        'エッジ画像を返す
        PictureBox4.Image = rinkak_ga(PictureBox1.Image.Clone, Black_white, Module02_image_processing.kaneru2) '画像 , 白黒しきい値 ,倍率1-9 カーネル係数

 

    End Sub


End Class

ff2の中

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form02_GaussianBlur
    Inherits System.Windows.Forms.Form

    'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Windows フォーム デザイナーで必要です。
    Private components As System.ComponentModel.IContainer

    'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。
    'Windows フォーム デザイナーを使用して変更できます。  
    'コード エディターを使って変更しないでください。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.PictureBox2 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.PictureBox3 = New System.Windows.Forms.PictureBox()
        Me.PictureBox4 = New System.Windows.Forms.PictureBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.btn_GaussianBlur = New System.Windows.Forms.Button()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.Label9 = New System.Windows.Forms.Label()
        Me.Label10 = New System.Windows.Forms.Label()
        Me.Label11 = New System.Windows.Forms.Label()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(33, 103)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(285, 205)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(337, 103)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(285, 205)
        Me.PictureBox2.TabIndex = 1
        Me.PictureBox2.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(56, 35)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(126, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "高速画像処理"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'PictureBox3
        '
        Me.PictureBox3.Location = New System.Drawing.Point(33, 330)
        Me.PictureBox3.Name = "PictureBox3"
        Me.PictureBox3.Size = New System.Drawing.Size(285, 205)
        Me.PictureBox3.TabIndex = 3
        Me.PictureBox3.TabStop = False
        '
        'PictureBox4
        '
        Me.PictureBox4.Location = New System.Drawing.Point(337, 330)
        Me.PictureBox4.Name = "PictureBox4"
        Me.PictureBox4.Size = New System.Drawing.Size(285, 205)
        Me.PictureBox4.TabIndex = 4
        Me.PictureBox4.TabStop = False
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(642, 103)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(38, 12)
        Me.Label1.TabIndex = 7
        Me.Label1.Text = "Label1"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(657, 177)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(38, 12)
        Me.Label2.TabIndex = 8
        Me.Label2.Text = "Label2"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(668, 209)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(38, 12)
        Me.Label3.TabIndex = 9
        Me.Label3.Text = "Label3"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(668, 228)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(38, 12)
        Me.Label4.TabIndex = 10
        Me.Label4.Text = "Label4"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(144, 78)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(38, 12)
        Me.Label5.TabIndex = 11
        Me.Label5.Text = "Label5"
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(447, 78)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(38, 12)
        Me.Label6.TabIndex = 12
        Me.Label6.Text = "Label6"
        '
        'btn_GaussianBlur
        '
        Me.btn_GaussianBlur.Location = New System.Drawing.Point(260, 15)
        Me.btn_GaussianBlur.Name = "btn_GaussianBlur"
        Me.btn_GaussianBlur.Size = New System.Drawing.Size(124, 62)
        Me.btn_GaussianBlur.TabIndex = 13
        Me.btn_GaussianBlur.Text = "ガウスぼかし"
        Me.btn_GaussianBlur.UseVisualStyleBackColor = True
        '
        'Label7
        '
        Me.Label7.AutoSize = True
        Me.Label7.Location = New System.Drawing.Point(174, 315)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(38, 12)
        Me.Label7.TabIndex = 14
        Me.Label7.Text = "Label7"
        '
        'Label8
        '
        Me.Label8.AutoSize = True
        Me.Label8.Location = New System.Drawing.Point(447, 315)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(38, 12)
        Me.Label8.TabIndex = 15
        Me.Label8.Text = "Label8"
        '
        'Label9
        '
        Me.Label9.AutoSize = True
        Me.Label9.Location = New System.Drawing.Point(677, 396)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(38, 12)
        Me.Label9.TabIndex = 16
        Me.Label9.Text = "Label9"
        '
        'Label10
        '
        Me.Label10.AutoSize = True
        Me.Label10.Location = New System.Drawing.Point(671, 425)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(44, 12)
        Me.Label10.TabIndex = 17
        Me.Label10.Text = "Label10"
        '
        'Label11
        '
        Me.Label11.AutoSize = True
        Me.Label11.Location = New System.Drawing.Point(679, 464)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(44, 12)
        Me.Label11.TabIndex = 18
        Me.Label11.Text = "Label11"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(568, 6)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(54, 19)
        Me.TextBox1.TabIndex = 19
        '
        'Form02_GaussianBlur
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 561)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label11)
        Me.Controls.Add(Me.Label10)
        Me.Controls.Add(Me.Label9)
        Me.Controls.Add(Me.Label8)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.btn_GaussianBlur)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PictureBox4)
        Me.Controls.Add(Me.PictureBox3)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox2)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form02_GaussianBlur"
        Me.Text = "Form1"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents PictureBox1 As PictureBox
    Friend WithEvents PictureBox2 As PictureBox
    Friend WithEvents Button1 As Button
    Friend WithEvents PictureBox3 As PictureBox
    Friend WithEvents PictureBox4 As PictureBox
    Friend WithEvents Label1 As Label
    Friend WithEvents Label2 As Label
    Friend WithEvents Label3 As Label
    Friend WithEvents Label4 As Label
    Friend WithEvents Label5 As Label
    Friend WithEvents Label6 As Label
    Friend WithEvents btn_GaussianBlur As Button
    Friend WithEvents Label7 As Label
    Friend WithEvents Label8 As Label
    Friend WithEvents Label9 As Label
    Friend WithEvents Label10 As Label
    Friend WithEvents Label11 As Label
    Friend WithEvents TextBox1 As TextBox
End Class

ff2

 


Public Class Form02_GaussianBlur

 

    Dim bbb As Bitmap = New Bitmap("./test1.bmp")


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        init_label()

        PictureBox1.Image = bbb


    End Sub

 

    ' ラベル初期値設定

    Private Sub init_label()

        Label5.Text = "原画像"

        Label6.Text = "パラメータ=2"

        Label7.Text = "パラメータ=4"

        Label8.Text = "パラメータ=8"

        Label10.Text = "所要時間(ms)"

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Bitmapオブジェクト生成
        Dim bmp As Bitmap
        If PictureBox1.Image Is Nothing = True Then
            ' Bitmapを新規生成
            bmp = New Bitmap(512, 512)
        Else
            ' BitmapをpictureBoxから取得
            bmp = New Bitmap(PictureBox1.Image)
        End If

        ' Bitmap処理の高速化開始
        Dim bmpP As Module01_BitmapPlus = New Module01_BitmapPlus(bmp)
        bmpP.BeginAccess()
        For i As Integer = 0 To bmp.Width - 1
            For j As Integer = 0 To bmp.Height - 1
                ' Bitmapの色取得
                'Dim bmpCol As Color = bmp.GetPixel(i, j)
                Dim bmpCol As Color = bmpP.GetPixel(i, j)
                If bmpCol.R = 0 Then
                    ' Bitmapの色設定
                    'bmp.SetPixel(i, j, Color.FromArgb(255, 255, 0, 0))
                    bmpP.SetPixel(i, j, Color.FromArgb(255, 255, 0, 0))
                Else
                    'bmp.SetPixel(i, j, Color.FromArgb(255, 0, 0, 0))
                    bmpP.SetPixel(i, j, Color.FromArgb(255, 0, 0, 0))
                End If
            Next
        Next
        ' Bitmap処理の高速化終了
        bmpP.EndAccess()
        PictureBox1.Image = bmp
    End Sub

 

 

    ' 原画像表示、ガウスぼかし画像(パラメータ2,4,8)を表示
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btn_GaussianBlur.Click
        Dim start_ As Integer = System.Environment.TickCount

        'If TextBox1.Text = "" Then Return

        Dim fname As String = TextBox1.Text

        Dim bmp As Image = Nothing

        Dim tmp As Bitmap = Nothing


        bmp = bbb

 

        ' パラメータ2のガウスぼかし画像を表示

        If Not PictureBox2.Image Is Nothing Then

            PictureBox2.Image.Dispose()

            PictureBox2.Image = Nothing

        End If

        tmp = GaussianBlurXY(bmp, 2)

        If tmp Is Nothing Then

            Return

        End If

        PictureBox2.Image = tmp

        PictureBox2.Refresh()

 

        ' パラメータ4のガウスぼかし画像を表示

        If Not PictureBox3.Image Is Nothing Then

            PictureBox3.Image.Dispose()

            PictureBox3.Image = Nothing

        End If

        tmp = GaussianBlurXY(bmp, 4)

        If tmp Is Nothing Then

            Return

        End If

        PictureBox3.Image = tmp

        PictureBox3.Refresh()

 

        ' パラメータ8のガウスぼかし画像を表示

        If Not PictureBox4.Image Is Nothing Then

            PictureBox4.Image.Dispose()

            PictureBox4.Image = Nothing

        End If

        tmp = GaussianBlurXY(bmp, 8)

        If tmp Is Nothing Then

            Return

        End If

        PictureBox4.Image = tmp

        PictureBox4.Refresh()

 

        Dim end_ As Integer = System.Environment.TickCount

        Label10.Text = "所要時間:" & (end_ - start_) & "(ms)"
    End Sub
End Class

Module01_BitmapPlus.vb

 

'Bitmap処理の高速化

Imports System.Drawing
Imports System.Drawing.Imaging

Public Class Module01_BitmapPlus
    ''' <summary>
    ''' オリジナルのBitmapオブジェクト
    ''' </summary>
    Private _bmp As Bitmap = Nothing

    ''' <summary>
    ''' Bitmapに直接アクセスするためのオブジェクト
    ''' </summary>
    Private _img As BitmapData = Nothing

    ''' <summary>
    ''' コンストラク
    ''' </summary>
    ''' <param name="original"></param>
    Public Sub New(ByVal original As Bitmap)
        ' オリジナルのBitmapオブジェクトを保存
        _bmp = original
    End Sub

    ''' <summary>
    ''' Bitmap処理の高速化開始
    ''' </summary>
    Public Sub BeginAccess()
        ' Bitmapに直接アクセスするためのオブジェクト取得(LockBits)
        _img = _bmp.LockBits(New Rectangle(0, 0, _bmp.Width, _bmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadWrite,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    End Sub

    ''' <summary>
    ''' Bitmap処理の高速化終了
    ''' </summary>
    Public Sub EndAccess()
        If _img Is Nothing = False Then
            ' Bitmapに直接アクセスするためのオブジェクト開放(UnlockBits)
            _bmp.UnlockBits(_img)
            _img = Nothing
        End If
    End Sub

    ''' <summary>
    ''' BitmapのGetPixel同等
    ''' </summary>
    ''' <param name="x">X座標</param>
    ''' <param name="y">Y座標</param>
    ''' <returns>Colorオブジェクト</returns>
    Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As Color
        If _img Is Nothing = True Then
            ' Bitmap処理の高速化を開始していない場合はBitmap標準のGetPixel
            Return _bmp.GetPixel(x, y)
        End If
        Dim adr As IntPtr = _img.Scan0
        Dim pos As Integer = x * 3 + _img.Stride * y
        Dim b As Byte = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 0)
        Dim g As Byte = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 1)
        Dim r As Byte = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 2)
        Return Color.FromArgb(r, g, b)
    End Function


    '''// <summary>
    ''' BitmapのSetPixel同等
    ''' </summary>
    ''' <param name="x">X座標</param>
    ''' <param name="y">Y座標</param>
    ''' <param name="col">Colorオブジェクト</param>
    Public Sub SetPixel(ByVal x As Integer, ByVal y As Integer, ByVal col As Color)
        If _img Is Nothing = True Then
            ' Bitmap処理の高速化を開始していない場合はBitmap標準のSetPixel
            _bmp.SetPixel(x, y, col)
            Return
        End If

        ' Bitmap処理の高速化を開始している場合はBitmapメモリへの直接アクセス
        Dim adr As IntPtr = _img.Scan0
        Dim pos As Integer = x * 3 + _img.Stride * y
        System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 0, col.B)
        System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 1, col.G)
        System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 2, col.R)
    End Sub
End Class