const std::list<FeatureMatch> match_features(const cv::Mat &image, const std::list<const Feature*> &features) { QTime a; int i = 0; a.start(); std::list<FeatureMatch> feature_matches; for (const Feature *feature : features) { int found = 0; if (feature->maxCount == 1) { for (const Sprite &sprite : feature->sprites) { i++; const Match m = FindBestMatch(image, MatchTemplate(sprite.img, sprite.mask), CV_TM_CCORR_NORMED); if (m.value > 0) { feature_matches.push_back(FeatureMatch(feature, &sprite, m)); found++; } } if (!found) { qDebug() << "Not found" << feature->humanName; } } else { int found = 0; for (const Sprite &sprite : feature->sprites) { // FIXME: limited maxCount i++; const std::list<Match> matches = FindAllMatches(image, MatchTemplate(sprite.img, sprite.mask), CV_TM_CCORR_NORMED, sprite.detectionThreshold); for (const Match &m : matches) { feature_matches.push_back(FeatureMatch(feature, &sprite, m)); } found += matches.size(); } qDebug() << "Found" << found << "of" << feature->humanName; } } qDebug() << "elapsed" << a.elapsed() << "per search" << a.elapsed() / i; return feature_matches; }
// Get matched source from all srcs, return NULL if no match IplImage* GetSourceImage(IplImage* imgTemp, int *nMatch) { IplImage* imgSrc; int i; double tmp, max; CvPoint point = cvPoint(0, 0); CvPoint bestpoint = cvPoint(0, 0); for (i = 0; i < srcs_len; i++) { imgSrc = cvLoadImage(srcs[i]); tmp = MatchTemplate(imgSrc, imgTemp, &point); if (globalArgs.verbosity) printf("match rate of srcs[%d]=%f\n", i, tmp); cvReleaseImage(&imgSrc); if (tmp > max) { max = tmp; *nMatch = i; bestpoint.x = point.x; bestpoint.y = point.y; } } if (globalArgs.verbosity) printf("limit=%f max=%f maxi=%d\n", match_limit, max, *nMatch); imgSrc = cvLoadImage(srcs[*nMatch]); CvSize sizeTemp = cvGetSize(imgTemp); CvRect rcROI; rcROI.x = bestpoint.x; rcROI.y = bestpoint.y; rcROI.width = sizeTemp.width; rcROI.height = sizeTemp.height; if (globalArgs.verbosity) printf("ROI %d, %d, %d, %d\n", rcROI.x, rcROI.y, rcROI.width, rcROI.height); cvSetImageROI(imgSrc, rcROI); if (max > match_limit) { return imgSrc; } else { return NULL; } }