【C#】 C# で OpenCV を使用する ~ 画像の位置合わせを考える ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/05/07/232536

の続き。
今回は、画像の位置合わせを考える。

■ 今回使用するOpenCVAPI

 * 以下のAPIが使えそう

1)GetPerspectiveTransform
2)WarpPerspective

 1)GetPerspectiveTransform

* 変換前後の座標をもとに変換行列を求める

2)WarpPerspective

* 変換行列用いて、画像を変換する

 ■ サンプル

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SampleForm
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      Mat matOrgImage = new Mat(@"20161215052204.png");
      Mat matDispImage = new Mat();
      OpenCvSharp.Size size = new OpenCvSharp.Size(this.pictureBox1.Width, this.pictureBox1.Height);
      
      Point2f[] src = new Point2f[]
      {
        new Point2f(24, 33), new Point2f(19, 407),
        new Point2f(560, 392), new Point2f(558, 45)
      };
      Point2f[] dst = new Point2f[] {
        new Point2f(19, 33), new Point2f( 19, 407),
        new Point2f(560, 407), new Point2f(560, 33)
      };
      Mat perspectiveMatrix = Cv2.GetPerspectiveTransform(src, dst);
      Cv2.WarpPerspective(matOrgImage, matDispImage, perspectiveMatrix, size, InterpolationFlags.Nearest);
      Image image = BitmapConverter.ToBitmap(matDispImage);

      this.pictureBox1.Image = image;
    }
  }
}

参考文献

https://lp-tech.net/articles/jNrnu?page=1
https://lp-tech.net/articles/jNrnu?page=2
http://sourcechord.hatenablog.com/entry/2017/02/20/233042
http://nobotta.dazoo.ne.jp/blog/?p=518
http://nobotta.dazoo.ne.jp/blog/?p=479
https://dixq.net/forum/viewtopic.php?t=18774
 

関連記事

C#OpenCV を使用する ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2019/05/07/232536
JavaOpenCV ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2016/06/27/234046