Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Udayraj123
GitHub Repository: Udayraj123/OMRChecker
Path: blob/master/src/processors/CropPage.py
214 views
1
"""
2
https://www.pyimagesearch.com/2015/04/06/zero-parameter-automatic-canny-edge-detection-with-python-and-opencv/
3
"""
4
import cv2
5
import numpy as np
6
7
from src.logger import logger
8
from src.processors.interfaces.ImagePreprocessor import ImagePreprocessor
9
from src.utils.image import ImageUtils
10
from src.utils.interaction import InteractionUtils
11
12
MIN_PAGE_AREA = 80000
13
14
15
def normalize(image):
16
return cv2.normalize(image, 0, 255, norm_type=cv2.NORM_MINMAX)
17
18
19
def check_max_cosine(approx):
20
# assumes 4 pts present
21
max_cosine = 0
22
min_cosine = 1.5
23
for i in range(2, 5):
24
cosine = abs(angle(approx[i % 4], approx[i - 2], approx[i - 1]))
25
max_cosine = max(cosine, max_cosine)
26
min_cosine = min(cosine, min_cosine)
27
28
if max_cosine >= 0.35:
29
logger.warning("Quadrilateral is not a rectangle.")
30
return False
31
return True
32
33
34
def validate_rect(approx):
35
return len(approx) == 4 and check_max_cosine(approx.reshape(4, 2))
36
37
38
def angle(p_1, p_2, p_0):
39
dx1 = float(p_1[0] - p_0[0])
40
dy1 = float(p_1[1] - p_0[1])
41
dx2 = float(p_2[0] - p_0[0])
42
dy2 = float(p_2[1] - p_0[1])
43
return (dx1 * dx2 + dy1 * dy2) / np.sqrt(
44
(dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10
45
)
46
47
48
class CropPage(ImagePreprocessor):
49
def __init__(self, *args, **kwargs):
50
super().__init__(*args, **kwargs)
51
cropping_ops = self.options
52
self.morph_kernel = tuple(
53
int(x) for x in cropping_ops.get("morphKernel", [10, 10])
54
)
55
56
def apply_filter(self, image, file_path):
57
image = normalize(cv2.GaussianBlur(image, (3, 3), 0))
58
59
# Resize should be done with another preprocessor is needed
60
sheet = self.find_page(image, file_path)
61
if len(sheet) == 0:
62
logger.error(
63
f"\tError: Paper boundary not found for: '{file_path}'\nHave you accidentally included CropPage preprocessor?"
64
)
65
return None
66
67
logger.info(f"Found page corners: \t {sheet.tolist()}")
68
69
# Warp layer 1
70
image = ImageUtils.four_point_transform(image, sheet)
71
72
# Return preprocessed image
73
return image
74
75
def find_page(self, image, file_path):
76
config = self.tuning_config
77
78
image = normalize(image)
79
80
_ret, image = cv2.threshold(image, 200, 255, cv2.THRESH_TRUNC)
81
image = normalize(image)
82
83
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, self.morph_kernel)
84
85
# Close the small holes, i.e. Complete the edges on canny image
86
closed = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
87
88
edge = cv2.Canny(closed, 185, 55)
89
90
if config.outputs.show_image_level >= 5:
91
InteractionUtils.show("edge", edge, config=config)
92
93
# findContours returns outer boundaries in CW and inner ones, ACW.
94
cnts = ImageUtils.grab_contours(
95
cv2.findContours(edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
96
)
97
# convexHull to resolve disordered curves due to noise
98
cnts = [cv2.convexHull(c) for c in cnts]
99
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
100
sheet = []
101
for c in cnts:
102
if cv2.contourArea(c) < MIN_PAGE_AREA:
103
continue
104
peri = cv2.arcLength(c, True)
105
approx = cv2.approxPolyDP(c, epsilon=0.025 * peri, closed=True)
106
if validate_rect(approx):
107
sheet = np.reshape(approx, (4, -1))
108
cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
109
cv2.drawContours(edge, [approx], -1, (255, 255, 255), 10)
110
break
111
112
return sheet
113
114