'OpenCV.NET: Finding multiple smaller images in a bigger image

I have a big image (different resolutions possible, usually FHD) and many small images (128x128 or 256x256). I want to know which of the smaller images are part of the bigger image and at which position. The location will be used to recognize text information below the image. The smaller images contained in the bigger image scale with the resolution of the big image, but there is no rotation or mirroring of the smaller images - but the lower part of the smaller image in the big image is hidden by some kind of border around the image.

My Idea is to use OpenCV.NET, read all the smaller Images (may be scale them down to the lowest resolution they share), calculate the SURF descriptor and save them as sidecar-files along the images to prevent recalculation (dont know if thats necessary when the images are that small). Everytime I get a "big" image, I'll load the surf-descriptors for the smaller images from disk and search the bigger image with a FlannBasedMatcher. If the result of the performed search has like 30 elements with a distance shorter than .25f, I assume that the smaller image is in the bigger image.

Code for one small image:

var imgSmall = Cv2.ImRead("small.png", ImreadModes.Unchanged);
var imgBig = Cv2.ImRead("big.png", ImreadModes.Color);

var featureTreshold = .25f;
var featuresRequired = 30;
var featureDetector = SURF.Create(1500);

KeyPoint[] keyPoints1, keyPoints2;
Mat descriptors1 = new Mat(), descriptors2 = new Mat();
featureDetector.DetectAndCompute(imgSmall, null, out keyPoints1, descriptors1);
featureDetector.DetectAndCompute(imgBig, null, out keyPoints2, descriptors2);

var matcher = new FlannBasedMatcher();
var matches = matcher.Match(descriptors1 /*small image*/, descriptors2 /*bigimage*/);
var optimized = matches.Where(o => o.Distance < featureTreshold).OrderBy(o => o.Distance).ToArray();
if (optimized.Length >= featuresRequired)
{
    // Looks like the smaller image is in the bigger image
}

For some manually tested files, that works. Now about the questions...

  1. Is this (looping through a list of SURF descriptors and matching them all to the bigger image) the right approach to test which of the smaller images are part of the bigger image?
  2. The featuresRequired method (seems) to work for my testcase (for now), but there surely is a more accurate way than hard-coding a number of required features...?
  3. How do I get the position (x/y) and the projected size (w/h) so I can estimate/calculate the text location below the image?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source