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

}

}

}

cs 复制代码
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下载

相关推荐
小满Autumn3 小时前
log4net 日志框架 — 从配置到实战速查手册
笔记·c#·.net·wpf·上位机·log4net
yaoxin5211236 小时前
434. Java 日期时间 API - Period 基于日期的时间段
java·开发语言·python
凡人叶枫7 小时前
Effective C++ 条款30:透彻了解 inlining 的里里外外
linux·开发语言·c++·嵌入式开发·effective c++
学逆向的7 小时前
C++纯虚函数
开发语言·c++·网络安全
程序员二叉7 小时前
【JUC】ThreadLocal底层原理|内存泄漏|弱引用|跨线程传递方案
java·开发语言·面试·职场和发展·juc
程序员二叉7 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
JaydenAI8 小时前
[对比学习LangChain和MAF-07]如何引入人机交互的审批流程
python·ai·langchain·c#·agent·hitl·maf
凡人叶枫8 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
Qt程序员8 小时前
掌握 Linux 内核调度:从原理到实现(进程篇)
java·开发语言
code bean8 小时前
【LangChain】检索器完全指南:从向量检索到生产级 RAG 架构
java·开发语言·微服务