Results 1 to 20 of 20

Thread: Comparing JPG image to an Image on a specified window

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Comparing JPG image to an Image on a specified window

    Hey guys,

    I have a specific image that I want my application to look for on a specific window. I've done something similar to this along time ago with VB6, but it was slow, and it was checking for specific points in the picture rather than the whole image itself (for speed reasons). I named this image "Image1.jpg" and I want to compare it to the window with the handle "001C08F2" and class "SunAwtCanvas"

    It has been a while since I have programmed and I'm trying to get into it again.

    Anyone have any ideas on how to accomplish this?
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Thanks for your help.

    Is that assuming both images are the same width and height though? Because I have a smaller image I want to compare to an image on the webbrowser of my form, so I would have to have it scan what it sees on the webbrowser and then check to see if the image exists. (Note: It would scan the webbrowser without scrolling up and down. This is assuming the scroll bars are not enabled.)
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    no. in that example there's a larger image + the code loops through each pixel in the larger image + looks for the smaller image contained within that larger image

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Ah. Alright. Thats assuming you have two pictures though, correct? What about if I have one picture and I want to scan the WebBrowser1 control for the same pixels?
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    you'd have to capture an image of the webbrowser contents

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    I'm still having trouble seeing how I can compare both my pictures.

    I have been able to code my program so the user can load a picture into userPictureBox, then when they click the button "Find on Screen", my program takes a snapshot of the screen and puts it into screenPictureBox. How could I make your code compatible to mine? I've been having trouble switching up the imgs and such in your code.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    ok. here it is simplified:

    vb Code:
    1. Private Function getImageRect(ByVal largerImage As Bitmap, ByVal imageToFind As Bitmap) As Rectangle
    2.     For x As Integer = 0 To largerImage.Width - 1
    3.         For y As Integer = 0 To largerImage.Height - 1
    4.             If largerImage.GetPixel(x, y) = imageToFind.GetPixel(0, 0) Then
    5.                 Dim dontMatch As Boolean = False
    6.                 Dim doesMatch As Integer = 0
    7.                 For x2 As Integer = 0 To imageToFind.Width - 1
    8.                     For y2 As Integer = 0 To imageToFind.Height - 1
    9.                         If largerImage.GetPixel(x + x2, y + y2) <> imageToFind.GetPixel(x2, y2) Then
    10.                             dontMatch = True
    11.                             Exit For
    12.                         Else
    13.                             doesMatch += 1
    14.                         End If
    15.                     Next
    16.                     If dontMatch Then Exit For
    17.                 Next
    18.                 If dontMatch = False AndAlso doesMatch = (imageToFind.Width * imageToFind.Height) Then Return New Rectangle(x, y, imageToFind.Width, imageToFind.Height)
    19.             End If
    20.         Next
    21.     Next
    22. End Function

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Thanks for helping!

    I got 1 problem though. Since it still wasn't working, I took away the function and I put it straight to the button that executes the code. I replaced everything properly and now where the GetPixel codes come up, I'm getting an error. For example:

    vb Code:
    1. If PictureBox2.GetPixel(x, y) = PictureBox1.GetPixel(0, 0) Then
    and
    vb Code:
    1. If PictureBox2.GetPixel(x + x2, y + y2) <> PictureBox1.GetPixel(x2, y2) Then

    The error I'm getting is: 'GetPixel' is not a member of 'System.Windows.Forms.PictureBox'.

    Any idea how to clear that up?
    Last edited by GRPsuper9; Nov 19th, 2011 at 11:12 PM.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    getPixel is a member of bitmap:

    vb Code:
    1. Dim image1 As Bitmap = DirectCast(PictureBox1.Image, Bitmap)
    2. Dim image2 As Bitmap = DirectCast(PictureBox2.Image, Bitmap)
    3. If image2.GetPixel(x, y) = image1.GetPixel(0, 0) Then
    4.  
    5. End If

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Ugh, I hate to keep bothering you about this, but I must be doing something wrong.

    This is the code I currently have:
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         getImageRect()
    3.     End Sub
    4.  
    5.     Private Function getImageRect() As Rectangle
    6.         Dim imageToFind As Bitmap = DirectCast(PictureBox1.Image, Bitmap)
    7.         Dim largerImage As Bitmap = DirectCast(PictureBox2.Image, Bitmap)
    8.         For x As Integer = 0 To largerImage.Width - 1
    9.             For y As Integer = 0 To largerImage.Height - 1
    10.                 If largerImage.GetPixel(x, y) = imageToFind.GetPixel(0, 0) Then
    11.                     Dim dontMatch As Boolean = False
    12.                     Dim doesMatch As Integer = 0
    13.                     For x2 As Integer = 0 To imageToFind.Width - 1
    14.                         For y2 As Integer = 0 To imageToFind.Height - 1
    15.                             If largerImage.GetPixel(x + x2, y + y2) <> imageToFind.GetPixel(x2, y2) Then
    16.                                 dontMatch = True
    17.                                 Exit For
    18.                             Else
    19.                                 doesMatch += 1
    20.                             End If
    21.                         Next
    22.                         If dontMatch Then Exit For
    23.                     Next
    24.                     If dontMatch = False AndAlso doesMatch = (imageToFind.Width * imageToFind.Height) Then Return New Rectangle(x, y, imageToFind.Width, imageToFind.Height)
    25.                 End If
    26.             Next
    27.         Next
    28.     End Function

    The images I have are actually screenshots of my code. PictureBox1 (imageToFind) is a little image with just the two words of "DirectCast" in it. The PictureBox2 (largerImage) is a picture of all of the code. For some reason it cannot find the DirectCast within the entire largerImage :/. Did you try this out and have luck?
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    as i explained in the link i posted, any deterioration in either of the images will cause the function to fail

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    These are the two images I'm using though. The pixels shouldn't be any different because they are from the same image.
    Attached Images Attached Images   
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    i didn't try it, but there are 256^3 colors used in windows + color.black looks exactly like color.fromargb(0,0,1) but vb doesn't see it that way...

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    So would it not be possible for me to use your method with what I am attempting to do? :/
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    it is possible but as you can see it's limited.

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Comparing JPG image to an Image on a specified window

    did you try:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     dim r as rectangle = getImageRect()  
    3.     msgbox(r isnot nothing)  
    4. End Sub

    ???

    dunno how i missed that

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Still a no go with it working. I'll try and see what I can find tomorrow and work on it a little more. Gonna go to sleep, got work in the morning :|. Haha.

    Thanks though! Let me know if you find out about other methods to do this or ways to fix this.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Comparing JPG image to an Image on a specified window

    Haven't had any success using google. Anyone else got one that might possibly work with what I'm trying to do?
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  20. #20
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Comparing JPG image to an Image on a specified window

    The important thing is to allow some margin of error in your comparison. It's easier to do if you first convert the images or the pixels to grayscale. A simple method is to add up the red, green and blue bytes of each pixel and dividing by 3. (There are also more sophisticated grayscale formulas that weight the RGB bytes according to the eye's sensitivity to different primary colors, but keep it simple for the moment.) If you are just comparing two pixels, you don't even need to divide by 3 because that factor is the same on both sides of the comparison.

    Another technique is to only require a certain percent -- say 70&#37; -- of the pixels to match on a first pass. That means keeping a count of the pixels that match, instead of exiting the For loop as soon as you find a mismatch. If you do get an approximate match this way, you could do a second pass with a higher percent or 100% to see if the images really are identical. This can be combined with the grayscale+comparison margin I described above.

    To sum up, the loop part of your code might look like this. I haven't checked it so it's just to give you the idea:

    vb Code:
    1. Dim matches As Integer
    2. Dim matchPercent As Integer = 80 'Try different values!
    3. Dim margin As Integer = 30 'Try different values!
    4. Dim totalPixels As Integer = imageToFind.Width * imageToFind.Height
    5. Dim doesMatch As Boolean = False
    6.  
    7. For x2 As Integer = 0 To imageToFind.Width - 1
    8.        For y2 As Integer = 0 To imageToFind.Height - 1
    9.                Dim clr1 = largerImage.GetPixel(x + x2, y + y2)
    10.                Dim clr2 = imageToFind.GetPixel(x2, y2)
    11.  
    12.                'Compare the grayscale values with a margin:
    13.                Dim gray1 As Integer = clr1.R + clr1.G + clr1.B
    14.                Dim gray2 As Integer = clr2.R + clr2.G + clr2.B
    15.                If gray1 - gray2 <= margin AndAlso gray1 - gray2 >= - margin Then
    16.                       matches +=1
    17.                       If CInt(matches * 100 / totalPixels) >= matchPercent Then
    18.                               doesMatch = True
    19.                               Exit For  
    20.                       End If
    21.               End If
    22.         Next
    23.         If doesMatch Then Exit For
    24. Next
    Note that you don't need dontMatch in this method.

    EDIT: This method just allows for possible color differences. If one of the images is stretched, rotated or otherwise distorted compared to the other it still won't work.

    BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width