EmgnCv进行轮廓寻找和计算物体凸包

一、轮廓寻找
用的是FindContours函数,在CvInvoke中
不过需要用到这个VectorOfVectorOfPoint,来代替c++中的Vector
还有就是FindContours函数中的第三个参数hierarchy,不知道作用是什么,填入的只要是符合IOutputArray类型的都可以运行
代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.Util;
using Emgu.CV.UI;

namespace EmguCVHist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Image<Bgr, byte> a = new Image<Bgr, byte>("153_140703112619_1.jpg");
            Image<Gray, byte> b = new Image<Gray, byte>(a.Width, a.Height);
            Image<Gray, byte> c = new Image<Gray, byte>(a.Width, a.Height);
            Image<Bgr, byte> d = new Image<Bgr, byte>(a.Width, a.Height);
            CvInvoke.Canny(a, b, 100, 60);
            VectorOfVectorOfPoint con = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(b, con, c, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);
            for (int i = 0; i < con.Size; i++)
                CvInvoke.DrawContours(d, con, i, new MCvScalar(255, 0, 255, 255),2);
            imageBox1.Image = d;
        }
    }
}

这里写图片描述

二、凸包的计算
凸包的计算需要用到上面所求得的轮廓寻找的数据
ConvexHull函数的参数之前的不太一样,换成了PointF类型的,但所求的数据为VectorOfVectorOfPoint类型,所以就需要个转换,先把VectorOfVectorOfPoint转为Point的二维数组,在把二维数组转成PointF的二维数组,然后再挨个调用,
代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.Util;
using Emgu.CV.UI;

namespace EmguCVHist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Image<Bgr, byte> a = new Image<Bgr, byte>("9660416_102608612175_2.jpg");
            Image <Gray, byte> b = new Image<Gray, byte>(a.Width, a.Height);
            Image<Gray, byte> c = new Image<Gray, byte>(a.Width, a.Height);
            Image<Bgr, byte> d = new Image<Bgr, byte>(a.Width, a.Height);
            CvInvoke.Canny(a, b, 100, 60);
            VectorOfVectorOfPoint con = new VectorOfVectorOfPoint();
            CvInvoke.FindContours(b, con, c, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);

            Point[][] con1 = con.ToArrayOfArray();
            PointF[][] con2 = Array.ConvertAll<Point[], PointF[]>(con1, new Converter<Point[], PointF[]>(PointToPointF));
            for (int i = 0; i < con.Size; i++)
            {
                PointF[] hull = CvInvoke.ConvexHull(con2[i], true);
                for (int j = 0; j < hull.Length; j++)
                {
                    Point p1 = new Point((int)(hull[j].X + 0.5), (int)(hull[j].Y + 0.5));
                    Point p2;
                    if (j == hull.Length - 1)
                        p2 = new Point((int)(hull[0].X + 0.5), (int)(hull[0].Y + 0.5));
                    else
                        p2 = new Point((int)(hull[j + 1].X + 0.5), (int)(hull[j + 1].Y + 0.5));
                    CvInvoke.Circle(d, p1, 3, new MCvScalar(0, 255, 255, 255), 6);
                    CvInvoke.Line(d, p1, p2, new MCvScalar(255, 255, 0, 255), 3);
                }
            }
            for (int i = 0; i < con.Size; i++)
                CvInvoke.DrawContours(d, con, i, new MCvScalar(255, 0, 255, 255), 2);

            imageBox1.Image = a.ConcateVertical(d);
        }
        public static PointF[] PointToPointF(Point[] pf)
        {
            PointF[] aaa = new PointF[pf.Length];
            int num = 0;
            foreach(var point in pf)
            {
                aaa[num].X = (int)point.X;
                aaa[num++].Y = (int)point.Y;
            }
            return aaa;
        }
    }
}

运行图:(之前那个图片由于会被分为好多块,所以换了张图片)
这里写图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cccccc1212

这是c币不是人民币,不要充值

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

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

打赏作者

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

抵扣说明:

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

余额充值