首页 > 解决方案 > 需要在 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'

输入图像在此处输入图像描述

输出图像在此处输入图像描述

标签: pythonopencvimage-processing

解决方案


推荐阅读