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

下载

源码下载

相关推荐

  1. Unity-序列序列

    2024-05-12 07:38:02       43 阅读
  2. 序列序列

    2024-05-12 07:38:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 07:38:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 07:38:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 07:38:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 07:38:02       20 阅读

热门阅读

  1. 网易灵犀办公企业邮箱的IMAP和POP3服务器地址

    2024-05-12 07:38:02       10 阅读
  2. Github2024-05-11 开源项目日报 Top10

    2024-05-12 07:38:02       12 阅读
  3. C语言经典例题-2

    2024-05-12 07:38:02       12 阅读
  4. 算法题① —— 数组专栏

    2024-05-12 07:38:02       12 阅读
  5. 光栅化渲染和物理渲染

    2024-05-12 07:38:02       12 阅读
  6. 代码随想录算法训练营第36期DAY25

    2024-05-12 07:38:02       8 阅读
  7. 设计模式-09 - 享元模式 flyweight pattern

    2024-05-12 07:38:02       9 阅读
  8. Linux权限(二)

    2024-05-12 07:38:02       11 阅读
  9. 数据结构之队列

    2024-05-12 07:38:02       9 阅读
  10. DBSCAN聚类算法

    2024-05-12 07:38:02       12 阅读
  11. WEB前端复习——HTML

    2024-05-12 07:38:02       11 阅读