python - 需要在 Python 中为已经找到的 Convex Hull 点找到 Convex Hull,绘制最外面的 Convex Hull 并获取它的 X 和 Y 坐标
问题描述
下面是我的程序,我试图找到图像的凸包并将该凸包点再次发送到 OpenCV 凸包以找到它的最外层凸包。
import cv2
import numpy as np
# Load the image
img1 = cv2.imread(r'test.tif')
# Convert it to greyscale
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# Threshold the image
ret, thresh = cv2.threshold(img,50,255,0)
# Find the contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img1, contours, -1, (255, 0, 0), 2)
hull=np.array([])
for i in range(len(contours)):
hullv=cv2.convexHull(contours[i])
hull=np.append(hull,hullv)
cv2.drawContours(img1, [hullv], 0, (255, 0, 0), 2)
conv2=cv2.convexHull(hull)
cv2.drawContours(img1, conv2, -1, (255, 0, 0), 2)
cv2.imwrite(r"contours2.png",img1)
执行此操作时,我看到以下错误。
Traceback (most recent call last):
File "C:/TestCode/DocumentLayoutDetection/ConvexHull.py", line 17, in <module>
conv2=cv2.convexHull(hull)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::convexHull'
解决方案
推荐阅读
- javascript - 三JS | 检测一个物体何时离开另一个物体
- php - PHP foreach xml列出所有出现
- mysql - MySQL Docker Image:使用 K8S Secret 的 env 变量初始化
- python - Odoo 13 中的页码
- javascript - Mongoose + GraphQL 无法填充模型
- xml - Eclipse 中 plugin.xml 文件的格式不起作用
- c# - 将 Web 项目 (ASP.NET Core 3) 设置为 IdentityServer 的客户端和 ApiResource
- php - 使用 Eval 函数时无法捕获异常
- python-3.x - 超集 keycloak 配置
- git - Azure Jenkins Pipeline:Azure Web App 发布阶段的错误包标头构建失败