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

【简要介绍】

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

写入数据(序列化)

  1. 创建FileStorage对象

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

    复制代码

    csharp复制代码

    |---|---------------------------------------------------------------------------|
    | | FileStorage fs = new FileStorage("output.xml", FileStorage.Mode.Write); |

  2. 写入数据

    使用Write方法将数据写入文件。对于基本数据类型(如int, float, string),可以直接写入。对于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对象写入文件,然后再从文件中读取回来,而无需手动处理每个像素值。这在处理图像数据时非常有用,因为它可以大大简化数据的存储和传输过程。

【界面展示】

【实现意义】

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

【实现代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;

namespace FIRC
{
    public partial class Form1 : Form
    {
        Mat src = new Mat();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
              
                src = Cv2.ImRead(openFileDialog.FileName);
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);


            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image==null)
            {
                return;
            }
            FileStorage fileStorage = new FileStorage("image.data", FileStorage.Modes.Write);
            fileStorage.Write("image", src);
            fileStorage.Release();

           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(File.Exists("image.data"))
            {
                FileStorage fileStorage = new FileStorage("image.data", FileStorage.Modes.Read);
                Mat resultMat = fileStorage["image"].ToMat();
                pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
            }
          
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
            //Console.WriteLine(src.ToString());
            Console.WriteLine(Cv2.Format(src,FormatType.NumPy));
            //Console.WriteLine(Cv2.Format(src, FormatType.CSV));
            //Console.WriteLine(Cv2.Format(src, FormatType.MATLAB));
            //Console.WriteLine(Cv2.Format(src, FormatType.Python));
        }
    }
}

【测试环境】

vs2019

netframework4.7.2

opencvsharp4.8.0

【源码下载】

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

相关推荐
Fan_web1 分钟前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的5 分钟前
[C语言]--编译和链接
c语言·开发语言
Jiaberrr1 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
XKSYA(小巢校长)2 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W2 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++
VBA63372 小时前
VBA信息获取与处理第三个专题第三节:工作薄在空闲后自动关闭
开发语言
wjs20242 小时前
XSLT 实例:掌握 XML 转换的艺术
开发语言
萧鼎3 小时前
Python第三方库选择与使用陷阱避免
开发语言·python
安冬的码畜日常3 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js