def get_shape_from_contour(contour, contour_box): # uppack bounding box of contour x, y, w, h = contour_box # list of contours (only contains the first contour, really) shifted so # that they can be drawn in a box the size of the bounding box contours_shifted = [np.array([[[j[0] - x, j[1] - y] for j in i] for i in contour])] # create image with filled contour shape_img = util.draw_contour(contours_shifted, 0, h, w) # for each card in trainings set, find one with most similarity diffs = [] training_set = get_training_set() for i, shape_ref in training_set.items(): # resize image shape_img_res = util.resize(shape_img, shape_ref.shape) # find diff and its sum d = cv2.absdiff(shape_img_res, shape_ref) sum_diff = np.sum(d) diffs.append(sum_diff) return diffs.index(min(diffs)) + 1
def get_shape_contour(img): rect = get_shape_bounding_rect(img) if rect is None: return None y1, y2, x1, x2, contours = rect shape_img = util.draw_contour(contours, 1) cropped = shape_img[y1:y2, x1:x2] return cropped