How to get specific image coordinates from within bitmap

Mansour_Dalir 1,936 Reputation points
2024-05-21T03:56:04.6666667+00:00

hi

User's image

Is there a special (Library/Class) for such an achievement? which can find and return the coordinates by giving a symbol of a specific image from within the larger image

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,652 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,376 Reputation points Microsoft Vendor
    2024-05-21T06:41:31.7833333+00:00

    Hi @Mansour_Dalir ,

    There is no such basic .NET class that can achieve.

    You can refer to the following code, this method is straightforward but may be slow for large images or complex comparisons.

    If you want more advanced and efficient image processing, you can find some third-party libraries.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim mainImage As Bitmap = Bitmap.FromFile("path_to_main_image.jpg")
            Dim template As Bitmap = Bitmap.FromFile("path_to_template_image.jpg")
    
            Dim mainWidth As Integer = mainImage.Width
            Dim mainHeight As Integer = mainImage.Height
            Dim templateWidth As Integer = template.Width
            Dim templateHeight As Integer = template.Height
    
            ' Loop through the main image to find the template
            For x As Integer = 0 To mainWidth - templateWidth
                For y As Integer = 0 To mainHeight - templateHeight
                    If CompareImages(mainImage, template, x, y) Then
                        ' Match found
                        MessageBox.Show("Match found at X: " & x & ", Y: " & y)
                        ' Draw a rectangle around the matching area
                        Dim g As Graphics = Graphics.FromImage(mainImage)
                        g.DrawRectangle(New Pen(Color.Red, 3), New Rectangle(x, y, templateWidth, templateHeight))
                        PictureBox1.Image = mainImage
                        g.Dispose()
                        Return
                    End If
                Next
            Next
    
            MessageBox.Show("No match found")
        End Sub
    
        Private Function CompareImages(mainImage As Bitmap, template As Bitmap, startX As Integer, startY As Integer) As Boolean
            For x As Integer = 0 To template.Width - 1
                For y As Integer = 0 To template.Height - 1
                    Dim mainPixel As Color = mainImage.GetPixel(startX + x, startY + y)
                    Dim templatePixel As Color = template.GetPixel(x, y)
                    ' Check if pixels are similar (you can adjust the tolerance as needed)
                    If Not PixelsAreSimilar(mainPixel, templatePixel) Then
                        Return False
                    End If
                Next
            Next
            Return True
        End Function
    
        Private Function PixelsAreSimilar(pixel1 As Color, pixel2 As Color, Optional tolerance As Integer = 50) As Boolean
            ' Compare the RGB values of the pixels within a tolerance
            Return Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) <= tolerance AndAlso
                   Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) <= tolerance AndAlso
                   Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) <= tolerance
        End Function
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.