1
+ import cv2
2
+ import numpy as np
3
+
4
+ def segmentspaces_input (PATH ,filename ):
5
+ # Insert location of image here
6
+ image = cv2 .imread (PATH )
7
+ ret ,image = cv2 .threshold (image ,220 ,255 ,cv2 .THRESH_BINARY )
8
+
9
+ gray = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
10
+ _ , thresh = cv2 .threshold (gray , 127 , 255 , cv2 .THRESH_BINARY_INV + cv2 .THRESH_OTSU )
11
+
12
+ kernel = np .ones ((5 , 5 ), np .uint8 )
13
+ img_dilated = cv2 .dilate (thresh , kernel , iterations = 1 )
14
+
15
+ cnts , _ = cv2 .findContours (img_dilated .copy (), cv2 .RETR_EXTERNAL , cv2 .CHAIN_APPROX_SIMPLE )
16
+
17
+ # Array of initial bounding rects
18
+ rects = []
19
+
20
+ # Bool array indicating which initial bounding rect has
21
+ # already been used
22
+ rectsUsed = []
23
+
24
+ # Just initialize bounding rects and set all bools to false
25
+ for cnt in cnts :
26
+ rects .append (cv2 .boundingRect (cnt ))
27
+ rectsUsed .append (False )
28
+
29
+ # Sort bounding rects by x coordinate
30
+ def getXFromRect (item ):
31
+ return item [0 ]
32
+
33
+ rects .sort (key = getXFromRect )
34
+
35
+ # Array of accepted rects
36
+ acceptedRects = []
37
+
38
+ # Merge threshold for x coordinate distance
39
+ xThr = 5
40
+
41
+ # Iterate all initial bounding rects
42
+ for supIdx , supVal in enumerate (rects ):
43
+ if (rectsUsed [supIdx ] == False ):
44
+
45
+ # Initialize current rect
46
+ currxMin = supVal [0 ]
47
+ currxMax = supVal [0 ] + supVal [2 ]
48
+ curryMin = supVal [1 ]
49
+ curryMax = supVal [1 ] + supVal [3 ]
50
+
51
+ # This bounding rect is used
52
+ rectsUsed [supIdx ] = True
53
+
54
+ # Iterate all initial bounding rects
55
+ # starting from the next
56
+ for subIdx , subVal in enumerate (rects [(supIdx + 1 ):], start = (supIdx + 1 )):
57
+
58
+ # Initialize merge candidate
59
+ candxMin = subVal [0 ]
60
+ candxMax = subVal [0 ] + subVal [2 ]
61
+ candyMin = subVal [1 ]
62
+ candyMax = subVal [1 ] + subVal [3 ]
63
+
64
+ # Check if x distance between current rect
65
+ # and merge candidate is small enough
66
+ if (candxMin <= currxMax + xThr ):
67
+
68
+ # Reset coordinates of current rect
69
+ currxMax = candxMax
70
+ curryMin = min (curryMin , candyMin )
71
+ curryMax = max (curryMax , candyMax )
72
+
73
+ # Merge candidate (bounding rect) is used
74
+ rectsUsed [subIdx ] = True
75
+ else :
76
+ break
77
+
78
+ # No more merge candidates possible, accept current rect
79
+ acceptedRects .append ([currxMin , curryMin , currxMax - currxMin , curryMax - curryMin ])
80
+
81
+ # for rect in acceptedRects:
82
+ # print(rect[0],rect[1],rect[2],rect[3])
83
+ # img = cv2.rectangle(image, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (121, 11, 189), 2)
84
+
85
+ # cv2_imshow(image)
86
+ c = 0
87
+ c = 0
88
+ for i in range (len (acceptedRects )):
89
+ flag = 0
90
+ stpt = [acceptedRects [i ][0 ], acceptedRects [i ][1 ]]
91
+ endpt = [acceptedRects [i ][0 ]+ acceptedRects [i ][2 ], acceptedRects [i ][1 ]+ acceptedRects [i ][3 ]]
92
+ if i < len (acceptedRects )- 1 :
93
+ nstpt = [acceptedRects [i + 1 ][0 ], acceptedRects [i + 1 ][1 ]]
94
+ nendpt = [acceptedRects [i + 1 ][0 ]+ acceptedRects [i + 1 ][2 ], acceptedRects [i + 1 ][1 ]+ acceptedRects [i + 1 ][3 ]]
95
+ dis = nstpt [0 ]- endpt [0 ]
96
+ print (dis )
97
+ if dis > 100 :
98
+ temp1 = np .zeros ((6 ,9 ), dtype = int )
99
+ # cv2_imshow(temp1)
100
+ flag = 1
101
+ cropp = image [stpt [1 ]:endpt [1 ], stpt [0 ]:endpt [0 ]]
102
+ color = [255 , 255 , 255 ]
103
+ pad = 100
104
+ new_im = cv2 .copyMakeBorder (cropp , pad , pad , pad , pad , cv2 .BORDER_CONSTANT ,value = color )
105
+ c += 1
106
+ cv2 .imwrite ("./images/{}c{}.png" .format (filename ,c ), new_im )
107
+ if flag == 1 :
108
+ c += 1
109
+ cv2 .imwrite ("./images/{}c{}.png" .format (filename ,c ), temp1 )
110
+ return c
0 commit comments