C# OpenCvSharp 通道分离

效果

C# OpenCvSharp 通道分离_第1张图片

C# OpenCvSharp 通道分离_第2张图片

项目

C# OpenCvSharp 通道分离_第3张图片

 代码

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

Demo下载 

你可能感兴趣的:(AI,C#,OpenCV,c#,开发语言,C#通道分离)