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