I want to share this generic usage method that I've wrote to simplify things:
''' <summary>
''' Finds a part of an image inside other image and returns the top-left corner coordinates and it's similarity percent.
''' </summary>
''' <param name="BaseImage">
''' Indicates the base image.
''' </param>
''' <param name="ImageToFind">
''' Indicates the image to find in the base image.
''' </param>
''' <param name="Similarity">
''' Indicates the similarity percentage to compare the images.
''' A value of '100' means identical image.
''' Note: High percentage values with big images could take several minutes to finish.
''' </param>
''' <returns>AForge.Imaging.TemplateMatch().</returns>
Private Function FindImage(ByVal BaseImage As Bitmap,
ByVal ImageToFind As Bitmap,
ByVal Similarity As Double) As AForge.Imaging.TemplateMatch()
Dim SingleSimilarity As Single
' Translate the readable similarity percent value to Single value.
Select Case Similarity
Case Is < 0.1R, Is > 100.0R ' Value is out of range.
Throw New Exception(String.Format("Similarity value of '{0}' is out of range, range is from '0.1' to '100.0'",
CStr(Similarity)))
Case Is = 100.0R ' Identical image comparission.
SingleSimilarity = 1.0F
Case Else ' Image comparission with specific similarity.
SingleSimilarity = Convert.ToSingle(Similarity) / 100.0F
End Select
' Set the similarity threshold to find all matching images with specified similarity.
Dim tm As New AForge.Imaging.ExhaustiveTemplateMatching(SingleSimilarity)
' Return all the found matching images,
' it contains the top-left corner coordinates of each one
' and matchings are sortered by it's similarity percent.
Return tm.ProcessImage(BaseImage, ImageToFind)
End Function
An Usage Example:
Private Sub Test() Handles MyBase.Shown
' A Desktop Screenshot, in 1920x1080 px. resolution.
Dim DesktopScreenshoot As New Bitmap("C:\Desktop.png")
' A cutted piece of the screenshot, in 50x50 px. resolution.
Dim PartOfDesktopToFind As New Bitmap("C:\PartOfDesktop.png")
' Find the part of the image in the desktop, with the specified similarity.
For Each matching As AForge.Imaging.TemplateMatch In
FindImage(BaseImage:=DesktopScreenshoot, ImageToFind:=PartOfDesktopToFind, Similarity:=80.5R) ' 80,5% Similarity.
Dim sb As New System.Text.StringBuilder
sb.AppendFormat("Top-Left Corner Coordinates: {0}", matching.Rectangle.Location.ToString())
sb.AppendLine()
sb.AppendFormat("Similarity Image Percentage: {0}%", (matching.Similarity * 100.0F).ToString("00.00"))
MessageBox.Show(sb.ToString)
Next matching
End Sub