• C# OpenCvSharp 通道分离


    目录

    效果

    项目

    代码

    下载 


    效果

    项目

    代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using OpenCvSharp;
    using OpenCvSharp.Extensions;

    namespace OpenCvSharp_通道分离
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            Bitmap bmp;
            String imgPath = "";
            Mat mat;

            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = fileFilter;
                if (ofd.ShowDialog() != DialogResult.OK) return;
                imgPath = ofd.FileName;
                bmp = new Bitmap(imgPath);
                mat = new Mat(imgPath, ImreadModes.AnyColor);
                pictureBox1.Image = bmp;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (pictureBox1.Image == null)
                {
                    return;
                }

                //BGR
                Mat[] mats = Cv2.Split(mat);

                if (radioButton1.Checked)
                {
                    //灰度图
                    pictureBox2.Image = BitmapConverter.ToBitmap(mats[0]);
                    pictureBox3.Image = BitmapConverter.ToBitmap(mats[1]);
                    pictureBox4.Image = BitmapConverter.ToBitmap(mats[2]);

                    //Window b = new Window("B", WindowMode.Normal);
                    //Window g = new Window("G", WindowMode.Normal);
                    //Window r = new Window("R", WindowMode.Normal);

                    //b.ShowImage(mats[0]);
                    //g.ShowImage(mats[1]);
                    //r.ShowImage(mats[2]);
                }
                else
                {
                    //彩色图

                    //创建一个空通道0
                    Mat zero = new Mat(mats[0].Size(), MatType.CV_8UC1, new Scalar(0));
                    Mat bsum = new Mat();
                    Mat gsum = new Mat();
                    Mat rsum = new Mat();
                    Cv2.Merge(new Mat[] { mats[0], zero, zero }, bsum);//(b,0,0)图像
                    Cv2.Merge(new Mat[] { zero, mats[1], zero }, gsum);//(0,g,0)图像
                    Cv2.Merge(new Mat[] { zero, zero, mats[2] }, rsum);//(0,0,r)图像

                    pictureBox2.Image = BitmapConverter.ToBitmap(bsum);
                    pictureBox3.Image = BitmapConverter.ToBitmap(gsum);
                    pictureBox4.Image = BitmapConverter.ToBitmap(rsum);

                }


            }

            private void Form1_Load(object sender, EventArgs e)
            {
                radioButton1.Checked = true;
            }
        }
    }
     

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using OpenCvSharp;
    10. using OpenCvSharp.Extensions;
    11. namespace OpenCvSharp_通道分离
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. Bitmap bmp;
    21. String imgPath = "";
    22. Mat mat;
    23. private void button2_Click(object sender, EventArgs e)
    24. {
    25. OpenFileDialog ofd = new OpenFileDialog();
    26. ofd.Filter = fileFilter;
    27. if (ofd.ShowDialog() != DialogResult.OK) return;
    28. imgPath = ofd.FileName;
    29. bmp = new Bitmap(imgPath);
    30. mat = new Mat(imgPath, ImreadModes.AnyColor);
    31. pictureBox1.Image = bmp;
    32. }
    33. private void button1_Click(object sender, EventArgs e)
    34. {
    35. if (pictureBox1.Image == null)
    36. {
    37. return;
    38. }
    39. //BGR
    40. Mat[] mats = Cv2.Split(mat);
    41. if (radioButton1.Checked)
    42. {
    43. //灰度图
    44. pictureBox2.Image = BitmapConverter.ToBitmap(mats[0]);
    45. pictureBox3.Image = BitmapConverter.ToBitmap(mats[1]);
    46. pictureBox4.Image = BitmapConverter.ToBitmap(mats[2]);
    47. //Window b = new Window("B", WindowMode.Normal);
    48. //Window g = new Window("G", WindowMode.Normal);
    49. //Window r = new Window("R", WindowMode.Normal);
    50. //b.ShowImage(mats[0]);
    51. //g.ShowImage(mats[1]);
    52. //r.ShowImage(mats[2]);
    53. }
    54. else
    55. {
    56. //彩色图
    57. //创建一个空通道0
    58. Mat zero = new Mat(mats[0].Size(), MatType.CV_8UC1, new Scalar(0));
    59. Mat bsum = new Mat();
    60. Mat gsum = new Mat();
    61. Mat rsum = new Mat();
    62. Cv2.Merge(new Mat[] { mats[0], zero, zero }, bsum);//(b,0,0)图像
    63. Cv2.Merge(new Mat[] { zero, mats[1], zero }, gsum);//(0,g,0)图像
    64. Cv2.Merge(new Mat[] { zero, zero, mats[2] }, rsum);//(0,0,r)图像
    65. pictureBox2.Image = BitmapConverter.ToBitmap(bsum);
    66. pictureBox3.Image = BitmapConverter.ToBitmap(gsum);
    67. pictureBox4.Image = BitmapConverter.ToBitmap(rsum);
    68. }
    69. }
    70. private void Form1_Load(object sender, EventArgs e)
    71. {
    72. radioButton1.Checked = true;
    73. }
    74. }
    75. }

    下载 

    Demo下载

  • 相关阅读:
    【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(8 月 31 日论文合集)
    fastadmin列表,关联筛选查询
    PHP中的面向对象编程
    C#快速排序算法
    spring中 shiro logout 配置方式
    重磅!Adobe收购Figma
    认识柔性数组
    【strlen()的使用方式】简洁明了初识C语言
    【TGRS 2023】RingMo: A Remote Sensing Foundation ModelWith Masked Image Modeling
    代码随想录 Day-44|#70 爬楼梯(进阶)|#322 零钱兑换|#279 完全平方数
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132661361