• [C#] opencvsharp对Mat数据进行序列化或者反序列化以及格式化输出


    【简要介绍】

    在OpenCVSharp中,FileStorage类用于将数据(包括OpenCVMat类型数据)序列化为XML或YAML格式的文件,以及从这些文件中反序列化数据。以下是关于FileStorage类用法的详细说明:

    写入数据(序列化)

    1. 创建FileStorage对象
      使用FileStorage类的构造函数创建一个新的FileStorage对象,并指定文件名和模式(写入或读取)。

       

      csharp复制代码

      FileStorage fs = new FileStorage("output.xml", FileStorage.Mode.Write);
    2. 写入数据
      使用Write方法将数据写入文件。对于基本数据类型(如intfloatstring),可以直接写入。对于Mat类型的数据,需要指定一个名称作为键。

       

      csharp复制代码

      fs.Write("int_value", 123);
      fs.Write("float_value", 3.14f);
      fs.Write("mat_name", myMat); // 假设myMat是一个Mat对象
    3. 释放资源
      在写入完成后,使用Release方法释放FileStorage对象占用的资源。

       

      csharp复制代码

      fs.Release();

    读取数据(反序列化

    1. 创建FileStorage对象
      与写入类似,但这次需要指定模式为读取。

       

      csharp复制代码

      FileStorage fs = new FileStorage("output.xml", FileStorage.Mode.Read);
    2. 读取数据
      使用索引器[]通过键名来访问数据。对于Mat类型的数据,可以直接将其转换为Mat对象。

       

      csharp复制代码

      int intValue = (int)fs["int_value"];
      float floatValue = (float)fs["float_value"];
      Mat loadedMat = (Mat)fs["mat_name"];

      注意:键名(如"int_value"、"float_value"、"mat_name")必须与写入时使用的名称一致。

    3. 释放资源
      在读取完成后,同样需要释放资源。

       

      csharp复制代码

      fs.Release();

    注意事项

    • 在使用FileStorage时,需要确保在读取或写入过程中不要出现错误,否则可能会导致数据丢失或文件损坏。
    • 对于复杂的数据结构(如包含多个Mat对象的列表或字典),可能需要使用更复杂的序列化策略。
    • 在处理大量数据时,需要注意内存管理和性能问题,确保应用程序的稳定性和响应性。

    序列化和反序列化Mat对象

    对于Mat对象,OpenCVSharp提供了直接的序列化和反序列化方法。这意味着你可以直接将Mat对象写入文件,然后再从文件中读取回来,而无需手动处理每个像素值。这在处理图像数据时非常有用,因为它可以大大简化数据的存储和传输过程。

    【界面展示】

    【实现意义】

    序列化和反序列化可以将图像数据存储为特定格式作为分析使用,格式化输出则有利于对图像数据进行肉眼分析。

    【实现代码】

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Diagnostics;
    6. using System.Drawing;
    7. using System.IO;
    8. using System.Linq;
    9. using System.Text;
    10. using System.Threading.Tasks;
    11. using System.Windows.Forms;
    12. using OpenCvSharp;
    13. namespace FIRC
    14. {
    15. public partial class Form1 : Form
    16. {
    17. Mat src = new Mat();
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. }
    22. private void button1_Click(object sender, EventArgs e)
    23. {
    24. OpenFileDialog openFileDialog = new OpenFileDialog();
    25. openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
    26. openFileDialog.RestoreDirectory = true;
    27. openFileDialog.Multiselect = false;
    28. if (openFileDialog.ShowDialog() == DialogResult.OK)
    29. {
    30. src = Cv2.ImRead(openFileDialog.FileName);
    31. pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
    32. }
    33. }
    34. private void button2_Click(object sender, EventArgs e)
    35. {
    36. if(pictureBox1.Image==null)
    37. {
    38. return;
    39. }
    40. FileStorage fileStorage = new FileStorage("image.data", FileStorage.Modes.Write);
    41. fileStorage.Write("image", src);
    42. fileStorage.Release();
    43. }
    44. private void Form1_Load(object sender, EventArgs e)
    45. {
    46. }
    47. private void button3_Click(object sender, EventArgs e)
    48. {
    49. if(File.Exists("image.data"))
    50. {
    51. FileStorage fileStorage = new FileStorage("image.data", FileStorage.Modes.Read);
    52. Mat resultMat = fileStorage["image"].ToMat();
    53. pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
    54. }
    55. }
    56. private void button4_Click(object sender, EventArgs e)
    57. {
    58. if (pictureBox1.Image == null)
    59. {
    60. return;
    61. }
    62. //Console.WriteLine(src.ToString());
    63. Console.WriteLine(Cv2.Format(src,FormatType.NumPy));
    64. //Console.WriteLine(Cv2.Format(src, FormatType.CSV));
    65. //Console.WriteLine(Cv2.Format(src, FormatType.MATLAB));
    66. //Console.WriteLine(Cv2.Format(src, FormatType.Python));
    67. }
    68. }
    69. }

    【测试环境】

    vs2019

    netframework4.7.2

    opencvsharp4.8.0

    【源码下载】

    https://download.csdn.net/download/FL1623863129/89450629

  • 相关阅读:
    分析Java中 CharSequence源码
    git rebase master
    半导体PVD和CVD设备用质量流量控制器市场现状研究分析
    【Java】JDK里有哪些线程安全的Set?
    Python数据分析--Numpy常用函数介绍(6)--Numpy中与股票成交量有关的计算
    (附源码)springboot学生宿舍管理系统 毕业设计 161542
    Himall验证帮助类是否是数值、 是否为邮政编码、是否是图片文件名、判断一个ip是否在另一个ip内
    线程池的使用
    systemverilog学习 ---- 任务和函数
    ASP.NET Core - 依赖注入(四)
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/139778884