【问题标题】:Error in ellipses using Opencv python使用 Opencv python 的省略号错误
【发布时间】:2018-04-03 16:58:14
【问题描述】:

我有一个输入图像,想在找到的所有轮廓上绘制椭圆。我正在使用 python-opencv 并收到以下错误。谁能帮我这个?我知道绘制椭圆,但不确定如何绘制图像中每个检测到的对象。我是这个领域的新手,因此请原谅我的愚蠢问题。

OpenCV Error: Incorrect size of input array (There should be at least 5 points to fit the ellipse) in cv::fitEllipse, file 
C:\bld\opencv_1498171314629\work\opencv-3.2.0\modules\imgproc\src\shapedescr.cpp, line 358
Traceback (most recent call last):
File "D:/project/test.py", line 41, in <module>
ellipse = cv2.fitEllipse(contours[0])
cv2.error: C:\bld\opencv_1498171314629\work\opencv- 
3.2.0\modules\imgproc\src\shapedescr.cpp:358: error: (-201) There should be 
at least 5 points to fit the ellipse in function cv::fitEllipse

【问题讨论】:

  • 请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
  • 错误信息是不言自明的,不是吗?
  • @Piglet,是的。但我仍然在努力解决这个问题。
  • 在确保点数至少为 5 之后,您只适合椭圆。只需将 fitEllipse 调用放入 if 语句中

标签: python python-3.x opencv computer-vision opencv-contour


【解决方案1】:

ellipse=cv2.fitEllipse(contours[0])你正在拟合第一个轮廓,它的点数少于 5...你应该遍历所有轮廓并只绘制超过 5 个点的轮廓。 .

试试这样的:

import numpy as np
import cv2

image=cv2.imread("cell.jpg")

grey=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 
grey_hist=cv2.calcHist([grey],[0],None,[256],[0,256])
eq=cv2.equalizeHist(grey)
blurredA1=cv2.blur(eq,(3,3))

(T,thresh)=cv2.threshold(blurredA1,190,255,cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

if len(contours) != 0:
  for i in range(len(contours)):
    if len(contours[i]) >= 5:
      cv2.drawContours(thresh,contours,-1,(150,10,255),3)
      ellipse=cv2.fitEllipse(contours[0])
    else:
      # optional to "delete" the small contours
      cv2.drawContours(thresh,contours,-1,(0,0,0),-1)

cv2.imshow("Perfectlyfittedellipses",thresh)
cv2.waitKey(0)

如您所见,我迭代了所有轮廓并检查它是否大于 5。此外,我添加了一些东西以使所有不够大的轮廓变得模糊。

【讨论】:

    【解决方案2】:

    我不知道你的输入图像是怎样的,所以这可能会也可能不会。当您在查找轮廓函数中使用 cv2.RETR_EXTERNAL 时,它只返回外部轮廓。相反,请使用“cv2.RETR_TREE”。

    这会检索所有轮廓并重建嵌套轮廓的完整层次结构。有关文档,请参阅 here。您的代码应更改如下。

    im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    

    希望这会有所帮助。如果它不起作用,如果您可以上传您的输入图像以便我们进行处理,那就太好了!

    另外,至于在找到的每个轮廓上放置一个椭圆,就像提到的 api55 一样,您正在尝试仅在第一个轮廓上拟合椭圆。希望他的回答对你有所帮助。如果你想在最大的轮廓上放置一个椭圆,你可以根据它们的面积对找到的轮廓进行排序,然后在最大的轮廓上或大于一定大小的轮廓上拟合椭圆。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2012-02-01
      • 2013-12-05
      • 1970-01-01
      • 2013-04-12
      相关资源
      最近更新 更多