OpenCvSharp


まずはNugetでOpenCvSharpを入れる

Install-Package OpenCvSharp-x64

Q:どんなことができるか?
A:画像解析
例えば、指標画像

txt

からスクリーンキャプチャの同一矩形を得られたりします。(画像内の赤枠)

txtResult

もちろんロゴ画像みたいなのでも

logo

結果

imgResult

コードは続きで。
画像認識で自動テストとかラスタから近似部分取り出しとか、色々使えそうではあるが…。

コードは適当な感じ

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCV_TEST
{
  public partial class frmMain : Form
  {
    public frmMain()
    {
      this.InitializeComponent();
    }

    public frmMain(Bitmap img) : this()
    {
      this.pictureBox1.Image = img;

      this.ShowInTaskbar = false;
      this.menuStrip1.Visible = false;
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
      string imgPath = null;
      using (OpenFileDialog dig = new OpenFileDialog())
      {
        dig.Title = "元画像を選択";
        if (dig.ShowDialog() != DialogResult.OK)
          return;
        imgPath = dig.FileName;
      }

      Bitmap img = new Bitmap(imgPath);
      if (this.pictureBox1.Image != null)
      {
        this.pictureBox1.Image.Dispose();
      }
      this.pictureBox1.Image = img;

      Bitmap screenImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
      using (Graphics g = Graphics.FromImage(screenImg))
      {
        g.CopyFromScreen(0, 0, 0, 0, screenImg.Size);
      }

      foreach (var rect in this.getMatchTemplates(img, screenImg, 5))
      {
        //見つけた場所に枠を描く
        using (Graphics g = Graphics.FromImage(screenImg))
        {
          g.DrawRectangle(Pens.Red, rect);
        }
      }
      using (var frm = new frmMain(screenImg))
      {
        frm.ShowDialog(this);
      }//end result

    }//end method

    private IEnumerable<Rectangle> getMatchTemplates(Bitmap src, Bitmap dst, int maxCount)
    {

      CvSize cvSize = new CvSize(dst.Width - src.Width + 1, dst.Height - src.Height + 1);

      int count = 0;
      while (true)
      {
        using (Bitmap copyDst = new Bitmap(dst.Width, dst.Height))
        using (Bitmap copySrc = new Bitmap(src.Width, src.Height))
        {
          using (Graphics g = Graphics.FromImage(copySrc))
          {
            g.DrawImageUnscaled(src, 0, 0);
          }
          using (Graphics g = Graphics.FromImage(copyDst))
          {
            g.DrawImageUnscaled(dst, 0, 0);
          }
          using (IplImage srcCvImg = IplImage.FromBitmap(copySrc))
          using (IplImage dstCvImg = IplImage.FromBitmap(copyDst))
          using (IplImage resultCv = Cv.CreateImage(cvSize, BitDepth.F32, 1))
          {

            count++;
            Cv.MatchTemplate(dstCvImg, srcCvImg, resultCv, MatchTemplateMethod.CCorrNormed);

            double minVal;
            double maxVal;
            CvPoint minLoc;
            CvPoint maxLoc;

            Cv.MinMaxLoc(resultCv, out minVal, out maxVal, out minLoc, out maxLoc);

            //指定回数試行か一致なしで戻す
            if (maxVal < 0.5 || maxCount < count)
            {
              break;
            }//end if

            var res = new Rectangle(maxLoc.X, maxLoc.Y, src.Width, src.Height);
            //塗り
            using (Graphics g = Graphics.FromImage(copyDst))
            using (var b = new SolidBrush(Color.Red))
            {
              g.FillRectangle(b, res);
            }
            yield return res;
          }//end cv img
        }//end img
      }//end loop
      yield break;
    }

  }//end class
}//end namespace
カテゴリー: 開発 タグ: パーマリンク