Quantcast
Channel: SourceHints » classes
Viewing all articles
Browse latest Browse all 5

Image Processing using AForge with COM Object

$
0
0

Post to Twitter

How to do Image Processing using AForge with COM+ Object

  • Requirements: Visual Studio 2005, 2008, 2010
  • Programming Level: Advance
  • Language: Visual Basic.Net
  • AForge.dll(v 2.2.0.0)
  • AForge.Imaging.dll(2.2.2.0)
  • AForge.Math.dll(2.2.2.0)

Project references

  • AForge
  • AForge.Imaging
  • AForge.Imaging.Filters
  • AForge.Math.Geometry
  • System.Collections.Generic
  • System.Drawing
  • System.Drawing.Imaging
  • System.Reflection

Scenario

Microsoft Visual Basic 6 as we all know is the ancestor of Visual Basic.Net the current versions of Visual Studio is 2012 upon this writing, and still due to the requirements that has passed to me that need to use Visual Basic 6.0 as the main system development as far as Image Processing is concerned. For those who have not known to them that Visual Basic.Net Application can still be use in vb6 to enhance its capabilities. So in this connection, I would like to introduce to you the COM+ Object, we can create an application in visual Basic.Net in the form of a Dynamic Link Library (DLL), a COM Class application that generates TLB Files, in which stands for Translation Lookaside Buffer this TLB file can then be access as COM+ component in Visual Basic 6 environment.

What is Translation Lookaside Buffer?

A TLB is a file extension for Object Linking and Embedding (OLE) used in Microsoft Visual C++ runtime. This file extension is accessible as COM (Component Object Model) in VB6 environment. As you referenced it from VB6 Project its class can be use as common class object in VB6 environment thus will expose its properties methods and event as regular class. A third Library is being use in this project the AForge.Net Libraries.

Shape Detector Class

' *********** Class Created by Eng'r Nolan F. Sunico **********************************
' ************* Created February 10, 2013 - 9:13 PM ***********************************
' ************* Visit us at http://www.sourcehints.com ********************************
' *************************************************************************************
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.Reflection
' ******** AForege.Net Reference Assembly Version 2.2.0.0 *****************************
Imports AForge
Imports AForge.Imaging
Imports AForge.Imaging.Filters
Imports AForge.Math.Geometry
' * The AForege.Net Assembly is not included in this project as we are not authorized *
' **** to distribute its assemblies please download the 2.2.0.0 version from their **** 
' **** Website at http://www.aforgenet.com/ the Official Website of AForge.Net ********
' *************************************************************************************
Namespace Geometry
    <ComClass(ShapeDetector.ClassId, ShapeDetector.InterfaceId, ShapeDetector.EventsId)> _
    Public Class ShapeDetector
#Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "1c033e28-ef50-4650-92d4-a4aeb11c5e0a"
        Public Const InterfaceId As String = "2e625fec-e82f-446a-9358-fef000ef7989"
        Public Const EventsId As String = "969d56d7-59cb-455a-946e-b54c41479ca5"
#End Region
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
#Region "Enumeration"
        Public Enum ShapePolygon
            [Nothing] = 0
            Square = 1
            Circle = 2
            EquilateralTriangle = 3
            Rectangle = 4
            Pentagon = 5
            Parallelogram = 6
            IsoscelesTriangle = 7
            Trapezoid = 8
            Rhombus = 9
        End Enum
#End Region
#Region "Private Subroutines"
        Private Function ProcessImage(bitmap As Bitmap) As ShapePolygon
            Dim SP As ShapePolygon = ShapePolygon.Nothing
            Try
                ' lock image
                Dim bmpData As BitmapData = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat)
                ' step 1 - turn background to black
                Dim colorFilter As New ColorFiltering()
                colorFilter.Red = New IntRange(0, 64)
                colorFilter.Green = New IntRange(0, 64)
                colorFilter.Blue = New IntRange(0, 64)
                colorFilter.FillOutsideRange = False
                colorFilter.ApplyInPlace(bmpData)
                ' step 2 - locating objects
                Dim blobCounter As New BlobCounter()
                blobCounter.FilterBlobs = True
                blobCounter.MinHeight = 5
                blobCounter.MinWidth = 5
                blobCounter.ProcessImage(bmpData)
                Dim blobs As Blob() = blobCounter.GetObjectsInformation()
                bitmap.UnlockBits(bmpData)
                ' step 3 - check objects' type and highlight
                Dim shapeChecker As New SimpleShapeChecker()
                Dim i As Integer = 0, n As Integer = blobs.Length
                While i < n
                    Dim edgePoints As List(Of IntPoint) = blobCounter.GetBlobsEdgePoints(blobs(i))
                    Dim center As DoublePoint
                    Dim radius As Single
                    Dim corners As List(Of IntPoint) = Nothing
                    ' is circle ?
                    If shapeChecker.IsCircle(edgePoints, center, radius) Then
                        SP = ShapePolygon.Circle
                        Dim st As AForge.Math.Geometry.ShapeType = shapeChecker.CheckShapeType(edgePoints)
                        Select Case st
                            Case ShapeType.Quadrilateral

                            Case ShapeType.Unknown
                                MsgBox("Unknown")
                        End Select
                    ElseIf shapeChecker.IsConvexPolygon(edgePoints, corners) Then
                        Dim subType As PolygonSubType = shapeChecker.CheckPolygonSubType(corners)
                        Dim s As String = ""
                        Select Case corners.Count
                            Case 3 'Triangle
                                Select Case subType
                                    Case PolygonSubType.IsoscelesTriangle
                                        SP = ShapePolygon.IsoscelesTriangle
                                    Case PolygonSubType.EquilateralTriangle
                                        SP = ShapePolygon.EquilateralTriangle
                                    Case Else
                                        SP = ShapePolygon.EquilateralTriangle
                                End Select
                                Exit Try
                            Case 4 ' Quadrilateral
                                Select Case subType
                                    Case PolygonSubType.Square
                                        SP = ShapePolygon.Square
                                    Case PolygonSubType.Rectangle
                                        SP = ShapePolygon.Rectangle
                                    Case PolygonSubType.Parallelogram
                                        SP = ShapePolygon.Parallelogram
                                    Case PolygonSubType.Rhombus
                                        SP = ShapePolygon.Rhombus
                                    Case PolygonSubType.Trapezoid
                                        SP = ShapePolygon.Trapezoid
                                End Select
                                Exit Try
                        End Select
                    Else
                        SP = ShapePolygon.Pentagon
                    End If
                    i += 1
                End While
                ' put new image to clipboard
                Clipboard.SetDataObject(bitmap)
                ' and to picture box
                Return SP
            Catch ex As Exception
                '
            End Try
            Return SP
        End Function
#End Region
#Region "Subroutines"
        'Public Subroutines
        Private Function ToPointsArray(ByVal points As List(Of IntPoint)) As System.Drawing.Point()
            Dim array As System.Drawing.Point() = New System.Drawing.Point(points.Count - 1) {}
            Dim i As Integer = 0, n As Integer = points.Count
            While i < n
                array(i) = New System.Drawing.Point(points(i).X, points(i).Y)
                i += 1
            End While

            Return array
        End Function
        Public Function ProcessImage(FileName As String) As ShapePolygon
            Dim SPoly As ShapePolygon = ShapePolygon.Nothing
            Dim mBitmap As Bitmap = Nothing
            Try
                mBitmap = New Bitmap(FileName)
                SPoly = ProcessImage(mBitmap)
            Catch ex As Exception
                SPoly = ShapePolygon.Nothing
            End Try
            Return SPoly
        End Function
        Public Function ProcessCropImage() As ShapePolygon
            Dim SPoly As ShapePolygon = ShapePolygon.Nothing
            Try
                SPoly = ProcessImage(CommonImage.CropBitmap)
            Catch ex As Exception
                SPoly = ShapePolygon.Nothing
            End Try
            Return SPoly
        End Function
#End Region
    End Class
End Namespace

Shape Recognizer

' *********** Class Created by Eng'r Nolan F. Sunico **********************************
' ************* Created February 10, 2013 - 9:13 PM ***********************************
' ************* Visit us at http://www.sourcehints.com ********************************
' *************************************************************************************
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports Microsoft.VisualBasic.Compatibility.VB6
' ******** AForege.Net Reference Assembly Version 2.2.0.0 *****************************
Imports AForge
Imports AForge.Imaging.Filters
Imports AForge.Imaging
Imports AForge.Math.Geometry
' * The AForege.Net Assembly is not included in this project as we are not authorized *
' **** to distribute its assemblies please download the 2.2.2.0 version from their **** 
' **** Website at http://www.aforgenet.com/ the Official Website of AForge.Net ********
' *************************************************************************************
Imports Image = System.Drawing.Image 'Remove ambiguousness between AForge.Image and System.Drawing.Image
Imports Point = System.Drawing.Point 'Remove ambiguousness between AForge.Point and System.Drawing.Point

Namespace Geometry
    <ComClass(ShapeRecognizer.ClassId, ShapeRecognizer.InterfaceId, ShapeRecognizer.EventsId)> _
    Public Class ShapeRecognizer
#Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "66e6a97a-68e3-4789-8929-8aa030235d58"
        Public Const InterfaceId As String = "450b4285-9805-4b16-96dc-57bd05146d52"
        Public Const EventsId As String = "d3189765-7fd0-4175-9b30-fd6f0174a034"
#End Region
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
#Region "Class"
        Private CommonSeq As FiltersSequence  'Commonly filter sequence to be used 
        ''' <summary>
        ''' Crop Image from Source Image given the Left
        ''' Top, Width and Height of a resultant image as parameters.
        ''' </summary>
        ''' <param name="ImageFilename">The Image Filename to be Cropped</param>
        ''' <param name="x">The Left or X Dimension</param>
        ''' <param name="y">The Top or Y Dimension</param>
        ''' <param name="width">The Width of an Image to be cropped</param>
        ''' <param name="height">The Height of an Image to be cropped</param>
        ''' <remarks></remarks>
        Public Sub CropImage(ByVal ImageFilename As String, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ImageFileNameToSave As String)
            Dim bmp As New Bitmap(ImageFilename)
            Try
                Dim crop As New Crop(New Rectangle(x, y, width, height))
                bmp = crop.Apply(bmp)
                Geometry.CommonImage.CropBitmap = bmp
                bmp.Save(ImageFileNameToSave, System.Drawing.Imaging.ImageFormat.Bmp)
            Catch ex As Exception
                ' Error Occurred
                MsgBox(ex.Message)
            End Try
        End Sub
#End Region
    End Class
End Namespace

Common Image Class

' *********** Class Created by Eng'r Nolan F. Sunico **********************************
' ************* Created February 10, 2013 - 9:13 PM ***********************************
' ************* Visit us at http://www.sourcehints.com ********************************
' *************************************************************************************

Imports System.Drawing

Namespace Geometry
    <ComClass(CommonImage.ClassId, CommonImage.InterfaceId, CommonImage.EventsId)> _
    Public Class CommonImage
#Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "e3369f65-21c3-4d4a-8983-711fee3e4b3b"
        Public Const InterfaceId As String = "e9530bcf-1598-4681-b9e0-aff5eab29553"
        Public Const EventsId As String = "10ac4620-f9a8-4984-8742-a0c680342839"
#End Region
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
#Region "FileSystem"
        Public Function CopyDirectory(SourceDirectory As String, DestinationDirectory As String) As Boolean
            Dim Success As Boolean = False
            Try
                My.Computer.FileSystem.CopyDirectory(SourceDirectory, SourceDirectory)
                Success = True
            Catch ex As Exception
                Success = False
            End Try
            Return Success
        End Function
        Public Function CopyFile(Source As String, Destination As String) As Boolean
            Dim Success As Boolean = False
            Try
                My.Computer.FileSystem.CopyFile(Source, Destination)
                Success = True
            Catch ex As Exception
                Success = False
            End Try
            Return Success
        End Function
        Public Function FileExist(Filename As String) As Boolean
            Dim ret As Boolean = False
            Try
                ret = My.Computer.FileSystem.FileExists(Filename)
            Catch ex As Exception
                ret = False
            End Try
            Return ret
        End Function
        Public Function DirectoryExist(Filename As String) As Boolean
            Dim ret As Boolean = False
            Try
                ret = My.Computer.FileSystem.DirectoryExists(Filename)
            Catch ex As Exception
                ret = False
            End Try
            Return ret
        End Function
        Public Function CreateDirectory(DirName As String) As Boolean
            Dim Success As Boolean = False
            Try
                If Not DirectoryExist(DirName) Then
                    My.Computer.FileSystem.CreateDirectory(DirName)
                End If
                Success = True
            Catch ex As Exception
                Success = False
            End Try
            Return Success
        End Function
        Public Function GetSystemDrive() As String
            Dim SysDrive As String = ""
            Try
                SysDrive = Environment.GetEnvironmentVariable("SystemDrive")
            Catch ex As Exception
                SysDrive = ""
            End Try
            Return SysDrive
        End Function

        Public Function GetWindowsDirectory() As String
            Dim WinDir As String = ""
            Try
                WinDir = Environment.GetEnvironmentVariable("SystemRoot")
            Catch ex As Exception
                WinDir = ""
            End Try
            Return WinDir
        End Function
        Public Function GetTempDirectoryPath() As String
            Dim TempDir As String = ""
            Try
                TempDir = My.Computer.FileSystem.SpecialDirectories.Temp
            Catch ex As Exception
                TempDir = ""
            End Try
            Return TempDir
        End Function
#End Region
#Region "Public Properties"
        Private Shared _CropBitmap As Bitmap
        Public Shared Property CropBitmap() As Bitmap
            Get
                Return _CropBitmap
            End Get
            Set(ByVal value As Bitmap)
                _CropBitmap = value
            End Set
        End Property
#End Region
#Region "Subroutines"
        Public Function GetCommonBitmap() As Bitmap
            Return CropBitmap
        End Function
#End Region
    End Class
End Namespace

Compiling the COM+ Library

Create a project in Visual Studio 2010, a Visual Basic>net Project and on a template select Class Library, and then remove or delete the default Class Library created by the process, right click the project name -> Add->Class from Popup menu and on the Template select COM Class, this will generate automatically a COM+ class library and exposed its public properties as component objects. to download the complete project please click the link below.

Sample Image Processing Project

Kindly like our Fan Page at: Like Us at SourceHints

ZIP Password: ilikesourcehints

ComPlus Project Download

Post to Twitter

Keywords: , , , , , , , , , , , ,

Other reading this article are also reading these:


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images