我正在使用Canny边缘探测器检测一个白色背景上的物体,并想画一个矩形和一个圆圈围绕它。我可以得到边界矩形的坐标,但不能得到OpenCV函数minAreaRect
和minEnclosingCircle
的坐标。在
import cv2
import numpy as np
img = cv2.imread(image.path, 0)
edges = cv2.Canny(img, 100, 200)
#Bounding Rectangle works
x, y, w, h = cv2.boundingRect(edges)
#This does not work
(x,y),radius = cv2.minEnclosingCircle(edges)
#This also does not work
rect = cv2.minAreaRect(edges)
错误:
Traceback (most recent call last): File "/home/hschneider/workspace/onspiration/website/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-28-f9e34ac01335>", line 1, in <module> cv2.minEnclosingCircle(edges) cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:160: error: (-215:Assertion failed) count >= 0 && (depth == CV_32F || depth == CV_32S) in function 'minEnclosingCircle'
我想,这是因为Canny边缘检测器的结果格式错误,但我无法找到如何转换它,使其正常工作。在
这些函数的区别在于}作用于2D点集。要从
boundingRect
作用于图像,而asminEnclosingCircle
和{Canny
的输出中获得一个点集,您可以按照this tutorial中的建议使用findCountours
:相关问题 更多 >
编程相关推荐