-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcrop_utils.py
More file actions
104 lines (83 loc) · 2.95 KB
/
Copy pathcrop_utils.py
File metadata and controls
104 lines (83 loc) · 2.95 KB
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 cv2
def get_binary_image(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY_INV)
return binary_image
def separate_parts(image):
binary_image = get_binary_image(image)
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
if area < 500000:
continue
boxes.append((x, y, w, h))
boxes = sorted(boxes, key = lambda box: box[1])
parts = []
for box in boxes:
x, y, w, h = box
parts.append(image[y:y+h, x:x+w])
x = boxes[0][0]
y = boxes[0][1] + boxes[0][3]
w = boxes[0][2]
h = boxes[1][1] - y
parts.append(image[y:y+h, x:x+w])
part_1 = parts[0]
part_2 = parts[1]
part_3 = parts[2]
part_1_parts = separate_part_1(part_1)
part_2_parts = separate_part_2(part_2)
part_3_parts = separate_part_3(part_3)
return part_1_parts, part_2_parts, part_3_parts
def separate_part_1(part_1):
binary_image = get_binary_image(part_1)
contours, _ = cv2.findContours(binary_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
boxes = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
if area < 1500:
continue
boxes.append((x, y, w, h))
boxes = sorted(boxes, key = lambda box: (box[1], box[0]))
needed_idx = [3, 8, 11, 13, 15, 17, 20, 22, 24, 26]
boxes = [boxes[idx] for idx in needed_idx]
parts = []
for box in boxes:
x, y, w, h = box
parts.append(part_1[y:y+h, x:x+w])
return parts
def separate_part_2(part_2):
binary_image = get_binary_image(part_2)
contours, _ = cv2.findContours(binary_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
boxes = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
if area < 1500:
continue
boxes.append((x, y, w, h))
boxes = sorted(boxes, key = lambda box: (box[1], box[0]))
needed_idx = [3, 6, 8, 10, 20, 23, 25, 27]
boxes = [boxes[idx] for idx in needed_idx]
parts = []
for box in boxes:
x, y, w, h = box
parts.append(part_2[y:y+h, x:x+w])
return parts
def separate_part_3(part_3):
binary_image = get_binary_image(part_3)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1500, 30))
dilated_image = cv2.dilate(binary_image, kernel, iterations = 3)
contours, _ = cv2.findContours(dilated_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
boxes.append((x, y, w, h))
boxes = sorted(boxes, key = lambda box: box[1])
parts = []
for box in boxes:
x, y, w, h = box
parts.append(part_3[y:y+h, x:x+w])
return parts