From 2f7644b0d3acb965e08449aa5433db3f97e5eb2b Mon Sep 17 00:00:00 2001 From: Robin Bijl <r.a.bijl@student.tudelft.nl> Date: Thu, 30 May 2019 16:29:08 +0200 Subject: [PATCH] adds check and tests for reference markers and documentation of function --- tests/test_scans.py | 24 ++++++++++++++++++++---- zesje/scans.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/tests/test_scans.py b/tests/test_scans.py index 7ad5ff11..5b61b30b 100644 --- a/tests/test_scans.py +++ b/tests/test_scans.py @@ -291,10 +291,7 @@ def test_realign_image(datadir, file_name): test_file = os.path.join(datadir, dir_name, file_name) test_image = np.array(PIL.Image.open(test_file)) - correct_file = os.path.join(datadir, dir_name, "a4.png") - correct_image = cv2.imread(correct_file) - - correct_corner_markers = scans.find_corner_marker_keypoints(correct_image) + correct_corner_markers = [(58, 58), (1180, 58), (58, 1694), (1180, 1694)] result_image, result_corner_markers = scans.realign_image(test_image) @@ -303,3 +300,22 @@ def test_realign_image(datadir, file_name): diff = np.absolute(np.subtract(correct_corner_markers[i], result_corner_markers[i])) assert diff[0] < epsilon assert diff[1] < epsilon + + +def test_incomplete_reference_realign_image(datadir): + dir_name = "cornermarkers" + epsilon = 2 + test_file = os.path.join(datadir, dir_name, "a4-rotated-3-markers.png") + test_image = cv2.imread(test_file) + + reference_markers = [(59, 59), (1179, 59), (1179, 1693)] + + correct_corner_markers = [(58, 58), (1180, 58), (58, 1694), (1180, 1694)] + + result_image, result_corner_markers = scans.realign_image(test_image, reference_keypoints=reference_markers) + + assert result_corner_markers is not None + for i in range(4): + diff = np.absolute(np.subtract(correct_corner_markers[i], result_corner_markers[i])) + assert diff[0] < epsilon + assert diff[1] < epsilon diff --git a/zesje/scans.py b/zesje/scans.py index 589238b4..02ad271f 100644 --- a/zesje/scans.py +++ b/zesje/scans.py @@ -794,6 +794,25 @@ def realign_image(image_array, keypoints=None, reference_keypoints=None): """ This function realigns an images based on the template image + + params + ------ + image_array : numpy.array + The image in the form of a numpy array. + keypoints : List[(int,int)] + tuples of coordinates of the found keypoints, (x,y), in pixels. Can be a set of 3 or 4 tuples. + if none are provided, they are calculated based on the image_array. + reference_keypoints: List[(int,int)] + Similar to keypoints, only these belong to the keypoints found on the original scan. + If none are provided, standard locations are used. Namely [(59, 59), (1179, 59), (59, 1693), (1179, 1693)], + which are from an ideal scan of an a4 at 200 dpi. + + returns + ------- + return_array: numpy.array + The image realign properly. + return_keypoints: List[(int,int)] + New keypoints properly aligned. """ if(keypoints is None): @@ -807,6 +826,10 @@ def realign_image(image_array, keypoints=None, if (reference_keypoints is None): reference_keypoints = [(59, 59), (1179, 59), (59, 1693), (1179, 1693)] + if (len(reference_keypoints) != 4): + # this function assumes that the template has the same dimensions as the input image + reference_keypoints = fix_corner_markers(reference_keypoints, image_array.shape) + rows, cols, _ = image_array.shape keypoints_32 = np.float32(keypoints) @@ -815,11 +838,12 @@ def realign_image(image_array, keypoints=None, # get the transformation matrix M = cv2.getPerspectiveTransform(keypoints_32, reference_keypoints_32) - # apply the transformation matrix + # apply the transformation matrix and fill in the new empty spaces with white return_image = cv2.warpPerspective(image_array, M, (cols, rows), borderValue=(255, 255, 255, 255)) - + # generate a new set of 4 keypoints return_keypoints = find_corner_marker_keypoints(return_image) + check_corner_keypoints(return_image, reference_keypoints) if(len(return_keypoints) != 4): return_keypoints = fix_corner_markers(return_keypoints, return_image.shape) -- GitLab