def test_detect_englobing_polygon_error(): array = np.ones(shape=(200, 200, 3), dtype='uint8') * 250 try: points = detect_englobing_polygon(array) assert False, 'Detected points while no polygon in the image' except LookupError: pass
def test_detect_englobing_polygon_simple(): array = np.ones(shape=(5000, 5000, 3), dtype='uint8') * 250 array[200:1800, 200:2800, :] = 0 points = detect_englobing_polygon(array) assert len(points) == 4 points = [tuple(*el) for el in points] assert (200, 200) in points assert (200, 1790) in points assert (2790, 200) in points assert (2790, 1790) in points
def test_detect_englobing_polygon_photo(): array = _image('sheet.jpg') points = detect_englobing_polygon(array).astype(int) assert len(points) == 4 area = cv2.contourArea(points) points = [(p[0, 0], p[0, 1]) for p in points] h, w = get_target_rectangle_size(points) assert h * w > (array.shape[0] * array.shape[1])* 1 / 3 assert w > array.shape[0]/2 assert h > array.shape[1]/2 assert h * w > area
def main(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("image", help="path to the image file") args = ap.parse_args() image = cv2.imread(args.image) image = resize(image, width=2000) pts = detect_englobing_polygon(image) pts = [tuple(*el) for el in pts] warped = process(image, pts) pts = np.array(pts, np.int32) pts = pts.reshape((-1,1,2)) image = cv2.polylines(image,[pts],True,(0,255,255), 10) cv2.imshow("Original", resize(image, width=1000)) cv2.imshow("Warped", resize(warped, width=1000)) cv2.waitKey(0)