Ⅰ. はじめに
OpenCV3 のラッパーである OpenCvSharp の MatchTemplate メソッドを使ってテンプレートマッチを行います。
Ⅱ. サンプル
スクリーンショットを取り、メモ帳のアイコンを探すサンプルです。
NuGet から OpenCV3 のラッパーをインストールする。
Install-Package OpenCvSharp3-AnyCPU
template.png
Program.cs
class Program { /// <summary> /// スクリーン全体のスクリーンショットを撮る /// </summary> static Bitmap TakeFullScreenScreenShot(int width, int height) { var bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb); //var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(bmp)) { g.CopyFromScreen(0, 0, 0, 0, bmp.Size); } return bmp; } /// <summary> /// テンプレートマッチを行う /// </summary> static (OpenCvSharp.Point minPoint, OpenCvSharp.Point maxPoint) MatchTemplate() { var screen = BitmapConverter.ToMat(TakeFullScreenScreenShot(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); var template = BitmapConverter.ToMat(new Bitmap("template.png")); var result = new Mat(); Cv2.MatchTemplate(screen, template, result, TemplateMatchModes.CCoeffNormed); Cv2.Threshold(result, result, 0.8, 1.0, ThresholdTypes.Binary); Cv2.MinMaxLoc(result, out OpenCvSharp.Point minPoint, out OpenCvSharp.Point maxPoint); //screen.Rectangle(maxPoint, new OpenCvSharp.Point(maxPoint.X + template.Width, maxPoint.Y + template.Height), Scalar.Red); //screen.SaveImage("out.bmp"); return (minPoint, maxPoint); } static void Main(string[] args) { MatchTemplate(); } }
Ⅲ. 実行結果
メモ帳ウィンドウの左上のアイコンのテンプレートマッチに成功し、四角い枠が付いています。