Python Opencv实践 - 凸包检测(ConvexHull)

复制代码
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread("../SampleImages/stars.png")
plt.imshow(img[:,:,::-1])

img_contour = img.copy()
#得到灰度图做Canny边缘检测
img_gray = cv.cvtColor(img_contour, cv.COLOR_BGR2GRAY)
edges = cv.Canny(img_gray, 120, 255, 0)
#提取并绘制轮廓
contours,hierarchy = cv.findContours(edges, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
img_contour = cv.drawContours(img_contour, contours, -1, (0,255,0), 2)
plt.imshow(img_contour, cmap=plt.cm.gray)

#凸包检测
#hull = cv.convexHull(points, clockwise, returnpoints)
#hull: 输出凸包结果,n*1*2数据结构,n为外包围圈的点的个数
#points: 输入的坐标点,通常为1* n * 2 结构,n为所有的坐标点的数目
#clockwise:转动方向,TRUE为顺时针,否则为逆时针;
#returnPoints:默认为TRUE,返回凸包上点的坐标,如果设置为FALSE,会返回与凸包点对应的轮廓上的点。
#参考资料:https://blog.csdn.net/lovetaozibaby/article/details/103214672
hulls = []
for contour in contours:
    hull = cv.convexHull(contour)
    hulls.append(hull)
img_convex_hull = cv.drawContours(img, hulls, -1, (0,255,0), 2)

plt.imshow(img_convex_hull[:,:,::-1])
相关推荐
DreamBoy@7 分钟前
Mnemra:一键剪藏,让灵感真正可复用(一键从Ai对话页面到飞书云文档,浏览器插件方便好用)
人工智能
2301_7950997412 分钟前
让 CSS Grid 自适应容器尺寸的动态布局方案
jvm·数据库·python
呆萌的代Ma17 分钟前
python读取并加载.env的配置文件
python
Muyuan199818 分钟前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
小陈phd27 分钟前
TensorRT 入门完全指南(一)——从核心定义到生态工具全解析
人工智能·笔记
CeshirenTester44 分钟前
从0到1学自动化测试该怎么规划?
人工智能
:mnong1 小时前
以知识驱动 AIAD 行业进化
人工智能·cad
U盘失踪了1 小时前
python curl转python脚本
开发语言·chrome·python
ZhengEnCi1 小时前
03-注意力机制基础 📚
人工智能