0

I'm trying to accomplish a task of finding a specific object inside a PictureBox in VB.NET and returning the object's location within the PictureBox.

I have the image containing the object inside a PictureBox and a Image saved on my computer at C:\Bitmaps\imgToFind.png which is the object I want to find.

For example, if I have this picture inside the PictureBox: See Here

and this is the part of the picture I want to find saved at C:\Bitmaps\imgToFind.png: Part of the Picture

How would I be able to determine the (X,Y) coordinates for the object is located inside the larger picture? (Assuming that the picture is always clear and not altered/misshapen/over layed)?

  • 1
    The answer here: [Find a bitmap within another bitmap](https://codereview.stackexchange.com/a/138012/156501) is a good start. Note that it requires that the smaller image is actually a section of the source image, not *similar*, since that method doesn't apply any threshold to the comparison. For example, the Images you have posted here have a slightly different color, so it won't find a match. If you crop a piece of the larger Image and save it as PNG, it will. You could convert the Images to black & white or grayscale to increase the chance of a match. – Jimi Feb 20 '21 at 05:09
  • @Jimi Theoretically, if the colors on both images were 100% exact and matching, would your example work without having to covert to black/white? I would rather not have to do a conversion and save the image somewhere else every time. – betanon Feb 20 '21 at 16:24
  • Yes, as described, if you crop a section of the larger image and save it as PNG (not JPG), it will get that image section correctly. You don't need to convert the image to black & white or grayscale using an external tool, you can do it in code. See, e.g., here: [Replacing Colour Of An Image](https://stackoverflow.com/a/47529000/7444103) a B&W *instant* conversion for OCR (optionally inverting the Colors - as the *negative* of an image) and here: [Analyze colors of an Image](https://stackoverflow.com/a/59102380/7444103) for a GrayScale adaptable filter. – Jimi Feb 20 '21 at 16:58
  • @Jimi, I've ran the c# sample you've given me through a C# and VB.NET Translator Service. See Here: https://pastebin.com/Gpr5PNsn I'm using this function to search for the BitMap: https://pastebin.com/F1WMspiK but everytime it just Messages that there is nothing. This is my form with images: https://drive.google.com/file/d/1V8aVlTHCIHTjC7fOwxlmOvhwhXLcBc-h/view?usp=sharing but it still doesn't work..? (These are all JPGs) – betanon Feb 20 '21 at 19:12
  • As mentioned, you cannot use the JPG format for image recognition. – Jimi Feb 20 '21 at 19:51
  • @Jimi, sorry I meant to say PNG and its run in PNG now but its still returns nothing. Is the images and code compatible? – betanon Feb 20 '21 at 20:04
  • I've tested the *translated* code with you source Image (the larger one), cropping the SO Logo section and saving it as PNG. The code you posted works as intended. Add these lines to extract the Image from the `Point?` location: `Dim position As Point? = Find(sourceImage, patternImage) If position.HasValue Then Dim resultImage = sourceImage.Clone(New Rectangle(position.Value, patternImage.Size), sourceImage.PixelFormat) PictureBox1.Image = resultImage End If`. Where `patternImage` is the small Image to find. Don't use the one you posted here, it won't work. Save the PNG without compression. – Jimi Feb 20 '21 at 20:17
  • Note that there's a mistake in the code, in this line `If Not ContainSameElements(haystack(y + point.Y), point.X, needle(y), 0, needle.Length) Then` in the `IsNeedlePresentAtLocation()` method: change `needle.Length` to `needle(y).Length`. It won't change the result here, though. – Jimi Feb 20 '21 at 20:22

0 Answers0