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进行轮廓寻找和计算物体凸包的更多相关文章

  1. OpenCV轮廓检测,计算物体旋转角度

    效果还是有点问题的,希望大家共同探讨一下 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : 定义控制台应用程序的入 ...

  2. opencv 6 图像轮廓与图像分割修复 1 查找并绘制轮廓 寻找物体的凸包

    查找并绘制轮廓 寻找轮廓(findContours)函数 绘制轮廓(drawContours()函数) 基础实例程序:轮廓查找 #include <opencv2/opencv.hpp> ...

  3. OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

    使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数  ...

  4. Halcon三 依据点关系计算物体三维位姿Halcon

    1.set_origin_pose( : : PoseIn, DX, DY, DZ : PoseNewOrigin) 平移POSEIN的原点,输出为新的原点.注意,平移沿着OBJ的坐标新进行,而非沿着 ...

  5. OpenCV计算物体的重心坐标(2值图像)

    效果图: 代码: // FindGravity.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  6. OpenCV 使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...

  7. OpenCV入门之寻找图像的凸包(convex hull)

    介绍   凸包(Convex Hull)是一个计算几何(图形学)中的概念,它的严格的数学定义为:在一个向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包.   在图像处理过程中,我们 ...

  8. 【计算机视觉】OpenCV篇(9) - 轮廓(寻找/绘制轮廓)

    什么是轮廓? 轮廓是一系列相连的点组成的曲线,代表了物体的基本外形. 轮廓与边缘好像挺像的? 是的,确实挺像,那么区别是什么呢?简而言之,轮廓是连续的,而边缘并不全都连续(见下图示例).其实边缘主要是 ...

  9. opencv6.5-imgproc图像处理模块之轮廓

    接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...

随机推荐

  1. Servicestack IRequestLogger获取 IHttpRequest

    我在 ServiceStack里实现了 Logging 中的请求与返回,同时我想在IRequestLogger.Log() 方法中获取 IHttpRequest . IRequestContext 并 ...

  2. Sql视图创建语句

    create view [dbo].[AllUsers] as select u.UserId, u.Firstname, u.Lastname, u.ts, am.Email, au.UserNam ...

  3. 中间件(middlebox)

    Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...

  4. word2vec 实践

    关于word2vec,这方面无论中英文的参考资料相当的多,英文方面既可以看官方推荐的论文,也可以看gensim作者Radim Řehůřek博士写得一些文章.而中文方面,推荐 @licstar的< ...

  5. C 语言学习的第 02 课:C 语言的开发环境

    工欲善其事,必先利其器.不知道还是不是记得上一篇文章中说到的,计算机本身是一个数据输入及输出的设备.所以,为了将你大脑中的各种 idea 输入到电脑,且最终生成能够执行的程序,总是要预备点什么的. 通 ...

  6. python作为一种胶水和c/c++

    如果需要用 Python 调用 C/C++ 编写的第三方库,只需要一个脚本语言来粘合它们.这个时候,用 Python ctypes 可以很方便地实现调用. StackOverflow 上的 Calli ...

  7. 【Android 开发】: Android 消息处理机制之一: Handler 与 Message

    最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...

  8. java保留两位小数

    java保留两位小数问题: 方式一: 四舍五入  double   f   =   111231.5585;  BigDecimal   b   =   new   BigDecimal(f);  d ...

  9. android 布局之scrollview

    今天在布局页面的时候后犯了难,我要显示的内容一个页面展示不完,怎么办呢? 于是随便找了个app点开一看,哎呀原来还能翻动啊!这是啥布局呢?原来是ScrollView 官方api相关的内容全是英文,这可 ...

  10. js日期显示效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...