-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlabel_connected.py
78 lines (68 loc) · 2.83 KB
/
label_connected.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
import numpy as np
import pandas as pd
import os
import cv2
from matplotlib import pyplot as plt
print cv2.__version__
def get_connected_components(bin_image, connectivity = 4):
'''
Method: One component at a time connected component labeling
Input: Binary Image (h x w array of 0 and non-zero values, will created connected components of non-zero)
Returns: connected_array -(h x w array where each connected component is labeled with a unique integer (1:counter-1)
counter-1 - integer, number of unique connected components
'''
h,w = bin_image.shape
yc, xc = np.where(bin_image!=0)
queue = []
connected_array = np.zeros((h,w)) #labeling array
counter = 1
for elem in range(len(xc)):
# iterate over all nonzero elements
i = yc[elem]
j = xc[elem]
if connected_array[i,j] == 0:
# not labeled yet proceed
connected_array[i,j] = counter
queue.append((i,j))
while len(queue) != 0:
# work through queue
current = queue.pop(0)
i,j = current
if i == 0 and j == 0:
coords = np.array([[i, i+1], [j+1, j]])
elif i == h - 1 and j == w - 1:
coords = np.array([[i, i-1], [j-1, j]])
elif i == 0 and j == w - 1:
coords = np.array([[i, i+1], [j-1, j]])
elif i == h - 1 and j == 0:
coords = np.array([[i, i-1], [j+1, j]])
elif i == 0:
coords = np.array([[i, i, i+1],[j-1, j+1, j]])
elif i == h - 1:
coords = np.array([[i, i, i-1],[j-1, j+1, j]])
elif j == 0:
coords = np.array([[i, i+1, i-1],[j+1, j, j]])
elif j == w - 1:
coords = np.array([[i, i+1, i-1],[j-1, j, j]])
else:
coords = np.array([[i, i, i+1, i-1],[j-1, j+1, j, j]])
for k in range(len(coords[0])):
# iterate over neighbor pixels, if not labeled and not zero then assign current label
if connected_array[coords[0,k], coords[1,k]] == 0 and bin_image[coords[0,k], coords[1,k]] != 0:
connected_array[coords[0,k], coords[1,k]] = counter
queue.append((coords[0,k], coords[1,k]))
counter += 1
return connected_array, counter-1
def test():
test_arr = np.zeros((8,8))
test_arr[1:3,1:3] = 1
test_arr[3:5,3:5] = 1
test_arr[6,1] = 1
test_arr[6,5:7] = 1
test_arr[7,3:5] = 1
test_arr[-1,0] = 1
print test_arr
connected_arr, count = get_connected_components(test_arr)
print connected_arr
if __name__ == "__main__":
test()