DelaunayTriangulation_VoronoiDiagram_using_OpenCV的实现
前言
最近project中有关于delaunay的实现问题,查找资料,看到learnopencv这个网站,有很多peoject的实现,本文主要是实现Delaunay Triangulation and Voronoi Diagram using OpenCV这个project,基于Python实现的。
环境
系统环境:win7_64
python版本:python3.5.2
实现过程
1.下载代码和依赖的资源;
github上列出了learnopencv用到的全部代码,非常不错!推荐~
2.基于python的代码;
#!/usr/bin/python
#RE:http://www.learnopencv.com/delaunay-triangulation-and-voronoi-diagram-using-opencv-c-python/ import cv2
import numpy as np
import random # Check if a point is inside a rectangle
def rect_contains(rect, point) :
if point[0] < rect[0] :
return False
elif point[1] < rect[1] :
return False
elif point[0] > rect[2] :
return False
elif point[1] > rect[3] :
return False
return True # Draw a point
def draw_point(img, p, color ) :
#cv2.circle( img, p, 2, color, cv2.cv.CV_FILLED, cv2.CV_AA, 0 )
cv2.circle( img, p, 2, color, cv2.FILLED, 0 ) # Draw delaunay triangles
def draw_delaunay(img, subdiv, delaunay_color ) : triangleList = subdiv.getTriangleList();
size = img.shape
r = (0, 0, size[1], size[0]) for t in triangleList : pt1 = (t[0], t[1])
pt2 = (t[2], t[3])
pt3 = (t[4], t[5]) if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3) : cv2.line(img, pt1, pt2, delaunay_color, 1, 0)
cv2.line(img, pt2, pt3, delaunay_color, 1, 0)
cv2.line(img, pt3, pt1, delaunay_color, 1, 0) # Draw voronoi diagram
def draw_voronoi(img, subdiv) : ( facets, centers) = subdiv.getVoronoiFacetList([]) for i in range(0,len(facets)) :
ifacet_arr = []
for f in facets[i] :
ifacet_arr.append(f) ifacet = np.array(ifacet_arr, np.int)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) cv2.fillConvexPoly(img, ifacet, color, 0);
ifacets = np.array([ifacet])
cv2.polylines(img, ifacets, True, (0, 0, 0), 1, 0)
cv2.circle(img, (centers[i][0], centers[i][1]), 3, (0, 0, 0), cv2.FILLED, 0) if __name__ == '__main__': # Define window names
win_delaunay = "Delaunay Triangulation"
win_voronoi = "Voronoi Diagram" # Turn on animation while drawing triangles
animate = True # Define colors for drawing.
delaunay_color = (255,255,255)
points_color = (0, 0, 255) # Read in the image.
img = cv2.imread("obama.jpg"); # Keep a copy around
img_orig = img.copy(); # Rectangle to be used with Subdiv2D
size = img.shape
rect = (0, 0, size[1], size[0]) # Create an instance of Subdiv2D
subdiv = cv2.Subdiv2D(rect); # Create an array of points.
points = []; # Read in the points from a text file
with open("obama.txt") as file :
for line in file :
x, y = line.split()
points.append((int(x), int(y))) # Insert points into subdiv
for p in points :
subdiv.insert(p) # Show animation
if animate :
img_copy = img_orig.copy()
# Draw delaunay triangles
draw_delaunay( img_copy, subdiv, (255, 255, 255) );
cv2.imshow(win_delaunay, img_copy)
cv2.waitKey(100) # Draw delaunay triangles
draw_delaunay( img, subdiv, (255, 255, 255) ); # Draw points
for p in points :
draw_point(img, p, (0,0,255)) # Allocate space for Voronoi Diagram
img_voronoi = np.zeros(img.shape, dtype = img.dtype) # Draw Voronoi diagram
draw_voronoi(img_voronoi,subdiv) # Show results
cv2.imshow(win_delaunay,img)
cv2.imshow(win_voronoi,img_voronoi)
cv2.waitKey(0)
注意:官网上的代码由于版本的不同不能正常运行,需要调试通过;
3.代码调试;
调试代码的过程中遇到较多的问题,针对出现的问题一一进行解决;
3.1)ImportError: No module named 'cv2'

原因:没有安装cv2第三方库;
解决方法:
在Python的安装目录下打开.\Scripts路径,再次目录下打开cmd,使用以下命令安装cv2
pip3 install opencv-python
尝试过的方法
第一次安装使用的命令是
pip install cv2
但是没有正确安装;
后来找到另一个方法,刚看的时候觉得很不错,不过仔细看发现主要是针对python2.7版本的,之后就放弃了;

3.2)忘记加载资源文件;

原因:程序代码中需要用到图片和txt文件,将其加载到合适的路径下即可;
3.3)版本有关;

解决方法:直接将cv2.CV_AA remove即可;
3.4)版本问题;

解决方法:将cv2.cv.CV_FILLED替换为cv2.CV_FILLED即可;
3.5)版本问题;

解决方法:直接将cv2.CV_FILLED替换为cv2.FILLED即可;
3.6)不同版本的语法问题;

解决方法:将xrange替换为range即可;
之前也遇到过这个问题,现在想想应该是不同版本的Python的语法不同造成的;
4.运行结果;

参考
完
DelaunayTriangulation_VoronoiDiagram_using_OpenCV的实现的更多相关文章
随机推荐
- BIOS和CMOS【转载】
在我们的电脑中,都有一块黑色的小芯片.但是请千万不要小看它,如果它损坏或者数据错误乱套的话,恭喜,如果不会“救回”这个小芯片,那么这台电脑可以挂闲鱼卖零件了……这个小芯片是什么呢?对,它就是BIOS芯 ...
- typescripts学习
可选与默认参数 可选参数:在参数名后面,冒号前面添加一个问号,则表明该参数是可选的.如下代码: function buildName(firstName: string, lastName?: str ...
- Python 错误与异常
2017-08-01 13:40:17 在程序运行过程中,总会遇到各种各样的错误. 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复 ...
- Python 运算符重载
https://www.cnblogs.com/hotbaby/p/4913363.html
- 记一次无法正常本地登陆Linux服务器(确定密码正确)
首先,ssh可以正常登陆使用.但是,本地可以确定密码是正确的情况还是不能登陆. 然后查看/var/log/secure文件如下提示: 然后,尝试去看了下/etc/pam.d/login 下面(有问题的 ...
- LeetCode--108--将有序数组转化为二叉搜索树
问题描述: 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10 ...
- Confluence 6 启用嵌套用户组
一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上级用户组的权限,使系统的权限配置变得简单. 这个页面描述了 Confluence ...
- 42. Trapping Rain Water *HARD*
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- zabbix自动化监控基础
zabbix安装配置文档 2 一 zabbix-server 安装配置(基础配置) 2 二 zabbix agent安装配置 5 2.1 主动模式和被动模式 6 2.2 安装配置zabbix_agen ...
- BZOJ3895 取石子
Orz PoPoQQQ 我等蒟蒻只能想到石子数 ≥ 2时的情况...1的时候就爆搜?大概是这个意思 最后再记忆化一下 /**************************************** ...