Search code examples
c#opencvcomputer-visionobject-detectiongame-automation

Image detection using OpenCvSharp - C#


interested in computer vision and automation. Now I want to find a small image on another large image. For Python I have seen a large amount of code and everything just works for them. But, it doesn't work for me for some reason.

Source files: enter image description here enter image description here

var sourceImage = Cv2.ImRead("path");
var template = Cv2.ImRead("path");

var resultImage = new Mat();

Cv2.MatchTemplate(sourceImage, template, resultImage, TemplateMatchModes.CCoeffNormed);
Cv2.MinMaxLoc(resultImage, out _, out double maxVal, out _, out OpenCvSharp.Point maxLoc);

Rect rect = new Rect(maxLoc, new Size(template.Width, template.Height));
Cv2.Rectangle(sourceImage, rect, new Scalar(0, 255, 0), 2);

Cv2.ImShow("Result", sourceImage);
Cv2.WaitKey(0);

this is the result enter image description here


Solution

  • I think the source file image of the thing you are looking for is too large. Look at the rectangle your program finds: it's much bigger than you have to use to contain the bait.
    You should resize the image to the size you'd see on the large picture. If that size can change in the game (e.g. zoom in / zoom out), try checking different sizes and select the best fit.

    (Also, I think you might be better off cutting the leaf part of the fishing bait, because this way you include a lot of background which may or may not be water, you are better off looking at the white part of the bait only. Also, cut more off of the green part of the picture, because from the picture, the bait might have yellow, red background too which would cause you problems. Maybe there is a way in C# to only import an image with removed background, and only use the non-background part for computation?)