码迷,mamicode.com
首页 > 其他好文 > 详细

学习OpenCV——OpenCvSharp实践(一)

时间:2021-04-21 12:41:44      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:images   cvs   lib   pac   sof   flag   pattern   criteria   max   

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位浮点数。

学习OpenCV——OpenCvSharp实践(一)

标签:images   cvs   lib   pac   sof   flag   pattern   criteria   max   

原文地址:https://www.cnblogs.com/octoberkey/p/14680521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!