-
Notifications
You must be signed in to change notification settings - Fork 1
/
findLink.py
executable file
·191 lines (164 loc) · 5.51 KB
/
findLink.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import cv2
import os
import math
import numpy as np
from setting import BINARY_PATH
def eclipseDistance(point1, point2):
return math.sqrt(math.pow(point2[0]-point1[0],2)+math.pow(point2[1]-point1[1],2)+math.pow(point2[2]-point1[2],2))
def getImgMax(imgBinary):
Q = []
result = []
size = img.shape
A = np.zeros(size, dtype=np.uint8)
B = np.zeros(size, dtype=np.uint8)
C = np.zeros(size, dtype=np.uint8)
D = np.zeros(size, dtype=np.uint8)
Q = findLinkPixel(imgBinary)
maxIndex = countMaxPixel(Q)
print(len(Q))
for item in Q[maxIndex]:
B[item] = 255
cv2.bitwise_not(B, C)
Q2 = findLinkPixel(C)
maxIndex = countMaxPixel(Q2)
for item in Q2[maxIndex]:
C[item] = 0
C1 = cv2.bitwise_or(B,C)
def getKeyPointEdge(imgBinary):
keyArr = []
kernel = np.ones((3, 3), np.uint8)
A = cv2.erode(imgBinary,kernel,iterations = 1)
imgEgde = cv2.bitwise_xor(imgBinary, A)
rows, cols = imgEgde.shape
for x in range(rows):
for y in range(cols):
if(imgEgde[x][y]==255):
keyArr.append((x,y))
return keyArr
def updateKeyPoint(maskArr, imgGray):
directionX = [-1, 0, 1,-1, 1,-1, 0, 1]
directionY = [-1,-1,-1, 0, 0, 1, 1, 1]
keyArray = getKeyPointEdge(maskArr)
tmpArr = []
while(len(keyArray)>0):
for item in keyArray:
for direc in range(8):
X = item[0]+directionX[direc]
Y = item[1]+directionY[direc]
if(X<0 or Y<0 or X>=100 or Y>=100 or (maskArr[X][Y]==255)):
continue
#if( abs(imgGray[item]-imgGray[X][Y]) < 10):
if(eclipseDistance(imgGray[X][Y], imgGray[item])<50):
maskArr[X][Y]=255
tmpArr.append((X,Y))
keyArray.clear()
keyArray.extend(tmpArr)
tmpArr.clear()
def findCheckPoints(row, colsLength):
length = len(row)
rowCheck = []
if(length == 0):
return rowCheck
head = row[0]
last = row[length-1]
if(head[0]>0):
rowCheck.append((head[0]-1,head[1]-1))
for item in row:
rowCheck.append((item[0]-1, item[1]))
if(last[0] < colsLength-1):
rowCheck.append((last[0]-1,last[1]+1))
return rowCheck
def getPoint(imgMat, inRow):
rows, columns = imgMat.shape
Q = []
Result = []
open = False
for x in range(columns):
if(imgMat[inRow][x]>200 and x!=(columns-1)):
Q.append((inRow,x))
open = True
elif((open == True) and (imgMat[inRow][x]==0)):
Result.append(Q[:])
Q.clear()
open = False
elif(x==(columns-1) and imgMat[inRow][x]>200):
Q.append((inRow,x))
Result.append(Q[:])
Q.clear()
open = False
else:
open = False
return Result
def checkRow(rowCheck, rowCluster):
for checkPoint in rowCheck:
for point in rowCluster:
if ((checkPoint[0] == point[0] and checkPoint[1] == point[1])):
return True
return False
def getRow(mat, index):
result = []
for item in mat:
if(item[0] == index):
result.append(item)
return result
def mergeValue(mat1, mat2, index):
result = []
for k in range(index):
row1 = getRow(mat1, k)
row2 = getRow(mat2, k)
result.extend(row1)
result.extend(row2)
return result
def changeQueue(matQ, checkR ,checkRowQueue):
for index, value in enumerate(checkRowQueue):
if index == 0:
continue
value = matQ[index]
matQ[checkRowQueue[0]].extend(matQ[checkRowQueue[index]])
#checkRowQueue[0] = mergeValue(checkRowQueue[0], matQ[checkRowQueue[index]])
matQ[checkRowQueue[0]].extend(checkR)
del checkRowQueue[0]
removeElement(matQ, checkRowQueue)
def findLinkPixel(img):
Q = []
rows, cols = img.shape
for rowIndex in range(rows):
points = getPoint(img, rowIndex)
if(len(points)==0 ):
continue
elif(len(Q)==0):
Q = points
continue
for checkR in points:
checkPoints = findCheckPoints(checkR, cols)
checkRowQueue = []
for indexQ, checkQ in enumerate(Q):
rowQ = getRow(checkQ, rowIndex- 1)
if(len(rowQ)==0):
continue
elif(checkRow(rowQ, checkPoints)):
checkRowQueue.append(indexQ)
if (len(checkRowQueue)==0):
Q.append(checkR)
elif(len(checkRowQueue)==1):
Q[checkRowQueue[0]].extend(checkR)
else:
changeQueue(Q, checkR, checkRowQueue)
return Q
def removeElement(matQ ,removeArray):
matQ = [points for index, points in enumerate(matQ) if index not in removeArray]
def countMaxPixel(matQ):
max = 0
maxIndex = 0
for index, item in enumerate(matQ):
if(len(item) > max):
max = len(item)
maxIndex = index
return maxIndex
def checkFuction(Q, rows, cols):
for index in range(rows):
for index1 in range(len(Q)-1):
for index2 in range(index1, len(Q)):
if (checkRow(getRow(Q, index), getRow(Q, index))):
return True
return False