实现内容:

设置一副图像大小为600*600。图像像素值全为0,为黑色。

在图像中Rect(100,100,400,400)的区域随机产生20个点。并画出。

产生这些点集的Delaunay剖分和Voronoi图。并画出。

程序

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream> using namespace cv;
using namespace std; //随机产生一个点集
vector<Point2f> generatePointSet(int n,Rect rect)
{
vector<Point2f> pointSet;
for (int i = 0; i < n;i++)
{
Point2f fp((float)(rand() % (rect.width - 2*rect.x) + rect.x),
(float)(rand() % (rect.height - 2*rect.y) + rect.y));
pointSet.push_back(fp);
}
return pointSet;
} //标记出点
static void drawPoint(Mat& img, Point2f fp, Scalar color)
{
circle(img, fp, 3, color, CV_FILLED, 8, 0);
} //画出点集
static void drawPointSet(Mat& img, vector<Point2f> pointSet, Scalar color)
{
for (int i = 0; i < pointSet.size();i++)
{
drawPoint(img, pointSet[i], color);
}
} //画出剖分
static void drawSubdiv(Mat& img, Subdiv2D& subdiv, Scalar delaunay_color)
{
vector<Vec6f> triangleList;
subdiv.getTriangleList(triangleList);
vector<Point> pt(3); for (size_t i = 0; i < triangleList.size(); i++)
{
Vec6f t = triangleList[i];
pt[0] = Point(cvRound(t[0]), cvRound(t[1]));
pt[1] = Point(cvRound(t[2]), cvRound(t[3]));
pt[2] = Point(cvRound(t[4]), cvRound(t[5]));
line(img, pt[0], pt[1], delaunay_color, 1, CV_AA, 0);
line(img, pt[1], pt[2], delaunay_color, 1, CV_AA, 0);
line(img, pt[2], pt[0], delaunay_color, 1, CV_AA, 0);
}
} //画出Voronoi图
static void paintVoronoi(Mat& img, Subdiv2D& subdiv)
{
vector<vector<Point2f> > facets;
vector<Point2f> centers;
subdiv.getVoronoiFacetList(vector<int>(), facets, centers); vector<Point> ifacet;
vector<vector<Point> > ifacets(1); for (size_t i = 0; i < facets.size(); i++)
{
ifacet.resize(facets[i].size());
for (size_t j = 0; j < facets[i].size(); j++)
ifacet[j] = facets[i][j]; Scalar color;
color[0] = rand() & 255;
color[1] = rand() & 255;
color[2] = rand() & 255;
fillConvexPoly(img, ifacet, color, 8, 0); ifacets[0] = ifacet;
polylines(img, ifacets, true, Scalar(), 1, CV_AA, 0);
circle(img, centers[i], 3, Scalar(), CV_FILLED, CV_AA, 0);
}
} int main()
{
//创建点集
Rect rect(100,100,400,400); Mat img(rect.size(), CV_8UC3);
img = Scalar::all(0); vector<Point2f> pointset = generatePointSet(20,rect);
Mat img_ptst = img.clone();
drawPointSet(img_ptst,pointset,Scalar(0,255,0));
imshow("Point set",img_ptst);
imwrite("pointSet.jpg",img_ptst); //创建Delaunay剖分
Subdiv2D subdiv(rect);
for (int i = 0; i < pointset.size();i++)
{
subdiv.insert(pointset[i]);
} //画出Delaunay剖分三角形
Mat img_delaunay = img.clone();
drawSubdiv(img_delaunay, subdiv, Scalar(255,255,255));
imshow("Delaunay", img_delaunay);
imwrite("delaunay.jpg", img_delaunay); //画出Voronoi图
Mat img_voronoi = img.clone();
paintVoronoi(img_voronoi, subdiv);
imshow("Voronoi", img_voronoi);
imwrite("voronoi.jpg", img_voronoi); waitKey(0);
return 0;
}

结果

OpenCV生成点集的Delaunay剖分和Voronoi图的更多相关文章

  1. Delaunay剖分与平面欧几里得距离最小生成树

    这个东西代码我是对着Trinkle的写的,所以就不放代码了.. Delaunay剖分的定义: 一个三角剖分是Delaunay的当且仅当其中的每个三角形的外接圆内部(不包括边界)都没有点. 它的存在性是 ...

  2. Voronoi图和Delaunay三角剖分

    刷题的时候发现了这么一个新的东西:Voronoi图和Delaunay三角剖分 发现这个东西可以$O(nlogn)$解决平面图最小生成树问题感觉非常棒 然后就去学了.. 看的n+e的blog,感谢n+e ...

  3. C++ 生成 voronoi 图 & C++生成泰森多边形图形

    1. 功能 生成voronoi图的一个类 2. 代码 VoronoiDiagramGenerator.h #pragma once //Microsoft Visual Studio 2015 Ent ...

  4. Voronoi图及matlab实现

    [题外话:想一想真是...美赛时我预测求爱尔兰的充电站位置分布,画Voronoi图,程序跑了一个小时...]   Voronoi图,又叫泰森多边形或Dirichlet图,它是由一组由连接两邻点直线的垂 ...

  5. 渲染voronoi图

    渲染voronoi图要比计算voronoi图简单. 渲染voronoi图: 方法1: 在pixel shader里,对每一个像素,求哪个种子点到它的距离最近,将此种子点的颜色作为此像素颜色. 当种子点 ...

  6. Arcgis做出voronoi图

    人类第一步,,,我需要给我目前的基站点数据划分voronoi,预期得到每个基站的服务范围 在地统计模块geostatistical analysis 下面的数据探索expore就有Voronoi图 将 ...

  7. 《图像处理实例》 之 Voronoi 图

    Voronoi 图的设计 以下的改进是http://www.imagepy.org/的作者原创,我只是对其理解之后改进和说明,欢迎大家使用这个小软件! 如有朋友需要源工程,请在评论处留邮箱! 说明:类 ...

  8. python opencv 生成验证码

    基本思路是使用opencv来把随机生成的字符,和随机生成的线段,放到一个随机生成的图像中去. 虽然没有加复杂的形态学处理,但是目前看起来效果还不错 尝试生成1000张图片,但是最后只有998张,因为有 ...

  9. opencv生成灰度图并保存

    #include <opencv2/opencv.hpp>#include <iostream> using namespace cv;using namespace std; ...

随机推荐

  1. 声卡(Sound Card)基本概念

    声卡 (Sound Card)是实现声音的模拟/数字信号相互转换.信号处理的一种硬件. 声卡的基本功能是把来自话筒.磁带.光盘的原始声音信号加以转换(模数转换或者数模转换),输出到耳机.扬声器.扩音机 ...

  2. BZOJ 2190:[SDOI2008]仪仗队(欧拉函数)

    [SDOI2008]仪仗队 Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视 ...

  3. 【SPOJ694】Distinct Substrings (SA)

    求不相同子串个数    该问题等价于求所有后缀间不相同前缀的个数..也就是对于每个后缀suffix(sa[i]),将贡献出n-sa[i]+1个,但同时,要减去那些重复的,即为height[i],故答案 ...

  4. POJ2749 Building roads 【2-sat】

    题目 Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to ...

  5. redis学习(四)redis持久化之RDB、AOF

    redis是内存数据库,它把数据存储在内存中,这样在加快读取速度的同时也对数据安全性产生了新的问题,即当redis所在服务器发生宕机后,redis数据库里的所有数据将会全部丢失.为了解决这个问题,re ...

  6. python3的cookielib

    http://stackoverflow.com/questions/8405096/python-3-2-cookielib

  7. 控制cxGrid 主从表的明细只展开一个

    procedure TForm.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIn ...

  8. python安装matplotlib

    linux安装 方法: 首先matplotlib是需要numpy先行包支持的,这里,我已经安装了numpy,下面安装matplotlib. matplot需要一些其他软件支持 (1)这时需要安装fre ...

  9. 存储过程中set什么什么的讲解

    原文发布时间为:2008-09-27 -- 来源于本人的百度文章 [由搬家工具导入] set ansi_nulls [on/off] 与 set quoted_identifier [on/off] ...

  10. 汉化CodeBlock

    codeblock最新版本发布了,但是对一些看不惯英文的来说,还是中文好点. 一.准备工作,先下载codeblock最新版,可以从官方下载,也可以从http://www.uzzf.com/soft/1 ...