目录
效果
项目
代码
下载
效果
项目
代码
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;
}
}
}
文章来源:https://www.toymoban.com/news/detail-700422.html
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下载文章来源地址https://www.toymoban.com/news/detail-700422.html
到了这里,关于C# OpenCvSharp 通道分离的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!