opencv计算点集凸包

点集求凸包的问题经常会遇到,https://www.geeksforgeeks.org/orientation-3-ordered-points/  给出了一种思路,有空了再仔细研究,本文总结了opencv中相关的方法并给出一个实例。

参考:OpenCV: Structural Analysis and Shape Descriptors

1、opencv接口

hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]])

参数说明:

  1. points:点集。需要注意的是,该参数输入的坐标系是向右为X,向上为Y,和迪卡尔坐标系是相同的,而opencv中的图像坐标系是向右为X,向下为Y,即图像的左上角坐标为(0,0)。
  2. clockwise:如果设置为True,输出的凸包是顺时针的,如果设置为False,输出的凸包是逆时针的。
  3. returnPoints:如果设置为True,返回凸包的点坐标,如果设置为False,输出凸包点在点集points中的序号。

2、示例

代码如下:

# encoding:utf-8

import numpy as np
import cv2
import matplotlib.pyplot as plt

axis_list = [[3,1],[2,2],[3,3],[4,4],[2,4],[2,3],[1,2],[2,2],[2,1]]
axis_list = np.array(axis_list)

hull = cv2.convexHull(axis_list,clockwise=True,returnPoints=True)
print(hull)

hull = np.squeeze(hull)
plt.scatter(axis_list[:,0],axis_list[:,1])
plt.plot(hull[:,0],hull[:,1],"r")
plt.plot([hull[-1,0],hull[0,0]],[hull[-1,1],hull[0,1]],"r")
plt.show()

计算结果如下图所示,其中蓝色点是点集,红色线条是凸包轮廓。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值