【问题标题】:OpenCV how to clear contour from noise and false positivesOpenCV如何从噪声和误报中清除轮廓
【发布时间】:2019-03-08 11:44:12
【问题描述】:

我在 Python 中使用标准函数 cv2.findContours 找到了图像轮廓。

但正如你所见,中间有一个点,如果不破坏轮廓线,我无法过滤它。

如何删除这样的误报集群? 轮廓之外的那些并不重要。

    gray = cv2.cvtColor(self.img, cv2.COLOR_RGB2GRAY)

    _, mask = cv2.threshold(gray, thresh=152, maxval=162, type=cv2.THRESH_BINARY)

    self.mask = cv2.bitwise_and(gray, mask)
    self.contours, hierarchy = cv2.findContours(self.mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

【问题讨论】:

  • 能否请您发布您尝试过的代码?看看你是怎么做到的。
  • 尝试使用cv.RETR_EXTERNAL作为检索模式选项,参见here
  • 是的,我正在使用这个选项。分享我的代码。

标签: python opencv contour


【解决方案1】:

避免误报的一个好方法是考虑面积最大的轮廓:

import imutils #to use sorting functionality 
#To install imutils run: pip install imutils for python 2.x
#Run: pip3 install imutils for python 3.x

cnts = imutils.grab_contours(cnts) #where cnts is the variable in which contours are stored, replace it with your variable name
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10] #sorts contours according to their area from largest to smallest.

largestCont = cnts[0] #store the largest contour

这将删除小点,同时只给出最大的轮廓

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2016-09-25
    • 2019-03-20
    • 1970-01-01
    • 2020-09-23
    • 2018-01-18
    相关资源
    最近更新 更多