def find_contours(cls, image, threshold=THRESHOLD): image_gray = utils.BGR_to_gray(image) ret, thresh = cv2.threshold(image_gray, threshold, 255, 0) base_contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # base_contours = [utils.approx_contour(cnt) for cnt in base_contours] contours = [] for contour in base_contours: area = cv2.contourArea(contour) mask = utils.mask_from_contour_points(image, contour) color = cv2.mean(image, mask=mask) perimeter = cv2.arcLength(contour, True) M = cv2.moments(contour) try: centroid_x = int(M['m10']/M['m00']) centroid_y = int(M['m01']/M['m00']) contours.append(Contour(contour, color, centroid_x, centroid_y, area, perimeter)) except ZeroDivisionError: pass return contours
def mask_image(self, image): mask = utils.mask_from_contour_points(image, self.points) return utils.apply_mask(image, mask)