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])
相关推荐
PPIO派欧云1 小时前
PPIO上线Qwen-VL-8B/30B、GLM-4.5-Air等多款中小尺寸模型
人工智能
御承扬1 小时前
编程素养提升之EffectivePython(Builder篇)
python·设计模式·1024程序员节
chenchihwen2 小时前
AI代码开发宝库系列:FAISS向量数据库
数据库·人工智能·python·faiss·1024程序员节
张登杰踩2 小时前
工业产品表面缺陷检测方法综述:从传统视觉到深度学习
人工智能·深度学习
sponge'2 小时前
opencv学习笔记6:SVM分类器
人工智能·机器学习·支持向量机·1024程序员节
I'm a winner2 小时前
基于YOLO算法的医疗应用专题:第一章 计算机视觉与深度学习概述
算法·yolo·计算机视觉
zandy10113 小时前
2025年AI IDE的深度评测与推荐:从单一功能效率转向生态壁垒
ide·人工智能
andyguo3 小时前
ChatGPT Atlas vs Chrome:AI 浏览器的新纪元
人工智能·chrome·chatgpt
AI视觉网奇3 小时前
json 可视化 2025 coco json
python·1024程序员节
北京迅为3 小时前
【北京迅为】iTOP-4412精英版使用手册-第六十七章 USB鼠标驱动详解
linux·人工智能·嵌入式·4412