-
Notifications
You must be signed in to change notification settings - Fork 1
/
boundingBox.py
115 lines (94 loc) · 3.47 KB
/
boundingBox.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
105
106
107
108
109
110
111
112
113
114
115
import os
import sys
import cv2
from cropper import crop
def seg_crop(file_name):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(BASE_DIR, "data/" + file_name)
img = cv2.imread(path)
img = crop(img, True)
img_gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img_gray = crop(img_gray, False)
ret, thresh1 = cv2.threshold(img_gray, 245, 255, cv2.THRESH_BINARY)
cv2.imwrite(
os.path.join(BASE_DIR, "Thresholds/thresholdcheck of " + file_name + ".jpg"), thresh1)
ret, thresh = cv2.threshold(
thresh1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
imga, contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print(len(contours))
for i in range(0, len(contours)):
area = cv2.contourArea(contours[i])
if i == 0:
maxArea = area
# print(maxArea)
# print(img_gray.size)
bigIndex = i
if area > maxArea:
maxArea = area
bigIndex = i
# print(cv2.contourArea(contours[2]))
# print(bigIndex)
# cv2.drawContours(img, contours[index], -1, (255,0,255),2, cv2.LINE_AA, maxLevel=2)
# print(hierarchy)
# maxAreaContour = hierarchy[index]
# print(maxAreaContour)
i = hierarchy[0][bigIndex][2]
# print(i)
count = 0
while True:
# print("here")
count += 1
x, y, w, h = cv2.boundingRect(contours[i])
boxArea = w * h
# print("BA:"+str(boxArea))
if boxArea < img_gray.size / 100:
# print("herein")
i = hierarchy[0][i][0]
# print(i)
if(i == -1):
break
continue
childIndex = hierarchy[0][i][2]
# cv2.drawContours(img, contours[childIndex], -1, (255,0,255),2,
# cv2.LINE_AA, maxLevel=2)
while True:
x, y, w, h = cv2.boundingRect(contours[childIndex])
contentArea = w * h
# print(contentArea)
if (contentArea > 900):
cropped_image = img[y:y + h, x:x + w]
# print(childIndex)
cv2.imwrite(
os.path.join(BASE_DIR, "croppedchildren/" + file_name[:-3] + " of " + str(childIndex) + ".jpg"), cropped_image)
img = cv2.rectangle(
img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.drawContours(img, contours[childIndex], -1, (
255, 0, 255), 2, cv2.LINE_AA, maxLevel=2)
break
if(hierarchy[0][childIndex][0] == -1):
break
else:
childIndex = hierarchy[0][childIndex][0]
# cv2.imwrite('convehull.jpg',img)
i = hierarchy[0][i][0]
if(hierarchy[0][i][0] == -1):
break
# pass
# cv2.drawContours(img, contours[52], -1, (255,0,255),2, cv2.LINE_AA,
# maxLevel=2)
nextBoxIndex = hierarchy[0][0][2]
# firtChild = contours[firstChildIndex]
# firstChildIndex = hierarchy[0][nextBoxIndex][2]
# print(firstChildIndex)
# print(contours[2])
cnt = contours[bigIndex]
x, y, w, h = cv2.boundingRect(cnt)
# img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# cropped_image = img[y:y+h, x:x+w]
cv2.imwrite(
os.path.join(BASE_DIR, 'Segmentation map/seg_map of ' + file_name + '.jpg'), img)
# cv2.imshow('img',img)
if __name__ == "__main__":
imga = seg_crop(sys.argv[1])
cv2.imshow('img', imga)