emgu cv 轮廓提取

第一步:

建c#窗体

添加好COM和引用

在窗体上添加两个picturebox 控件和一个按键

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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace lunkuo
{
    public partial class Form1 : Form
    {
        static string filename;
        public Form1()
        {
            
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                filename = open.FileName;
                pictureBox1.Load(filename); 
                Image<Gray, byte> src = new Image<Gray, byte>(open.FileName);
                test(src);
            }
        }

      
 private void test(Image<Gray, byte> src)
        {
            CvInvoke.cvThreshold(src, src, 50, 250, Emgu.CV.CvEnum.THRESH.CV_THRESH_OTSU);
            Image<Gray, byte> dst = new Image<Gray, byte>(src.Size);//存储最终结果图像
            IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址


            IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);//开辟内存区域


            int m;
            m = 88;//在不安全处理下获取的数据
            int n = CvInvoke.cvFindContours(src, Dynstorage, ref Dyncontour, m, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, new Point(1, 1));


            Seq<Point> DyncontourTemp1 = new Seq<Point>(Dyncontour, null);//方便对IntPtr类型进行操作


            Seq<Point> DyncontourTemp = DyncontourTemp1;
            for (; DyncontourTemp1 != null && DyncontourTemp1.Ptr.ToInt32() != 0; DyncontourTemp1 = DyncontourTemp1.HNext)
            {


                CvInvoke.cvDrawContours(dst, DyncontourTemp1, new MCvScalar(128, 128, 128), new MCvScalar(255, 255, 255), 0, 1, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, new Point(0, 0));


            }


            pictureBox2.Image = dst.ToBitmap();


        }
    }
}



结果如图所示:



void cvDrawContours( CvArr *img, CvSeq* contour, CvScalar external_color,

CvScalar hole_color, int max_level, int thickness=1, int line type=8 );

cvDrawContours函数在图像上画出轮廓,其中external_color指定外围轮廓的颜色,hole_color指定内部轮廓的颜色(孔),max_level是一个比较关键的参数,也比较难理解,它指定被画的轮廓的最大层数,等于0时只画出第一个轮廓,等于1时画出第一个轮廓以及在它后面的同一层所有的轮廓,等于2时下一层的轮廓也被画出来,以此类推。当max_level为负数时,同一层的轮廓不会被画出来,而是画出contour(当前轮廓)直至|max_level|-1层的子轮廓(特殊情况:当max_level = -1时,只画出当前轮廓)。



评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值