Results 1 to 4 of 4

Thread: Finding whether a small image is in a big image

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    12

    Smile Finding whether a small image is in a big image

    I have the following code, my img1 is big image, and img2 is small image

    I think the error is from
    Code:
    If img1.GetPixel(BigX, BigY) <> img2.GetPixel(SmallX, SmallY) Then
    Code:
        Public Function ImageCompare(ByVal img1 As Bitmap, ByVal img2 As Bitmap)
            Dim coordinates() As Integer = {-1, -1}
            For BigX = 1 To img1.Size.Width - img2.Size.Width + 1
                For BigY = 1 To img1.Size.Height - img2.Size.Height + 1
                    For SmallX = 1 To img2.Size.Width
                        For SmallY = 1 To img2.Size.Height
                            If img1.GetPixel(BigX, BigY) <> img2.GetPixel(SmallX, SmallY) Then
                                GoTo Exit_of_this_location
                            End If
                        Next
                    Next
                    coordinates(0) = BigX + Math.Ceiling(img2.Size.Width / 2)
                    coordinates(1) = BigY + Math.Ceiling(img2.Size.Height / 2)
    Exit_of_this_location:
                Next
            Next
            Return coordinates
        End Function
    Any advice

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding whether a small image is in a big image

    Quote Originally Posted by superhero_iceman View Post
    I think the error is from
    Code:
    If img1.GetPixel(BigX, BigY) <> img2.GetPixel(SmallX, SmallY) Then
    What error? Please provide a FULL and CLEAR explanation of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    432

    Re: Finding whether a small image is in a big image


  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Finding whether a small image is in a big image

    In most cases, that approach will be hopeless. It requires a bunch of things to be true for it to work. For one thing, when you are comparing pixels, you are comparing integers. If you have the RGB color {255, 244,111} and you compare it to a pixel with the color {255,243,111} they won't match, yet the difference is so small that you couldn't see it visually. So, you could have the smaller image shifted by just a single bit from the larger image. You'd see the small in the large, but your code wouldn't find it, because the code would be looking for an exact match.

    So, that approach will work if the small really is part of the large, and isn't just "really similar to a part of the large". So, if the small is a set of bytes taken from the bytes of the large, then you will find a match...unless the small image has been transformed in ANY way. If the two are just similar images, such as two different pictures of the same thing, then you'll never get a match with that code.
    My usual boring signature: Nothing

Tags for this Thread

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