C# OpenCvSharp Demo - Mat格式化输出、Mat序列化和反序列化

C# OpenCvSharp Demo - Mat格式化输出、Mat序列化和反序列化

目录

效果

项目

代码

下载


效果

复制代码
直接输出:

Mat [ 3*2*CV_8UC3, IsContinuous=True, IsSubmatrix=False, Ptr=0x1eb73ef9140, Data=0x1eb73ef91c0 ]


格式化输出:默认风格

[ 91,   2,  79, 179,  52, 205;
 236,   8, 181, 239,  26, 248;
 207, 218,  45, 183, 158, 101]


格式化输出:Python风格

[[[ 91,   2,  79], [179,  52, 205]],
 [[236,   8, 181], [239,  26, 248]],
 [[207, 218,  45], [183, 158, 101]]]


格式化输出:CSV风格

 91,   2,  79, 179,  52, 205
236,   8, 181, 239,  26, 248
207, 218,  45, 183, 158, 101


格式化输出:NumPy风格

array([[[ 91,   2,  79], [179,  52, 205]],
       [[236,   8, 181], [239,  26, 248]],
       [[207, 218,  45], [183, 158, 101]]], dtype='uint8')


格式化输出:c风格

{ 91,   2,  79, 179,  52, 205,
 236,   8, 181, 239,  26, 248,
 207, 218,  45, 183, 158, 101}


格式化输出一行:Python风格

[[[236,   8, 181], [239,  26, 248]]]


格式化输出一列:Python风格

[[179,  52, 205],
 [239,  26, 248],
 [183, 158, 101]]


格式化输出ROI 矩形:Python风格

[[[ 91,   2,  79], [179,  52, 205]],
 [[236,   8, 181], [239,  26, 248]]]


格式化输出ROI Range:Python风格

[[[ 91,   2,  79], [179,  52, 205]],
 [[236,   8, 181], [239,  26, 248]]]

项目

代码

using OpenCvSharp;

using System;

using System.Drawing;

using System.IO;

using System.Text;

using System.Windows.Forms;

namespace OpenCvSharp_Demo

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

Mat image;

StringBuilder sb = new StringBuilder();

private void Form1_Load(object sender, EventArgs e)

{

image = new Mat(3, 2, MatType.CV_8UC3);

Cv2.Randu(image, Scalar.All(0d), Scalar.All(255d));

pictureBox1.Image = new Bitmap(image.ToMemoryStream());

}

//序列化

private void button2_Click(object sender, EventArgs e)

{

textBox1.Text = "序列化";

FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Write);

fileStorage.Write("src", image);

fileStorage.Release();

//读取显示

textBox1.Text = File.ReadAllText("file.txt");

}

//反序列化

private void button4_Click(object sender, EventArgs e)

{

textBox1.Text = "反序列化";

FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Read);

Mat loadImage = fileStorage["src"].ToMat();

pictureBox2.Image = new Bitmap(loadImage.ToMemoryStream());

fileStorage.Release();

}

//格式化输出

private void button5_Click(object sender, EventArgs e)

{

sb.Clear();

sb.AppendLine("直接输出:");

sb.AppendLine(image.ToString());

sb.AppendLine("");

sb.AppendLine("格式化输出:默认风格");

sb.AppendLine(Cv2.Format(image));

sb.AppendLine("");

sb.AppendLine("格式化输出:Python风格");

sb.AppendLine(Cv2.Format(image, FormatType.Python));

sb.AppendLine("");

sb.AppendLine("格式化输出:CSV风格");

sb.AppendLine(Cv2.Format(image, FormatType.CSV));

sb.AppendLine("");

sb.AppendLine("格式化输出:NumPy风格");

sb.AppendLine(Cv2.Format(image, FormatType.NumPy));

sb.AppendLine("");

sb.AppendLine("格式化输出:c风格");

sb.AppendLine(Cv2.Format(image, FormatType.C));

sb.AppendLine("");

sb.AppendLine("格式化输出一行:Python风格");

sb.AppendLine(Cv2.Format(image.Row(1), FormatType.Python));

sb.AppendLine("");

sb.AppendLine("格式化输出一列:Python风格");

sb.AppendLine(Cv2.Format(image.Col(1), FormatType.Python));

sb.AppendLine("");

sb.AppendLine("格式化输出ROI 矩形:Python风格");

sb.AppendLine(Cv2.Format(new Mat(image, new Rect(0, 0, 2, 2)), FormatType.Python));

sb.AppendLine("");

sb.AppendLine("格式化输出ROI Range:Python风格");

sb.AppendLine(Cv2.Format(new Mat(image, new OpenCvSharp.Range(0, 2), new OpenCvSharp.Range(0, 2)), FormatType.Python));

sb.AppendLine("");

sb.Replace("\n", "\r\n");

textBox1.Text = sb.ToString();

}

}

}

复制代码
using OpenCvSharp;
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Mat image;
        StringBuilder sb = new StringBuilder();

        private void Form1_Load(object sender, EventArgs e)
        {
            image = new Mat(3, 2, MatType.CV_8UC3);
            Cv2.Randu(image, Scalar.All(0d), Scalar.All(255d));

            pictureBox1.Image = new Bitmap(image.ToMemoryStream());
        }

        //序列化
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "序列化";
            FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Write);
            fileStorage.Write("src", image);
            fileStorage.Release();

            //读取显示
            textBox1.Text = File.ReadAllText("file.txt");
        }

        //反序列化
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "反序列化";
            FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Read);
            Mat loadImage = fileStorage["src"].ToMat();
            pictureBox2.Image = new Bitmap(loadImage.ToMemoryStream());
            fileStorage.Release();
        }

        //格式化输出
        private void button5_Click(object sender, EventArgs e)
        {
            sb.Clear();

            sb.AppendLine("直接输出:");
            sb.AppendLine(image.ToString());
            sb.AppendLine("");

            sb.AppendLine("格式化输出:默认风格");
            sb.AppendLine(Cv2.Format(image));
            sb.AppendLine("");

            sb.AppendLine("格式化输出:Python风格");
            sb.AppendLine(Cv2.Format(image, FormatType.Python));
            sb.AppendLine("");

            sb.AppendLine("格式化输出:CSV风格");
            sb.AppendLine(Cv2.Format(image, FormatType.CSV));
            sb.AppendLine("");

            sb.AppendLine("格式化输出:NumPy风格");
            sb.AppendLine(Cv2.Format(image, FormatType.NumPy));
            sb.AppendLine("");

            sb.AppendLine("格式化输出:c风格");
            sb.AppendLine(Cv2.Format(image, FormatType.C));
            sb.AppendLine("");

            sb.AppendLine("格式化输出一行:Python风格");
            sb.AppendLine(Cv2.Format(image.Row(1), FormatType.Python));
            sb.AppendLine("");

            sb.AppendLine("格式化输出一列:Python风格");
            sb.AppendLine(Cv2.Format(image.Col(1), FormatType.Python));
            sb.AppendLine("");

            sb.AppendLine("格式化输出ROI 矩形:Python风格");
            sb.AppendLine(Cv2.Format(new Mat(image, new Rect(0, 0, 2, 2)), FormatType.Python));
            sb.AppendLine("");

            sb.AppendLine("格式化输出ROI Range:Python风格");
            sb.AppendLine(Cv2.Format(new Mat(image, new OpenCvSharp.Range(0, 2), new OpenCvSharp.Range(0, 2)), FormatType.Python));
            sb.AppendLine("");

            sb.Replace("\n", "\r\n");

            textBox1.Text = sb.ToString();
        }
    }
}

下载

源码下载

相关推荐
心愿许得无限大23 分钟前
Qt 常用界面组件
开发语言·c++·qt
2401_8582861134 分钟前
OS15.【Linux】gdb调试器的简单使用
linux·运维·服务器·开发语言·gdb
牛马baby35 分钟前
MATLAB下载安装教程(附安装包)2025最新版(MATLAB R2024b)
开发语言·matlab
shenyan~44 分钟前
关于 c、c#、c++ 三者区别
开发语言·c++
Ashlee_code1 小时前
什么是Web3?金融解决方案
开发语言·金融·架构·eclipse·web3·区块链·php
Evand J1 小时前
【MATLAB例程】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,附下载链接
开发语言·matlab
机器视觉知识推荐、就业指导1 小时前
Qt 与Halcon联合开发八: 结合Qt与Halcon实现海康相机采图显示(附源码)
开发语言·数码相机·qt
Heartoxx2 小时前
c语言-指针与一维数组
c语言·开发语言·算法
hqxstudying2 小时前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
charlie1145141912 小时前
如何使用Qt创建一个浮在MainWindow上的滑动小Panel
开发语言·c++·qt·界面设计