【问题标题】:How do I find ten biggest contours?我如何找到十个最大的轮廓?
【发布时间】:2018-07-09 15:59:30
【问题描述】:

我是 OpenCV 的新手,我对自己在做什么不是很了解。 我正在地球黑暗面的照片中寻找十个最大的光污染区域。 在找到每个光污染区域的轮廓后,如何创建一个具有十个最大轮廓的变量? 我试过了

biggest_contours=(0,0,0,0,0,0,0,0,0,0)
for enum,contour in enumerate(contours):
    for item in biggest_contours:
        if cv2.contourArea(contours[enum])>cv2.contourArea(contours[item]):
            biggest_contours[item]=enum

但它返回“TypeError: 'tuple' object does not support item assignment”

【问题讨论】:

  • 你有这个错误,因为tuple 在 Python 中是不可变的,一旦创建就不能更改它,请改用 list。

标签: python opencv find contour area


【解决方案1】:

为什么不直接按面积对轮廓进行排序并保留最后 10 个?

largest_contours = sorted(contours, key=cv2.contourArea)[-10:]

【讨论】:

  • 我刚刚尝试了你的建议,当我尝试使用 cv2.drawContours(image_contours,largest_contours,-1,(255,255,0),1) 时,轮廓不存在
  • 没有更多代码很难知道,试试cv2.drawContours(image_contours, largest_contours, 0, (255,255,255,255), -1)
  • pastebin.com/hKmjVM3c 这是我正在使用的代码,由于某种原因它不显示排序轮廓
  • 当我使用轮廓而不是最大轮廓时,一切正常
  • 这很奇怪,我自己使用它,所以知道它有效。根据 opencv 版本,返回不同 contours, hierarchy = cv2.findContours() 但由于它与 contours 一起使用,它之前应该已经失败。 print len(largest_contours) 应该给你10
猜你喜欢
  • 2017-04-06
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2016-12-10
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
相关资源
最近更新 更多