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
Re: Finding whether a small image is in a big image
Quote:
Originally Posted by
superhero_iceman
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.
Re: Finding whether a small image is in a big image
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.