我在 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)
1 回答
白板的微信
TA贡献1883条经验 获得超3个赞
避免误报的一种好方法是考虑面积最大的轮廓:
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
这将删除小点,同时只给出最大的轮廓
添加回答
举报
0/150
提交
取消