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])
相关推荐
梨落秋霜1 小时前
Python入门篇【文件处理】
android·java·python
kisshuan123961 小时前
【深度学习】使用RetinaNet+X101-32x4d_FPN_GHM模型实现茶芽检测与识别_1
人工智能·深度学习
Java 码农1 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
Learn Beyond Limits2 小时前
解构语义:从词向量到神经分类|Decoding Semantics: Word Vectors and Neural Classification
人工智能·算法·机器学习·ai·分类·数据挖掘·nlp
崔庆才丨静觅2 小时前
0代码生成4K高清图!ACE Data Platform × SeeDream 专属方案:小白/商家闭眼冲
人工智能·api
qq_356448372 小时前
机器学习基本概念与梯度下降
人工智能
张登杰踩3 小时前
VIA标注格式转Labelme标注格式
python
水如烟3 小时前
孤能子视角:关系性学习,“喂饭“的小孩认知
人工智能
徐_长卿3 小时前
2025保姆级微信AI群聊机器人教程:教你如何本地打造私人和群聊机器人
人工智能·机器人
XyX——3 小时前
【福利教程】一键解锁 ChatGPT / Gemini / Spotify 教育权益!TG 机器人全自动验证攻略
人工智能·chatgpt·机器人