-1

I have a small bitmap image and I will do a screenshot. I need to find whether the small image is the screenshot. How I can compare two bitmap images? and then return the coordinates.

If it is MATLAB, (NOTE: I need VB.net)

What my plan is this

screenshot
for x_screen = 1: screen_width_x
     for y_screen = 1: screen_col_y 
      for x_pic = = 1: pic_width_x
       for y_pic = = 1: pic_col_y
       if screenshot(x_screen, y_screen) != pic(x_pic , y_pic)
       break
       end
    end
    xx = (x_screen)
    yy = (y_screen)
    end
    end
Marco
  • 985
  • 7
  • 22
  • 50

2 Answers2

2

See Image comparison if you want something advanced Also you can look for machine learning techniques for such tasks.

Here is naive solution and not optimized one (plus it is better to do such tasks in native code):

Plus Note that it is better to check this with bmp files. otherwise it will not handle problem

Private Function FindSubImg2(img As Bitmap, subimg As Bitmap) As Point
    If (img.Width - subimg.Width < 0) Or (img.Height - subimg.Height < 0) Then Return Nothing 
    Dim stepxLen As Integer = img.Width - subimg.Width
    Dim stepyLen As Integer = img.Height - subimg.Height 
    Dim coor As Point
    Dim match As Boolean = False

    For oy As Integer = 0 To stepyLen
    For ox As Integer = 0 To stepxLen
            match = True 
            For x As Integer = 0 To subimg.Width - 1
                For y As Integer = 0 To subimg.Height - 1
                    'actually here we do not need ToArgb method. But it will skip unneeded Color comparisions
                    If img.GetPixel(x + ox, y + oy).ToArgb <> subimg.GetPixel(x, y).ToArgb Then
                        match = False
                        Exit For 'we can use goto operator instead of double exit for 
                    End If
                Next
                If match = False Then Exit For
            Next
            If match = True Then
                coor.X = ox
                coor.Y = oy
                Return coor
            End If 
        Next
    Next

    Return New Point(-1, -1)
End Function

Private Function FindSubImg(a As Bitmap, b As Bitmap) As Point

    Dim subimg As Bitmap
    Dim img As Bitmap

    If (a.Height <= b.Height AndAlso a.Width <= b.Width) Then
        subimg = a : img = b
        Return FindSubImg2(img, subimg)

    ElseIf (a.Height > b.Height AndAlso a.Width > b.Width) Then
        subimg = b : img = a
        Return FindSubImg2(img, subimg)
    Else
        Return New Point(-1, -1)
    End If


End Function

Usage:

Dim p As Point = FindSubImg(New Bitmap("A.bmp"), New Bitmap("B.bmp"))
Community
  • 1
  • 1
qwr
  • 3,660
  • 17
  • 29
1

Try something like this:

Public Function CompareImages(ByVal img1 As Bitmap, ByVal img2 As Bitmap) As Boolean
    Dim i As Integer
    Dim j As Integer

    For i = 0 To img1.Width - 1
        For j = 0 To img2.Height - 1
            If img1.GetPixel(i, j) <> img2.GetPixel(i, j) Then
                Return False
            End If
        Next
    Next
    Return True
End Function

sample call:

CompareImages(New Bitmap("f:\img1.bmp"), New Bitmap("f:\img2.bmp"))
Andrea
  • 11,801
  • 17
  • 65
  • 72