public IEnumerable <Match> FindMatches(IPattern Template, double Similarity) { using var result = new DisposableMat(); // max is used for tmccoeff Imgproc.MatchTemplate(Mat, (Template as DroidCvPattern)?.Mat, result.Mat, Imgproc.TmCcoeffNormed); Imgproc.Threshold(result.Mat, result.Mat, 0.1, 1, Imgproc.ThreshTozero); while (true) { using var minMaxLocResult = Core.MinMaxLoc(result.Mat); var score = minMaxLocResult.MaxVal; if (score >= Similarity) { var loc = minMaxLocResult.MaxLoc; var region = new Region((int)loc.X, (int)loc.Y, Template.Width, Template.Height); yield return(new Match(region, score)); using var mask = new DisposableMat(); // Flood fill eliminates the problem of nearby points to a high similarity point also having high similarity const double floodFillDiff = 0.05; Imgproc.FloodFill(result.Mat, mask.Mat, loc, new Scalar(0), new Rect(), new Scalar(floodFillDiff), new Scalar(floodFillDiff), 0); } else { break; } } }
public bool IsMatch(IPattern Template, double Similarity) { using var result = new DisposableMat(); Imgproc.MatchTemplate(Mat, (Template as DroidCvPattern)?.Mat, result.Mat, Imgproc.TmCcoeffNormed); using var minMaxLocResult = Core.MinMaxLoc(result.Mat); return(minMaxLocResult.MaxVal >= Similarity); }
DisposableMat Match(DroidCvPattern Template) { var result = new DisposableMat(); if (Template.Mask != null) { Imgproc.MatchTemplate(Mat, Template.Mat, result.Mat, Imgproc.TmCcorrNormed, Template.Mask); } else { Imgproc.MatchTemplate(Mat, Template.Mat, result.Mat, Imgproc.TmCcoeffNormed); } return(result); }
private void match_eye(Rect area, Mat mTemplate, int type) { Point matchLoc; Mat mROI = mGray.Submat(area); int result_cols = mROI.Cols() - mTemplate.Cols() + 1; int result_rows = mROI.Rows() - mTemplate.Rows() + 1; // Check for bad template size if (mTemplate.Cols() == 0 || mTemplate.Rows() == 0) { return; } Mat mResult = new Mat(result_cols, result_rows, CvType.Cv8u); switch (type) { case TM_SQDIFF: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmSqdiff); break; case TM_SQDIFF_NORMED: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmSqdiffNormed); break; case TM_CCOEFF: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmCcoeff); break; case TM_CCOEFF_NORMED: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmCcoeffNormed); break; case TM_CCORR: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmCcorr); break; case TM_CCORR_NORMED: Imgproc.MatchTemplate(mROI, mTemplate, mResult, Imgproc.TmCcorrNormed); break; } Core.MinMaxLocResult mmres = Core.MinMaxLoc(mResult); // there is difference in matching methods - best match is max/min value if (type == TM_SQDIFF || type == TM_SQDIFF_NORMED) { matchLoc = mmres.MinLoc; } else { matchLoc = mmres.MaxLoc; } Point matchLoc_tx = new Point(matchLoc.X + area.X, matchLoc.Y + area.Y); Point matchLoc_ty = new Point(matchLoc.X + mTemplate.Cols() + area.X, matchLoc.Y + mTemplate.Rows() + area.Y); Imgproc.Rectangle(mRgba, matchLoc_tx, matchLoc_ty, new Scalar(255, 255, 0, 255)); Rect rec = new Rect(matchLoc_tx, matchLoc_ty); }