View Single Post
 
Old 06-26-2015, 08:07 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default Tweaked the code a little...

It now run's alot faster.

Code:
Imports System.Drawing
Imports EdgeDetection

Public Class EdgeDetection
	
	Private m_Original As Bitmap  
	Private m_Threshold As Double 
	
	Private m_Detected As Bitmap 
	Private m_Rectangle As Rectangle 
	Private m_Data As Imaging.BitmapData
	Private m_Ptr As IntPtr 
	Private m_ByteCount As Integer 
    Private m_RGBValues() As Byte                   ' The RGB Values
    Private m_RGBOutput() As Byte                   ' The RGB Values
    
    Private Black() As Byte = {0, 0, 0}
    Private White() As Byte = {255, 255, 255}

    Public Sub New()
        Me.new(Nothing)
    End Sub
    Public Sub New(Img As Bitmap, Optional Threshold As Double = 0.1)
        Me.m_Original = Img
        Me.m_Threshold = Threshold
    End Sub

    Public Property Image As Bitmap
        Get
            Return Me.m_Original
        End Get
        Set(value As Bitmap)
            Me.m_Original = value

        End Set
    End Property
    Public ReadOnly Property Detected As Bitmap
        Get
            Detect()
            Return Me.m_Detected
        End Get
    End Property

    Private Sub Detect()
        Dim OriginalWidth As Integer = Me.m_Original.Width
        Dim PixelMatrix(26) As Byte
        Dim Start As Integer = System.Environment.TickCount
        Dim Offset As Integer = 0
        Dim StepSize As Integer = 3

        m_Rectangle = New Rectangle(0, 0, Me.m_Original.Width, Me.m_Original.Height)
        m_Detected = m_Original.Clone(m_Rectangle, m_Original.PixelFormat)

        m_Data = m_Detected.LockBits(m_Rectangle, Imaging.ImageLockMode.ReadWrite, m_Detected.PixelFormat)
        ' Get the address of the first line. 
        m_ByteCount = m_Data.Stride * m_Detected.Height
        '= not RGB But BGR
        ReDim m_RGBValues(m_ByteCount)
        ReDim m_RGBOutput(m_ByteCount)
        ' Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(m_Data.Scan0, m_RGBValues, 0, m_ByteCount)
        System.Runtime.InteropServices.Marshal.Copy(m_Data.Scan0, m_RGBOutput, 0, m_ByteCount)
        Select Case m_Detected.PixelFormat
            Case Imaging.PixelFormat.Format16bppArgb1555, Imaging.PixelFormat.Format16bppGrayScale, Imaging.PixelFormat.Format16bppRgb555
            Case Imaging.PixelFormat.Format32bppArgb, Imaging.PixelFormat.Format32bppPArgb, Imaging.PixelFormat.Format32bppRgb
                Offset = 1
                StepSize = 4
        End Select
        '
        For Row As Integer = 1 To m_Detected.Height - 2
            For Column As Integer = Offset To m_Data.Stride - 9 Step StepSize
                System.Buffer.BlockCopy(m_RGBValues, ((Row - 1) * m_Data.Stride) + Column, PixelMatrix, 0, 9)
                System.Buffer.BlockCopy(m_RGBValues, (Row * m_Data.Stride) + Column, PixelMatrix, 9, 9)
                System.Buffer.BlockCopy(m_RGBValues, ((Row + 1) * m_Data.Stride) + Column, PixelMatrix, 18, 9)
                If IsBrighterThenTreshold(PixelMatrix) Then
                    System.Buffer.BlockCopy(Black, 0, m_RGBOutput, (Row * m_Data.Stride) + Column + 3, 3)
                Else
                    System.Buffer.BlockCopy(White, 0, m_RGBOutput, (Row * m_Data.Stride) + Column + 3, 3)
                End If
            Next
        Next
        ' Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(m_RGBOutput, 0, m_Data.Scan0, m_ByteCount)
        ' Unlock the bits.
        m_Detected.UnlockBits(m_Data)

        Debug.Print(((System.Environment.TickCount - Start) / 1000).ToString)
    End Sub
    
    Private Function GetBrightness(R As Double, G As Double, B As Double) As Double
        ' Return (R + R + B + G + G + G) / 6
        Return (0.2126 * R + 0.7152 * G + 0.0722 * B)
        ' Return(.299 * R^2 + .587 * G^2 + .114 * B^2)
        ' Return (R * R * 0.241 + G * G * 0.691 + B * B * 0.068) ^ 0.5
    End Function

    Private Function IsBrighterThenTreshold(RGB() As Byte) As Boolean
        Dim Current As Double
        Dim Brightest As Double = GetBrightness(RGB(0), RGB(2), RGB(1))
        For I As Integer = 3 To 26 Step 3
            Current = GetBrightness(RGB(I), RGB(I + 2), RGB(I + 1))
            If Current > Brightest Then
                Brightest = Current
            End If
        Next
        Return (Brightest - GetBrightness(RGB(12), RGB(14), RGB(13)) >= m_Threshold)
    End Function
End Class
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote