C# EMGU 3.4.1学习笔记(二)XML和YAML文件的写入

以下是《OpenCV3编程入门》中5.6.3的示例程序的C# + EMGU 3.4.1版,和C++程序相比,有如下几点不同:

1. 使用Matrix<>存储多维数组,多维数组的各维需要使用{}扩起来,之间用逗号分隔;

2. C#中无法使用<<和>>实现流的输入和输出,对应于<<的是Insert和Write函数,Insert函数只能插入string,Write函数可以把一个value插入到string形式的node;

3. 对于vector结构的输入和输出,要注意在第一个元素前加上“[”,在最后一个元素前加上“]”;而对于map结构的操作,使用的符号是“{”和“}”。需要注意的是,如果在“[”或“{”的后面紧跟“:”,则表示不进行换行输出,反之则进行换行输出;

4. 代码中使用了DateTime类获得系统当前时间,并以GetDateTimeFormats('r')[0]的格式进行显示;

5. 代码中使用了System.Security.Cryptography.RNGCryptoServiceProvider类,生成random seed,实现“真”随机数功能。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Security.Cryptography; //RNGCryptoServiceProvider(用于生成真随机数)所在名称空间
using Emgu.CV;

namespace XML_YAML_Write
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化
            FileStorage fs = new FileStorage("test.yaml", FileStorage.Mode.Write);
            //开始文件写入
            fs.Write(5, "frameCount");
            DateTime rawtime = DateTime.Now;
            fs.Insert("calibrationDate");
            fs.Insert(rawtime.GetDateTimeFormats('r')[0].ToString());
            double[,] values1 = new double[3, 3] { { 1000, 0, 30 }, { 0, 1000, 240 }, { 0, 0, 1 } };
            Matrix<double> cameraMatrix = new Matrix<double>(values1);
            double[,] values2 = new double[5, 1] { { 0.1 }, { 0.01 }, { -0.001 }, { 0 }, {0} };
            Matrix<double> distCoeffs = new Matrix<double>(values2);
            fs.Write(cameraMatrix.Mat, "cameraMatrix");
            fs.Write(distCoeffs.Mat, "distCoeffs");
            fs.Insert("features");
            fs.Insert("["); //开始写入array
            for(int i = 0; i < 3; i++)
            {
                int x = new Random(GetRandomSeed()).Next() % 640;
                int y = new Random(GetRandomSeed()).Next() % 480;
                byte lbp = (byte) (new Random(GetRandomSeed()).Next() % 256);
                //开始写入Mapping文本
                fs.Insert("{:"); //“:”是格式符,带之则不换行写入,反之换行写入
                fs.Write(x, "x");
                fs.Write(y, "y");
                fs.Insert("lbp");
                fs.Insert("[:"); //“:”是格式符,带之则不换行写入,反之换行写入
                for (int j = 0; j < 8; j++)
                    fs.Insert(((lbp >> j) & 1).ToString()); // “>>”为移位运算符
                fs.Insert("]");
                fs.Insert("}"); //关闭Mapping
            }
            fs.Insert("]"); //关闭array
            fs.ReleaseAndGetString();
            Console.WriteLine("文件写入完毕,请在工程目录下查看生成的文件~");
            Console.ReadKey();
        }

        //该函数作用为产生随机数种子,以便生成“真”随机数
        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4]; //4个字节可合成为int32
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0); //把4个字节合成为int32
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值