[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

相关推荐
等等5439 分钟前
Java EE初阶——wait 和 notify
java·开发语言
低代码布道师17 分钟前
第五部分:第一节 - Node.js 简介与环境:让 JavaScript 走进厨房
开发语言·javascript·node.js
盛夏绽放34 分钟前
Python字符串常用方法详解
开发语言·python·c#
满怀10151 小时前
【Vue 3全栈实战】从响应式原理到企业级架构设计
前端·javascript·vue.js·vue
伟笑1 小时前
elementUI 循环出来的表单,怎么做表单校验?
前端·javascript·elementui
确实菜,真的爱2 小时前
electron进程通信
前端·javascript·electron
好吃的肘子2 小时前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
nlog3n2 小时前
Go语言交替打印问题及多种实现方法
开发语言·算法·golang
kaixin_learn_qt_ing2 小时前
Golang
开发语言·后端·golang
ddd...e_bug2 小时前
Shell和Bash介绍
开发语言·bash