一、简介

二、绘制点集的凸包

 #include<opencv2/opencv.hpp>
using namespace cv; void main()
{
//---绘制点集的凸包
Mat img(, , CV_8UC3, Scalar::all()); //定义绘制图像
RNG rng; //定义随机数对象
while()
{
char key;
int count = (unsigned int)rng % ; //定义点的个数
vector<Point> points; //定义点集
for(int i=; i<count; i++)
{
Point pt;
pt.x = rng.uniform(img.cols/, img.cols*/); //设定点的x范围
pt.y = rng.uniform(img.rows/, img.rows*/); //设定点的y范围
points.push_back(pt);
} //检测凸包
vector<int> hull;
convexHull(Mat(points), hull, true); img = Scalar::all();
for(int i = ; i < count; i++ )
circle(img, points[i], , Scalar(rng.uniform(, ), rng.uniform(, ), rng.uniform(, )), CV_FILLED, CV_AA); //准备参数
int hullcount = (int)hull.size(); //凸包的边数
Point point0 = points[hull[hullcount-]]; //连接凸包边的坐标点 //绘制凸包的边
for(int i = ; i < hullcount; i++ )
{
Point point = points[hull[i]];
circle(img, point, , Scalar(, , ), , );
line(img, point0, point, Scalar(, , ), , CV_AA);
point0 = point;
} //显示效果图
imshow("img", img); //按下ESC,Q,或者q,程序退出
key = (char)waitKey();
if( key == || key == 'q' || key == 'Q' )
break;
}
}

三、绘制轮廓的凸包

 #include<opencv2/opencv.hpp>
using namespace cv; void main()
{
Mat srcImg = imread("E://12.jpg");
imshow("src", srcImg);
Mat dstImg2 = srcImg.clone();
Mat tempImg(srcImg.rows, srcImg.cols, CV_8UC3, Scalar::all()); //用于绘制凸包
Mat dstImg(srcImg.rows, srcImg.cols, CV_8UC3, Scalar::all()); //用于绘制轮廓
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化 vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<vector<Point>> hull(contours.size());
for(int i=; i<contours.size(); i++)
{
convexHull(Mat(contours[i]), hull[i], true); //查找凸包
drawContours(dstImg, contours, i, Scalar(, , ), -, ); //绘制轮廓
//drawContours(dstImg, hull, i, Scalar(rand()%255, rand()%255, rand()%255), 2, 8);
drawContours(tempImg, hull, i, Scalar(, , ), -, );
}
imshow("hull", tempImg);
imshow("contours", dstImg); Mat diffImg;
absdiff(tempImg, dstImg, diffImg); //图像相减
Mat element = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -));
erode(diffImg, diffImg, element);
imshow("diff", diffImg); vector<vector<Point>> contours2;
vector<Vec4i> hierarcy2;
cvtColor(diffImg, diffImg, CV_BGR2GRAY); //转为灰度图
threshold(diffImg, diffImg, , , CV_THRESH_BINARY); //二值化
findContours(diffImg, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(dstImg2, contours2, -, Scalar(, , ), , ); //红色绘制缺陷轮廓
imshow("defects", dstImg2);
waitKey();
}

opencv学习之路(24)、轮廓查找与绘制(三)——凸包的更多相关文章

  1. opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点

    一.简介 二.画出每个轮廓的每个点 #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src= ...

  2. opencv学习之路(22)、轮廓查找与绘制(一)

    一.简介 图2 二.代码 #include"opencv2/opencv.hpp" #include<iostream> using namespace std; us ...

  3. opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

    一.点与轮廓的距离及位置关系 #include "opencv2/opencv.hpp" #include <iostream> using namespace std ...

  4. opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓

    一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...

  5. opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形

    一.简介 二.外接矩形的查找绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //外接矩形的查找 ...

  6. opencv学习之路(29)、轮廓查找与绘制(八)——轮廓特征属性及应用

    一.简介 HSV颜色空间(hue色调,saturation饱和度,value亮度) 二.HSV滑动条 #include "opencv2/opencv.hpp" #include ...

  7. opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形

    一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...

  8. opencv学习之路(39)、PCA

    一.PCA理论介绍 网上已经有许多介绍pca原理的博客,这里就不重复介绍了.详情可参考 http://blog.csdn.net/zhongkelee/article/details/44064401 ...

  9. opencv学习之路(37)、运动物体检测(二)

    一.运动物体轮廓椭圆拟合及中心 #include "opencv2/opencv.hpp" #include<iostream> using namespace std ...

随机推荐

  1. 【CF542D】Superhero's Job 暴力

    [CF542D]Superhero's Job 题意:$ f(x)=\sum\limits_{d|x,gcd(d,{x\over d})=1} d$ 给出 $A$ ,求方程 $f(x)=A$ 的正整数 ...

  2. java源码中的注解

    spring框架源码中充满了注解,如果对注解不是很了解,阅读源码就寸步难行,下面我们来看看annotation.https://blog.csdn.net/briblue/article/detail ...

  3. 第七天 py

  4. Ubuntu16.04 本地提权漏洞复测过程

    一.漏洞概述 Ubuntu 16.04 版本且unprivileged_bpf_disable 权限没有关闭的情况下就会存在 提权漏洞查看方式:1,cat /proc/version 查看系统版本 2 ...

  5. 27、 jq 拖拽

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

  6. 6 关于plsql中文显示乱码问题

    打开plsql查看数据表时,会看到中文显示乱码问题 解决方案 输入下图所示语句并运行 在输入下图所示语句并运行,查看第一行和第九行是否对应 3)查看下环境变量的设置,查看是否有变量NLS_LANG,没 ...

  7. Mysql 索引迁移策略

    Mysql 索引迁移策略 近日在核查项目中的一些慢sql时发现一个很鸡仔儿的问题,本地开发库表中索引跟生产上差距很大,又因为生产库登录各种麻烦,需要各种验证码,那么多的慢sql分给好些个人,不可能让大 ...

  8. Oracle课程档案,第四天

    “子查询”就是查询中嵌套着另一个查询,也即通过SELECT语句的嵌套使用形成子查询.当我们不知道特定的查询条件时,可以用子查询来为父查询提供查询条件以获得查询结果. 子查询先清除子查询 在清除主查询 ...

  9. Python学习之旅(十七)

    Python基础知识(16):面向对象编程(Ⅰ) 类和实例 类是抽象的模板 实例是根据类创建出来的一个个具体的对象,每个对象都拥有相同的方法,但各自的数据可能不同. 类可以在创建实例的时候,把一些我们 ...

  10. ubuntu14.04 LTS 更新国内网易163源

    2015/10/7 更改ubuntu的默认源是linux学习中必须掌握的基础技能.在此记录,以作参考. 在ubuntu14.04 LTS默认使用的是国外源,由于网络的原因,使用apt-get安装包时异 ...