首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-02-19 03:20
  • 2863
  • 7830
blog.csdn.net

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

目录

效果

项目 

代码

下载


效果

  1. 直接输出:
  2. Mat [ 3*2*CV_8UC3, IsContinuous=True, IsSubmatrix=False, Ptr=0x1eb73ef9140, Data=0x1eb73ef91c0 ]
  3. 格式化输出:默认风格
  4. [ 91, 2, 79, 179, 52, 205;
  5. 236, 8, 181, 239, 26, 248;
  6. 207, 218, 45, 183, 158, 101]
  7. 格式化输出:Python风格
  8. [[[ 91, 2, 79], [179, 52, 205]],
  9. [[236, 8, 181], [239, 26, 248]],
  10. [[207, 218, 45], [183, 158, 101]]]
  11. 格式化输出:CSV风格
  12. 91, 2, 79, 179, 52, 205
  13. 236, 8, 181, 239, 26, 248
  14. 207, 218, 45, 183, 158, 101
  15. 格式化输出:NumPy风格
  16. array([[[ 91, 2, 79], [179, 52, 205]],
  17. [[236, 8, 181], [239, 26, 248]],
  18. [[207, 218, 45], [183, 158, 101]]], dtype='uint8')
  19. 格式化输出:c风格
  20. { 91, 2, 79, 179, 52, 205,
  21. 236, 8, 181, 239, 26, 248,
  22. 207, 218, 45, 183, 158, 101}
  23. 格式化输出一行:Python风格
  24. [[[236, 8, 181], [239, 26, 248]]]
  25. 格式化输出一列:Python风格
  26. [[179, 52, 205],
  27. [239, 26, 248],
  28. [183, 158, 101]]
  29. 格式化输出ROI 矩形:Python风格
  30. [[[ 91, 2, 79], [179, 52, 205]],
  31. [[236, 8, 181], [239, 26, 248]]]
  32. 格式化输出ROI Range:Python风格
  33. [[[ 91, 2, 79], [179, 52, 205]],
  34. [[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();
        }
    }
}

  1. using OpenCvSharp;
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace OpenCvSharp_Demo
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. Mat image;
  16. StringBuilder sb = new StringBuilder();
  17. private void Form1_Load(object sender, EventArgs e)
  18. {
  19. image = new Mat(3, 2, MatType.CV_8UC3);
  20. Cv2.Randu(image, Scalar.All(0d), Scalar.All(255d));
  21. pictureBox1.Image = new Bitmap(image.ToMemoryStream());
  22. }
  23. //序列化
  24. private void button2_Click(object sender, EventArgs e)
  25. {
  26. textBox1.Text = "序列化";
  27. FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Write);
  28. fileStorage.Write("src", image);
  29. fileStorage.Release();
  30. //读取显示
  31. textBox1.Text = File.ReadAllText("file.txt");
  32. }
  33. //反序列化
  34. private void button4_Click(object sender, EventArgs e)
  35. {
  36. textBox1.Text = "反序列化";
  37. FileStorage fileStorage = new FileStorage("file.txt", FileStorage.Modes.Read);
  38. Mat loadImage = fileStorage["src"].ToMat();
  39. pictureBox2.Image = new Bitmap(loadImage.ToMemoryStream());
  40. fileStorage.Release();
  41. }
  42. //格式化输出
  43. private void button5_Click(object sender, EventArgs e)
  44. {
  45. sb.Clear();
  46. sb.AppendLine("直接输出:");
  47. sb.AppendLine(image.ToString());
  48. sb.AppendLine("");
  49. sb.AppendLine("格式化输出:默认风格");
  50. sb.AppendLine(Cv2.Format(image));
  51. sb.AppendLine("");
  52. sb.AppendLine("格式化输出:Python风格");
  53. sb.AppendLine(Cv2.Format(image, FormatType.Python));
  54. sb.AppendLine("");
  55. sb.AppendLine("格式化输出:CSV风格");
  56. sb.AppendLine(Cv2.Format(image, FormatType.CSV));
  57. sb.AppendLine("");
  58. sb.AppendLine("格式化输出:NumPy风格");
  59. sb.AppendLine(Cv2.Format(image, FormatType.NumPy));
  60. sb.AppendLine("");
  61. sb.AppendLine("格式化输出:c风格");
  62. sb.AppendLine(Cv2.Format(image, FormatType.C));
  63. sb.AppendLine("");
  64. sb.AppendLine("格式化输出一行:Python风格");
  65. sb.AppendLine(Cv2.Format(image.Row(1), FormatType.Python));
  66. sb.AppendLine("");
  67. sb.AppendLine("格式化输出一列:Python风格");
  68. sb.AppendLine(Cv2.Format(image.Col(1), FormatType.Python));
  69. sb.AppendLine("");
  70. sb.AppendLine("格式化输出ROI 矩形:Python风格");
  71. sb.AppendLine(Cv2.Format(new Mat(image, new Rect(0, 0, 2, 2)), FormatType.Python));
  72. sb.AppendLine("");
  73. sb.AppendLine("格式化输出ROI Range:Python风格");
  74. sb.AppendLine(Cv2.Format(new Mat(image, new OpenCvSharp.Range(0, 2), new OpenCvSharp.Range(0, 2)), FormatType.Python));
  75. sb.AppendLine("");
  76. sb.Replace("\n", "\r\n");
  77. textBox1.Text = sb.ToString();
  78. }
  79. }
  80. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/138699025"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top