-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop.py
104 lines (65 loc) · 2.67 KB
/
crop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import cv2
import numpy
import click
TEXT_MIN_WIDTH = 35
TEXT_MIN_HEIGHT = 10
DEFAULT_WIDTH = 850
DEFAULT_HEIGHT = 1100
KERNEL_WIDTH = 25
KERNEL_HEIGHT = 15
def remove_borders(image, threshold, max_width, max_height):
height, width = image.shape[:2]
for i in range(max_width):
total = image[:, i].sum() / 255
if total > threshold:
image[:, i] = numpy.ones(height) * 255
total = image[:, width - i - 1].sum() / 255
if total > threshold:
image[:, i - 1] = numpy.ones(height) * 255
for i in range(max_height):
total = image[i, :].sum() / 255
if total > threshold:
image[i, :] = numpy.ones(width) * 255
total = image[height - i - 1, :].sum()
if total > threshold:
image[height - i - 1, :] = numpy.ones(width) * 255
return image
def crop(filename):
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray = remove_borders(gray, 0.8, 15, 15)
adjusted_width = image.shape[1] / DEFAULT_WIDTH
adjusted_height = image.shape[0] / DEFAULT_HEIGHT
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (KERNEL_WIDTH, KERNEL_HEIGHT))
eroded = cv2.erode(gray, kernel)
_, bw = cv2.threshold(eroded, 127, 255, cv2.THRESH_BINARY_INV)
total, markers = cv2.connectedComponents(bw)
images = [numpy.uint8(markers==i) * 255 for i in range(total) if numpy.uint8(markers==i).sum() > 10]
rectangles = []
for label in images:
countours = cv2.findContours(label, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
(x,y,w,h) = cv2.boundingRect(countours[0])
rectangles.append((x, y, w, h, label.sum() / 255.0))
rectangles = sorted(rectangles, key=lambda x:x[4], reverse=True)
rectangles = rectangles[1:]
expanded = [sys.maxsize, sys.maxsize, -sys.maxsize, -sys.maxsize]
for rect in rectangles:
x0, y0, w0, h0 = expanded
x1, y1, w1, h1, _ = rect
if w1 <= (TEXT_MIN_WIDTH * adjusted_width):
continue
if h1 <= (TEXT_MIN_HEIGHT * adjusted_height):
continue
x = min(x0, x1)
y = min(y0, y1)
w = max(x0 + w0, x1 + w1) - x
h = max(y0 + h0, y1 + h1) - y
expanded = [x, y, w, h]
return image[y:y+h, x:x+w]
@click.command()
@click.argument('input_filepath', type=click.Path(exists=True))
@click.argument('output_filepath', type=click.Path())
def main(input_filename, output_filename):
cropped = crop(input_filename)
cv2.imwrite(output_filename, cropped)