目录
效果
代码
下载
效果
代码
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 void button1_Click(object sender, EventArgs e)
{
Mat srcImage = Cv2.ImRead("test.jpg");
Mat dstImage = ToolsFindContours(srcImage);
Bitmap Bitmap1 = BitmapConverter.ToBitmap(srcImage);
Bitmap Bitmap2 = BitmapConverter.ToBitmap(dstImage);
pictureBox1.Image = Bitmap1;
pictureBox2.Image = Bitmap2;
}
/// <summary>
/// 查找轮廓
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
public static Mat ToolsFindContours(Mat srcImage)
{
// 转化为灰度图
Mat src_gray = new Mat();
Cv2.CvtColor(srcImage, src_gray, ColorConversionCodes.RGB2GRAY);
// 滤波
Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3));
// Canny边缘检测
Mat canny_Image = new Mat();
// 输入、输出、最小阀值、最大阀值
Cv2.Canny(src_gray, canny_Image, 100, 200);
// 获得轮廓
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
/*
1.寻找轮廓的图像
2.返回轮廓数组
3.层次结构索引
4.轮廓的检索模式(External只检测外轮廓,List检测所有轮廓,CComp检测所有轮廓并建立两个等级,Tree检测所有轮廓并建立等级树
5.轮廓近似模式(ApproxNone保存物体边界上所有连续的轮廓点, ApproxSimple仅保存轮廓的拐点信息。CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用Teh-Chin chain 近似算法)
6.Point偏移量,所有的轮廓信息相对于原始图像对应点的偏移量
*/
Cv2.FindContours(canny_Image, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
// 将结果画出并返回结果
Mat dst_Image = Mat.Zeros(canny_Image.Size(), srcImage.Type());
for (int i = 0; i < contours.Length; i++)
{
// 轮廓的颜色为绿色
Scalar color = new Scalar(0, 255, 0);
/*
1.输入图
2.表示输入的轮廓组
3.指明画第几个轮廓
4.颜色
5.thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部
6.线形
7.轮廓结构信息
*/
Cv2.DrawContours(dst_Image, contours, i, color, 2, LineTypes.Link8, hierarchly);
}
return dst_Image;
}
}
}文章来源:https://www.toymoban.com/news/detail-851669.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 void button1_Click(object sender, EventArgs e)
{
Mat srcImage = Cv2.ImRead("test.jpg");
Mat dstImage = ToolsFindContours(srcImage);
Bitmap Bitmap1 = BitmapConverter.ToBitmap(srcImage);
Bitmap Bitmap2 = BitmapConverter.ToBitmap(dstImage);
pictureBox1.Image = Bitmap1;
pictureBox2.Image = Bitmap2;
}
/// <summary>
/// 查找轮廓
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
public static Mat ToolsFindContours(Mat srcImage)
{
// 转化为灰度图
Mat src_gray = new Mat();
Cv2.CvtColor(srcImage, src_gray, ColorConversionCodes.RGB2GRAY);
// 滤波
Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3));
// Canny边缘检测
Mat canny_Image = new Mat();
// 输入、输出、最小阀值、最大阀值
Cv2.Canny(src_gray, canny_Image, 100, 200);
// 获得轮廓
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
/*
1.寻找轮廓的图像
2.返回轮廓数组
3.层次结构索引
4.轮廓的检索模式(External只检测外轮廓,List检测所有轮廓,CComp检测所有轮廓并建立两个等级,Tree检测所有轮廓并建立等级树
5.轮廓近似模式(ApproxNone保存物体边界上所有连续的轮廓点, ApproxSimple仅保存轮廓的拐点信息。CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用Teh-Chin chain 近似算法)
6.Point偏移量,所有的轮廓信息相对于原始图像对应点的偏移量
*/
Cv2.FindContours(canny_Image, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
// 将结果画出并返回结果
Mat dst_Image = Mat.Zeros(canny_Image.Size(), srcImage.Type());
for (int i = 0; i < contours.Length; i++)
{
// 轮廓的颜色为绿色
Scalar color = new Scalar(0, 255, 0);
/*
1.输入图
2.表示输入的轮廓组
3.指明画第几个轮廓
4.颜色
5.thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部
6.线形
7.轮廓结构信息
*/
Cv2.DrawContours(dst_Image, contours, i, color, 2, LineTypes.Link8, hierarchly);
}
return dst_Image;
}
}
}
下载
Demo下载文章来源地址https://www.toymoban.com/news/detail-851669.html
到了这里,关于C# OpenCvSharp 轮廓检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!