【问题标题】:cv2.contourArea not working properlycv2.contourArea 无法正常工作
【发布时间】:2018-07-06 17:05:15
【问题描述】:

我想做的是在夜间地球的照片中找到大的光污染区域。我将源照片转换为灰度,然后转换为具有阈值的二进制照片。 cv2.findcontours 工作正常,但是当我试图摆脱小轮廓时,它只会删除其中的一部分。

Source image

import cv2
image_orig=cv2.imread('C:\Users\pc\Desktop\middleeast.jpg')
image_gray=cv2.cvtColor(image_orig,cv2.COLOR_BGR2GRAY)
_, image_threshold=cv2.threshold(image_gray,60,255,cv2.THRESH_BINARY)
_,contours,_=cv2.findContours(image_threshold,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
image_contours=image_orig.copy()
cv2.drawContours(image_contours,contours,-1,(255,255,0),1)
cv2.imshow('image_contours',image_contours)
cv2.imwrite('C:\Users\pc\Desktop\middleEastAllContours.jpg', image_contours)
for counter,contour in enumerate(contours):
    if cv2.contourArea(contour)<250.0:
        contours.pop(counter)
image_big_contours=image_orig.copy()
cv2.drawContours(image_big_contours,contours,-1,(255,255,0),1)
cv2.imshow('big contours',image_big_contours)
cv2.waitKey(0)

如您所见,轮廓上仍有许多小光污染区域。我怎样才能摆脱它们?

All contours comparison

Big contours comparison

Source image all contoursSource image big contours

【问题讨论】:

    标签: python image opencv contour area


    【解决方案1】:

    我认为问题在于 for 的弹出:当您弹出一个轮廓时,您正在跳过下一个。

    例如,如果您弹出轮廓编号 10,那么下一个计数将变为编号 10,但您将跳过它,因为在下一次迭代中您将查看轮廓 11。

    我不是 python 专家(我真的不知道这两个变量在 for 中是如何使用的)但你可以尝试在 pop 之后执行 counter=counter-1。另一种选择是向后迭代列表(从最后一个元素开始并在第一个元素结束)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2019-12-23
      • 1970-01-01
      • 2013-02-20
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多