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

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下载

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱蜂朝王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值