Python OpenCV4获取轮廓最大内切圆和外接圆
为了方便讲解,我们先来创建一个多边形做演示
第一步:创建图像,并绘制一个六边形。代码和生成图像如下:
# Create an image
r = 100
src = np.zeros((4*r, 4*r), dtype=np.uint8) # Create a sequence of points to make a contour
vert = [None]*6
vert[0] = (3*r//2, int(1.34*r))
vert[1] = (1*r, 2*r)
vert[2] = (3*r//2, int(2.866*r))
vert[3] = (5*r//2, int(2.866*r))
vert[4] = (3*r, 2*r)
vert[5] = (5*r//2, int(1.34*r))
# Draw it in src
for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3) cv.imshow("src", src)

第二步:查找轮廓,计算图像上的点到轮廓的距离
# Get the contours
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # Calculate the distances to the contour
raw_dist = np.empty(src.shape, dtype=np.float32)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
raw_dist[i,j] = cv.pointPolygonTest(contours[1], (j,i), True)
注意cv.RETR_TREE查找轮廓后轮廓是从外到内的排列顺序,那么contours[1]就是六边形的内边轮廓
第三步:获取轮廓内部距离轮廓最远的点(作为内切圆圆心)和最小距离(作为内切圆半径),绘制内切圆
# 获取最大值即内接圆半径,中心点坐标
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
minVal = abs(minVal)
maxVal = abs(maxVal) result = cv.cvtColor(src,cv.COLOR_GRAY2BGR) cv.circle(result,maxDistPt, np.int(maxVal),(0,255,0), 2, cv.LINE_8, 0)
cv.imshow('result', result)
最终效果:

当然,如果你还想获取外接圆,直接调用函数cv2.minEnclosingCircle(即可),下面是代码演示和效果:
# contours[0]对应最外层轮廓
center,radius = cv.minEnclosingCircle(contours[0])
cv.circle(result,(int(center[0]),int(center[1])),int(radius),(0,0,255),2)

完整代码:
import cv2 as cv
import numpy as np # Create an image
r = 100
src = np.zeros((4*r, 4*r), dtype=np.uint8) # Create a sequence of points to make a contour
vert = [None]*6
vert[0] = (3*r//2, int(1.34*r))
vert[1] = (1*r, 2*r)
vert[2] = (3*r//2, int(2.866*r))
vert[3] = (5*r//2, int(2.866*r))
vert[4] = (3*r, 2*r)
vert[5] = (5*r//2, int(1.34*r))
# Draw it in src
for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3) cv.imshow("src", src) # Get the contours
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # Calculate the distances to the contour
raw_dist = np.empty(src.shape, dtype=np.float32)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
raw_dist[i,j] = cv.pointPolygonTest(contours[1], (j,i), True) # 获取最大值即内接圆半径,中心点坐标
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
minVal = abs(minVal)
maxVal = abs(maxVal) result = cv.cvtColor(src,cv.COLOR_GRAY2BGR) center,radius = cv.minEnclosingCircle(contours[0])
cv.circle(result,(int(center[0]),int(center[1])),int(radius),(0,0,255),2) cv.circle(result,maxDistPt, np.int(maxVal),(0,255,0), 2, cv.LINE_8, 0)
cv.imshow('result', result) cv.waitKey(0)
cv.destroyAllWindows()
关注【OpenCV与AI深度学习】获取更多学习资讯
长按或者扫描下面二维码即可关注

Python OpenCV4获取轮廓最大内切圆和外接圆的更多相关文章
- 【NLP】Python NLTK获取文本语料和词汇资源
Python NLTK 获取文本语料和词汇资源 作者:白宁超 2016年11月7日13:15:24 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集 ...
- 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接
使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...
- Python中获取异常(Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置.下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序 ...
- python基础——获取对象信息
python基础——获取对象信息 当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type( ...
- python 单例模式获取IP代理
python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...
- python 如何获取当前文件/文件夹
python 如何获取当前文件/文件夹? 1.获取当前文件的实际路劲: os.path.realpath(__file__) ==> D:\python_test\test_p ...
- oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame
import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...
- 基于Python Shell获取hostname和fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- Python中获取异常(try Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置. 这里获取异常(Exception)信息采用try...except...程序结构.如下所示: try: ... exce ...
随机推荐
- dotnet学习系列
这里整理下之前关于dotnet方面的文章索引. 一.dotnet core 系列 dotnet core 微服务教程 asp.net core 系列之并发冲突 asp.net core 系列之中间件进 ...
- saltstack手册(含官方pdf)
官方手册 https://docs.saltstack.com/en/pdf/Salt-2019.2.1.pdf 快速入门 SALTSTACK是什么? Salt是一种和以往不同的基础设施管理方法,它是 ...
- 前端H5知识总结
两年大专生活匆匆而过,身边的朋友也都各奔东西,9月份开始实习感觉自己的前端功底有所欠缺,这个暑假除了打工我还有一个半月的学习时间希望自己能够充分利用这段时间.7月3号所学知识在此做以下总结以便自己复习 ...
- JS日期处理——月末、季度末
需求: 很多时候对于页面设置默认日期,我们有这样的需求—— 日期频度为月度:如果今天是本月最后一天,默认日期取今天,否则取上月最后一天: 日期频度为季度:如果今天是本季度最后一天,默认日期取今天,否则 ...
- 实验吧——因缺思汀的绕过(sql with rollup)
题目地址:http://ctf5.shiyanbar.com/web/pcat/index.php 通读源码,得知出flag的条件 1.需要post提交uname以及pwd,否则直接die了 if ( ...
- SAP CRM Product Interlinkage - Customer Product ID的一个例子
For detail technical introduction about relationship, please refer to this wiki. The relationship tr ...
- mysql单个表拆分成多个表
一.横向拆分 create table 新表的名称 select * from 被拆分的表 order by id limit int1,int2 int1为其实位置,int2为几条 注意:这样拆分后 ...
- Elasticsearch的快照备份
该文档适用于备份使用NAS的仓库类型.所有Elasticsearch集群中的服务通过挂载NAS目录来存放备份快照数据. 1.创建备份仓库 创建一个仓库名称:backup curl -H "C ...
- Linux的yum管理
前面介绍了软件的管理的方式rpm.但有个缺点,rpm不能解决依赖. 下面介绍的yum软件管理.可以完美的解决这个问题. 使用yum的方式管理rpm软件 优势:自动解决软件的依赖关系 ...
- Skeleton-Based Action Recognition with Directed Graph Neural Network
Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...