學習OpenCV——OpenCvSharp實踐(一)


EstimateAffine2D用法

OpenCV官方幫助

cv::Mat cv::estimateAffine2D(InputArray from, InputArray to, OutputArray inliers = noArray(), int method = RANSAC, double ransacReprojThreshold = 3, size_t maxIters = 2000, double confidence = 0.99, size_t refineIters = 10);

運行環境

Visual Studio 2019 版本 16.94
OpenCvSharp 版本 4.5.2.20210404

測試圖像

棋盤格測試圖像

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;

namespace OpenCvSharpDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      /// 1、找角點
      Size patternsize = new Size(5, 4);
      Mat gray = Cv2.ImRead("checkerboard 1.png", ImreadModes.Grayscale);
      var corners = new List<Point2f>();
      bool patternfound = Cv2.FindChessboardCorners(gray, patternsize, OutputArray.Create<Point2f>(corners),
        ChessboardFlags.AdaptiveThresh | ChessboardFlags.NormalizeImage | ChessboardFlags.FastCheck);

      if (patternfound)
      {
        Cv2.CornerSubPix(gray, corners, new Size(11, 11), new Size(-1, -1),
          new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.Count, 100, 0.01));
      }

      /// 2、顯示結果
      var img = gray.Clone();
      Cv2.CvtColor(img, img, ColorConversionCodes.GRAY2RGB);
      Cv2.DrawChessboardCorners(img, patternsize, corners, patternfound);

      /// 3、計算映射關系
      corners.Reverse();
      var corners_realworld = new List<Point2f>()      {
        new Point2f(00.0f, 00.0f),        new Point2f(02.5f, 00.0f),        new Point2f(05.0f, 00.0f),        new Point2f(07.5f, 00.0f),        new Point2f(10.0f, 00.0f),
        new Point2f(00.0f, 02.5f),        new Point2f(02.5f, 02.5f),        new Point2f(05.0f, 02.5f),        new Point2f(07.5f, 02.5f),        new Point2f(10.0f, 02.5f),
        new Point2f(00.0f, 05.0f),        new Point2f(02.5f, 05.0f),        new Point2f(05.0f, 05.0f),        new Point2f(07.5f, 05.0f),        new Point2f(10.0f, 05.0f),
        new Point2f(00.0f, 07.5f),        new Point2f(02.5f, 07.5f),        new Point2f(05.0f, 07.5f),        new Point2f(07.5f, 07.5f),        new Point2f(10.0f, 07.5f),
      };
      var from =  InputArray.Create<Point2f>(corners/*, MatType.CV_32FC2*/);
      var to = InputArray.Create<Point2f>(corners_realworld/*, MatType.CV_32FC2*/);
      var inliers = new Mat();
      var affine_mat = Cv2.EstimateAffine2D(from, to, inliers);

      /// 4、計算投影誤差
      var result = new List<Point2f>();
      var m00 = affine_mat.At<double>(0, 0);
      var m01 = affine_mat.At<double>(0, 1);
      var m02 = affine_mat.At<double>(0, 2);
      var m10 = affine_mat.At<double>(1, 0);
      var m11 = affine_mat.At<double>(1, 1);
      var m12 = affine_mat.At<double>(1, 2);
      Console.WriteLine("[Error]");
      for (int i = 0; i < corners.Count; i++)
      {
        result.Add(new Point2f((float)(m00 * corners[i].X + m01 * corners[i].Y + m02 * 1),
                               (float)(m10 * corners[i].X + m11 * corners[i].Y + m12 * 1)));

        Console.WriteLine($"{i, 2}: {(result[i].X - corners_realworld[i].X).ToString("+0.00000;-0.00000;0.00000")}, {(result[i].Y - corners_realworld[i].Y).ToString("+0.00000;-0.00000;0.00000")}");
      }

      Cv2.ImWrite("result.bmp", img);
      Cv2.ImShow("img", img);
      Cv2.WaitKey();
    }
  }
}

紀要

EstimateAffine2D傳入的from和to是32位浮點數,返回的affine_mat是64位浮點數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM