gpt4 book ai didi

python - OpenCV:Canny 边缘检测器获得 minEnclosingCircle

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:29 26 4
gpt4 key购买 nike

我正在使用 Canny 边缘检测器检测白色背景上的对象,并想在其周围绘制一个矩形和一个圆圈。我可以获得边界矩形的坐标,但不能获取 OpenCV 函数 minAreaRectminEnclosingCircle 的坐标。

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 边缘检测器的结果格式错误,但我找不到如何转换它以使其有效。

最佳答案

这些函数之间的区别在于,boundingRect 作用于图像,而 minEnclosingCircleminAreaRect 作用于二维点集。要从 Canny 的输出中获取点集,您可以按照 this tutorial 中的建议使用 findCountours :

# im2, contours, hierarchy = cv.findContours(thresh, 1, 2) # OpenCV 3.x
contours, hierarchy = cv.findContours(thresh, 1, 2) # OpenCV 4.x
cnt = contours[0]

rect = cv.minAreaRect(cnt)

(x,y),radius = cv.minEnclosingCircle(cnt)

关于python - OpenCV:Canny 边缘检测器获得 minEnclosingCircle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119075/

26 4 0
文章推荐: c# - 如何在 C# 中使用代理访问 Web 服务
文章推荐: css - 显示网格

行与网格

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com