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();
        }
    }
}

下载

源码下载

相关推荐
CN.LG24 分钟前
C# 企业微信机器人消息推送
c#·企业微信·1024程序员节·机器人推送
扫地的小何尚27 分钟前
一小时内使用NVIDIA Nemotron创建你自己的Bash计算机使用智能体
开发语言·人工智能·chrome·bash·gpu·nvidia
唐青枫36 分钟前
C#.NET ArrayPool 深入解析:高性能内存池的实现与应用
c#·.net
MoonBit月兔36 分钟前
MoonBit Pearls Vol.12:初探 MoonBit 中的 JavaScript 交互
开发语言·javascript·数据库·交互·moonbit
张人玉1 小时前
WPF 核心概念笔记(补充示例)
c#·wpf·1024程序员节·布局控件
草莓熊Lotso1 小时前
《算法闯关指南:优选算法--二分查找》--23.寻找旋转排序数组中的最小值,24.点名
开发语言·c++·算法·1024程序员节
foundbug9991 小时前
C# 实现 Modbus TCP 通信
开发语言·tcp/ip·c#
郝学胜-神的一滴1 小时前
主成分分析(PCA)在计算机图形学中的深入解析与应用
开发语言·人工智能·算法·机器学习·1024程序员节
JuicyActiveGilbert1 小时前
【Python进阶】第2篇:单元测试
开发语言·windows·python·单元测试
yugi9878382 小时前
基于Qt实现百度地图路径规划功能
开发语言·qt