EmgnCv进行轮廓寻找和计算物体凸包
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
二、凸包的计算
凸包的计算需要用到上面所求得的轮廓寻找的数据
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进行轮廓寻找和计算物体凸包的更多相关文章
- OpenCV轮廓检测,计算物体旋转角度
效果还是有点问题的,希望大家共同探讨一下 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : 定义控制台应用程序的入 ...
- opencv 6 图像轮廓与图像分割修复 1 查找并绘制轮廓 寻找物体的凸包
查找并绘制轮廓 寻找轮廓(findContours)函数 绘制轮廓(drawContours()函数) 基础实例程序:轮廓查找 #include <opencv2/opencv.hpp> ...
- OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体
使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数 ...
- Halcon三 依据点关系计算物体三维位姿Halcon
1.set_origin_pose( : : PoseIn, DX, DY, DZ : PoseNewOrigin) 平移POSEIN的原点,输出为新的原点.注意,平移沿着OBJ的坐标新进行,而非沿着 ...
- OpenCV计算物体的重心坐标(2值图像)
效果图: 代码: // FindGravity.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- OpenCV 使用二维特征点(Features2D)和单映射(Homography)寻找已知物体
#include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...
- OpenCV入门之寻找图像的凸包(convex hull)
介绍 凸包(Convex Hull)是一个计算几何(图形学)中的概念,它的严格的数学定义为:在一个向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包. 在图像处理过程中,我们 ...
- 【计算机视觉】OpenCV篇(9) - 轮廓(寻找/绘制轮廓)
什么是轮廓? 轮廓是一系列相连的点组成的曲线,代表了物体的基本外形. 轮廓与边缘好像挺像的? 是的,确实挺像,那么区别是什么呢?简而言之,轮廓是连续的,而边缘并不全都连续(见下图示例).其实边缘主要是 ...
- opencv6.5-imgproc图像处理模块之轮廓
接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...
随机推荐
- 【BZOJ1001】狼抓兔子
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 7530 Solved: 1724[Submit][S ...
- Android Bundle
#Bundle类介绍 Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. 我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean ...
- JNI系列——常见错误
1.本地方法没有找到 原因一:在Java代码中没有加载对应的类 原因二:在.c文件中将本地的方法名转换错误 2.本地库返回为空 原因一:加载的库名称错误 原因二:生成的库与部署设备平台错误
- model 的验证
Ext.onReady(function(){ Ext.define('User', { extend: 'Ext.data.Model', fields: [ { name: 'name', typ ...
- HTML5基础知识(4)--white-space属性
1.white-space 属性设置如何处理元素内的空白. 这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认值: no ...
- oracle中SET DEFINE意思
et define off关闭替代变量功能 在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符 号,SQL*Plus就会要你输入替代值.这就意味着 ...
- JSP 中的 tag 文件
在jsp文件中,可以引用 tag 和tld 文件,本文主要针对 tag 对于tag 文件 1)将此类文件放在 WEB-INF 下,比如 /WEB-INF/tags,tags 是目录,其下可以有多个.t ...
- quartz使用案例
@Service public class QuartzServiceImpl extends BaseServiceImpl<JobDetails, String, QuartzTaskVO& ...
- Web前端性能优化教程02:使用内容分发网络
基础知识 服务器离用户越近,HTTP请求的响应时间将更短. CNAME:别名记录,当多个域名需要指向同一服务器IP,可以使用一个域名做A记录指向该服务器IP,然后让多个域名指向该A记录. ICP:In ...
- 升级ubuntu,apt-get update出现Hash Sum mismatch
sudo apt-get update 出现Hash Sum mismatch cd /var/lib/apt sudo rm -fr lists sudo mkdir lists sudo mkdi ...