IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> OpenCVSharp 相机棋盘格校正 -> 正文阅读

[人工智能]OpenCVSharp 相机棋盘格校正

用?OpenCVSharp 4.5?跑一遍 OpenCV?官方教程。

原?OpenCV 官方教程链接:OpenCV: Camera calibration With OpenCV

这个教程的程序源码比较复杂,这里简化一下以集中精力在核心的校正部分:只限于用棋盘格图片校正,本来就少量几个参数直接写进程序里,免去xml文件读写的麻烦。

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

namespace ConsoleApp1
{
    class tutorial58 : ITutorial
    {
        static int BoardSize_Width = 9; //宽度和高度是指内部交叉点的个数,而不是方形格的个数
        static int BoardSize_Height = 6;
        static Size BoardSize = new Size(BoardSize_Width, BoardSize_Height);
        static int SquareSize = 50; //每格的宽度应设置为实际的毫米数
        static int winSize = 11;

        public void Run()
        {
            List<string> imagesList = new List<string>() {
                @"I:\csharp\images\left01.jpg",
                @"I:\csharp\images\left02.jpg",
                @"I:\csharp\images\left03.jpg",
                @"I:\csharp\images\left04.jpg",
                @"I:\csharp\images\left05.jpg"
            };

            List<Point2f[]> imagesPoints = new List<Point2f[]>();
            Mat cameraMatrix = new Mat(), distCoeffs = new Mat();
            Size imageSize = new Size();
            bool found = false;

            Mat[] imagesPointsM = new Mat[imagesList.Count];

            for (int i = 0; i < imagesList.Count; i++)
            {
                Mat view = new Mat(imagesList[i]);
                if (!view.Empty())
                {
                    imageSize = view.Size();
                    Point2f[] pointBuf;

                    found = Cv2.FindChessboardCorners(view, BoardSize, out pointBuf, ChessboardFlags.AdaptiveThresh | ChessboardFlags.NormalizeImage);
                    if (found == true)
                    {
                        Mat viewGray = new Mat();
                        Cv2.CvtColor(view, viewGray, ColorConversionCodes.BGR2GRAY);
                        Cv2.CornerSubPix(viewGray, pointBuf, new Size(winSize, winSize), new Size(-1, -1), new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.Count, 30, 0.0001));
                        imagesPoints.Add(pointBuf);
                        Mat p = Mat.FromArray<Point2f>(pointBuf);
                        imagesPointsM[i] = p;

                        Cv2.DrawChessboardCorners(view, BoardSize, pointBuf, found);
                        Mat temp = view.Clone();
                        Cv2.ImShow("Image View", view);
                        Cv2.WaitKey(500);
                    }
                }
            }
            Mat[] rvecs = new Mat[0];
            Mat[] tvecs = new Mat[0];

            runCalibration(imagesList.Count, imageSize, out cameraMatrix, out distCoeffs, imagesPointsM, out rvecs, out tvecs, out double totalAvgErr);

            Console.WriteLine("Camera Matrix:\n{0}", Cv2.Format(cameraMatrix));
            Console.WriteLine("Distortion Coefficients:\n{0}", Cv2.Format(distCoeffs));
            Console.WriteLine("Total Average Error:\n{0}", totalAvgErr);

            Mat map1 = new Mat();
            Mat map2 = new Mat();
            Mat newCameraMatrix = Cv2.GetOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, out Rect roi);
            Cv2.InitUndistortRectifyMap(cameraMatrix, distCoeffs, new Mat(), newCameraMatrix, imageSize, MatType.CV_16SC2, map1, map2);
            for (int i = 0; i < imagesList.Count; i++)
            {
                Mat view = Cv2.ImRead(imagesList[i], ImreadModes.Color);
                Mat rview = new Mat();
                if (view.Empty())
                    continue;
                Cv2.Remap(view, rview, map1, map2, InterpolationFlags.Linear);
                Cv2.ImShow("Image View", rview);
                Cv2.WaitKey(500);
            }
            Cv2.WaitKey();
        }
        public static bool runCalibration(int imagesCount, Size imageSize, out Mat cameraMatrix, out Mat distCoeffs, Mat[] imagePoints, out Mat[] rvecs, out Mat[] tvecs, out double totalAvgErr)
        {
            cameraMatrix = Mat.Eye(new Size(3, 3), MatType.CV_64F);
            distCoeffs = Mat.Zeros(new Size(8, 1), MatType.CV_64F);

            Mat[] objectPoints = calcBoardCornerPositions(BoardSize, SquareSize, imagesCount);
            //Find intrinsic and extrinsic camera parameters
            double rms = Cv2.CalibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, out rvecs, out tvecs, CalibrationFlags.None);
            bool ok = Cv2.CheckRange(InputArray.Create(cameraMatrix)) && Cv2.CheckRange(InputArray.Create(distCoeffs));
            totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs, tvecs, cameraMatrix, distCoeffs);
            return ok;
        }

        public static Mat[] calcBoardCornerPositions(Size BoardSize, float SquareSize, int imagesCount)
        {
            Mat[] corners = new Mat[imagesCount];
            for (int k = 0; k < imagesCount; k++)
            {
                Point3f[] p = new Point3f[BoardSize.Height * BoardSize.Width];

                for (int i = 0; i < BoardSize.Height; i++)
                {
                    for (int j = 0; j < BoardSize.Width; j++)
                    {
                        p[i * BoardSize.Width + j] = new Point3f(j * SquareSize, i * SquareSize, 0);
                    }
                }
                //corners[k] = new Mat(BoardSize.Width, BoardSize.Height, MatType.CV_64F, p); 
                corners[k] = Mat.FromArray<Point3f>(p);
            }
            return corners;
        }
        public static double computeReprojectionErrors(Mat[] objectPoints, Mat[] imagePoints, Mat[] rvecs, Mat[] tvecs, Mat cameraMatrix, Mat distCoeffs)
        {
            Mat imagePoints2 = new Mat();
            int totalPoints = 0;
            double totalErr = 0, err;

            for (int i = 0; i < objectPoints.Length; ++i)
            {
                Cv2.ProjectPoints(objectPoints[i], rvecs[i], tvecs[i], cameraMatrix, distCoeffs, imagePoints2);

                err = Cv2.Norm(imagePoints[i], imagePoints2, NormTypes.L2);

                int n = objectPoints[i].Width * objectPoints[i].Height;
                //perViewErrors[i] = (float)Math.Sqrt(err * err / n);
                totalErr += err * err;
                totalPoints += n;
            }

            return Math.Sqrt(totalErr / totalPoints);
        }
    }
}

Camera Matrix:
[529.7323072631546, 0, 341.637394542699;
?0, 529.8893813111246, 237.8977196717263;
?0, 0, 1]
Distortion Coefficients:
[-0.3026327409202396, 0.2048271778048029, 0.001454843694634934, -0.0007689018321
320775, -0.2108852653389639]

Total Average Error:? ?0.419872613934806

?

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:47:03  更:2021-07-23 10:48:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/17 20:41:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码