Skip to content
Snippets Groups Projects
Commit 9e131021 authored by Ruben Young On's avatar Ruben Young On
Browse files

Added some fixes and a test

parent 53c5d035
No related branches found
No related tags found
1 merge request!17Add pregrading
Pipeline #17915 failed
tests/data/cornermarkers/a4-3-markers.png

14.9 KiB

tests/data/cornermarkers/a4-rotated-3-markers.png

15.4 KiB

tests/data/cornermarkers/a4-rotated.png

16.1 KiB

import cv2
import os
import numpy as np
from zesje.images import fix_corner_markers, guess_dpi
from zesje.scans import find_corner_marker_keypoints, process_page
def test_three_straight_corners_1():
shape = (240, 200, 3)
corner_markers = [(50, 50), (120, 50), (50, 200)]
corner_markers = fix_corner_markers(corner_markers, shape)
assert (120, 200) in corner_markers
def test_three_straight_corners_2():
shape = (240, 200, 3)
corner_markers = [(120, 50), (50, 200), (120, 200)]
corner_markers = fix_corner_markers(corner_markers, shape)
assert (50, 50) in corner_markers
def test_pdf(datadir):
# Max deviation of inferred corner marker and actual location
epsilon = 2
# Scan rotated image with 4 corner markers
image_filename1 = 'a4-rotated.png'
image_path = os.path.join(datadir, 'cornermarkers', image_filename1)
page_img = cv2.imread(image_path)
corners1 = find_corner_marker_keypoints(page_img)
# Scan the same image with 3 corner markers
image_filename2 = 'a4-rotated-3-markers.png'
image_path = os.path.join(datadir, 'cornermarkers', image_filename2)
page_img = cv2.imread(image_path)
corners2 = find_corner_marker_keypoints(page_img)
# Get marker that was removed
diff = [corner for corner in corners1 if corner not in corners2]
diff_marker = min(diff)
fixed_corners2 = fix_corner_markers(corners2, page_img.shape)
added_marker = [corner for corner in fixed_corners2 if corner not in corners2][0]
# Check if 'inferred' corner marker is not too far away
dist = np.linalg.norm(np.subtract(added_marker, diff_marker))
assert dist < epsilon
......@@ -38,34 +38,32 @@ def get_box(image_array, box, padding=0.3):
return image_array[top:bottom, left:right]
def fix_corner_markers(corner_keypoints, image, image_format):
def fix_corner_markers(corner_keypoints, shape):
"""
Corrects the list of corner markers if only three corner markers are found.
This function raises if only two corner keypoints are detected.
Params
------
Parameters
----------
corner_keypoints: list of corner marker locations as tuples
image: the image of the page with the corner markers
dpi: the dpi of the image in which the corner markers are from
image_format: either A4 or US letter
Returns
-------
A list of four corner markers.
"""
if len(corner_keypoints) == 4:
pass
return corner_keypoints
# TODO: raise runtime error here?
if len(corner_keypoints) < 3:
raise RuntimeError
dpi = guess_dpi(image)
# TODO: add support for US letter format
x_len = 210
y_len = 297
raise RuntimeError("Two or fewer corner markers detected")
x_sep = x_len / dpi * 25.4
y_sep = y_len / dpi * 25.4
x_sep = shape[1] / 2
y_sep = shape[0] / 2
top_left = [(x, y) for x, y in corner_keypoints if x < x_sep and y < y_sep]
bottom_left = [(x, y) for x, y in corner_keypoints if x < x_sep and y > y_sep]
......@@ -77,28 +75,28 @@ def fix_corner_markers(corner_keypoints, image, image_format):
(dx, dy) = tuple(map(sub, top_right[0], bottom_right[0]))
top_left = tuple(map(add, bottom_left[0], (dx, dy)))
return corner_keypoints + top_left
return corner_keypoints + [top_left]
if not bottom_left:
# Bottom left point is missing
(dx, dy) = tuple(map(sub, top_right[0], bottom_right[0]))
bottom_left = tuple(map(sub, top_left[0], (dx, dy)))
return corner_keypoints + bottom_left
return corner_keypoints + [bottom_left]
if not top_right:
# Top right point is missing
(dx, dy) = tuple(map(sub, top_left[0], bottom_left[0]))
top_right = tuple(map(add, bottom_right[0], (dx, dy)))
return corner_keypoints + top_right
return corner_keypoints + [top_right]
if not bottom_right:
# bottom right
(dx, dy) = tuple(map(sub, top_left[0], bottom_left[0]))
bottom_right = tuple(map(sub, top_right[0], (dx, dy)))
return corner_keypoints + bottom_right
return corner_keypoints + [bottom_right]
def box_is_filled(image_array, box_coords, padding=0.3, threshold=150, pixels=False):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment