def _removeSmallerContours(self, image): cv2.rectangle(image, (0, 0), (640, 480), (0, 0, 0), thickness=1) contours, _ = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) bestContour = max(contours, key=cv2.contourArea) for contour in contours: if not numpy.array_equal(contour, bestContour): leftmostX = findLeftmostPoint(contour)[0] topmostY = findTopmostPoint(contour)[1] rightmostX = findRightmostPoint(contour)[0] bottommostY = findBottommostPoint(contour)[1] cv2.rectangle(image, (leftmostX, topmostY), (rightmostX, bottommostY), (255, 255, 255), thickness=-1) contours, _ = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) bestContour = max(contours, key=cv2.contourArea) for contour in contours: if not numpy.array_equal(contour, bestContour): leftmostX = findLeftmostPoint(contour)[0] topmostY = findTopmostPoint(contour)[1] rightmostX = findRightmostPoint(contour)[0] bottommostY = findBottommostPoint(contour)[1] cv2.rectangle(image, (leftmostX, topmostY), (rightmostX, bottommostY), (0, 0, 0), thickness=-1) cv2.rectangle(image, (0, 0), (640, 480), (0, 0, 0), thickness=169) return image
def discriminateContours(self, contours, rgbImage): bestContours = [] for contour in contours: bottomPointX, bottomPointY = findBottommostPoint(contour) pointsBelow = [(bottomPointX, bottomPointY + j * 5) for j in range(1, 5)] pointBelowContourX, pointBelowContourY = bottomPointX, bottomPointY + 10 for x, y in pointsBelow: if not all(coordinate < self.BLACK_COLOR_THRESHOLD for coordinate in rgbImage[y, x]): break else: bestContours.append(contour) return bestContours