def segment_image(img): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1] filtered_contours = [] for i, c in enumerate(contours): area = cv2.contourArea(c) if area < 1000: filtered_contours.append(c) filtered_contours.sort( key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0]) masks = [] start_x = 6 start_y = 10 width = 41 height = 70 contour_index = -1 for i in range(5): x = start_x + (i * width) sub_mask = img[start_y:start_y + height, x:x + width] curr_contours = [] for j, c in enumerate(filtered_contours[contour_index:]): mid_x, _ = features_util.mid_bbox(cv2.boundingRect(c)) if mid_x > x < x + width: if contour_index != -1: contour_index = j curr_contours.append(c) contour_index = -1 cv2.drawContours(sub_mask, curr_contours, -1, (255, 255, 255), -1) masks.append(sub_mask) return masks
def get_masked_images(image): contours = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_L1)[0] contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))) mask = np.zeros(image.shape[:2], dtype="uint8") masks = [] curr_contours = [] sub_mask = mask.copy() for i, c in enumerate(contours): x, y, w, h = cv2.boundingRect(c) mid_x = x + (w/2) next_x = -1 if i < len(contours) - 1: x2, y2, w2, h2 = cv2.boundingRect(contours[i+1]) next_x = x2 + (w2/2) curr_contours.append(c) if next_x == -1 or abs(mid_x - next_x) > 6: cv2.drawContours(sub_mask, curr_contours, -1, (255, 255, 255), -1) x1, y1, x2, y2 = largest_bounding_rect(curr_contours) curr_contours = [] cropped = sub_mask[y1:y2, x1:x2] masks.append(cropped) sub_mask = mask.copy() filtered_masks = [] for mask in masks: if mask.shape[1] > 10: filtered_masks.append(mask) return filtered_masks
def get_masked_images(img, x_dist): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1] # Sort contours by x value. contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0]) # Combine contours that are less than 'x_dist' apart from each on the x-axis (or 30 on the y). contours = features_util.combine_contours(contours, x_dist, 40) #contours = features_util.combine_contours_better(filtered_contours, x_dist, 30) masks = [] for c in contours: mask = np.zeros(img.shape, "uint8") cv2.drawContours(mask, c, -1, (255, 255, 255), -1) min_y, max_y, min_x, max_x = features_util.crop_to_content(mask, padding=2) mask = mask[min_y:max_y, min_x:max_x] masks.append(mask) # Split masks into two, if they are over '24' pixels in width. # This is because some letters are too close to each other for the contours # to be seperated. filtered_masks = [] for mask in masks: if mask.shape[1] > 24: mask1 = mask[:, 0:12] mask2 = mask[:, 12:] filtered_masks.append(mask1) filtered_masks.append(mask2) elif mask.shape[0] > 12 or mask.shape[1] > 12: filtered_masks.append(mask) return filtered_masks
def segment_image(img): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0]) contours = features_util.combine_contours(contours, 3, 20) for c in contours: if 15 < cv2.contourArea(c[0]) < 35: return True return False
def get_contour_positions(img): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] contours = features_util.combine_contours(contours, 3) bboxes = [features_util.largest_bounding_rect(c) for c in contours] bboxes.sort(key=lambda x: x[2], reverse=True) # Sort by width of contour bbox. bboxes = [features_util.mid_bbox(bbox) for bbox in bboxes] return (bboxes[0], bboxes[1])
def segment_image(img): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1] min_y = 10000 min_cont = -1 for i, cont in enumerate(contours): _, mid_y = features_util.mid_bbox(cv2.boundingRect(cont)) if mid_y < min_y: min_y = mid_y min_cont = i contours.pop(min_cont) mask = np.zeros(img.shape, dtype="uint8") cv2.drawContours(mask, contours, -1, (255, 255, 255), 1) cv2.drawContours(mask, contours, -1, (255, 255, 255), -1) mask = crop_to_symbol(mask) return mask
def segment_image(img): contours = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] filtered_contours = [] for c in contours: area = cv2.contourArea(c) if area < 1000: filtered_contours.append(c) contours = filtered_contours contours.sort(key=lambda c: features_util.mid_bbox(cv2.boundingRect(c))[0]) contours = features_util.combine_contours(contours, 5, 30) masks = [] for c in contours: mask = np.zeros(img.shape[:2], dtype="uint8") cv2.drawContours(mask, c, -1, (255, 255, 255), -1) masks.append(mask) return masks