首页 技术 正文
技术 2022年11月15日
0 收藏 641 点赞 2,951 浏览 3321 个字

http://blog.csdn.net/qq_22033759/article/details/48029493

一、轮廓寻找 
用的是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;
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

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

二、凸包的计算 
凸包的计算需要用到上面所求得的轮廓寻找的数据 
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;
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

运行图:(之前那个图片由于会被分为好多块,所以换了张图片) 
EmgnCv进行轮廓寻找和计算物体凸包

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862