-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskelet.py
48 lines (41 loc) · 1.46 KB
/
skelet.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
import cv2 as cv
import numpy as np
def skeletonize(image):
goal_point = [300, 500]
img = image
wh_pixels_up = []
wh_pixels_left = []
wh_pixels_right = []
thresh_erode = img.copy()
skel = np.zeros(img.shape, np.uint8)
element = cv.getStructuringElement(cv.MORPH_CROSS, (3, 3))
while True:
open = cv.morphologyEx(img, cv.MORPH_OPEN, element)
temp = cv.subtract(img, open)
eroded = cv.erode(img, element)
skel = cv.bitwise_or(skel, temp)
img = eroded.copy()
if cv.countNonZero(img) == 0:
break
thresh_erode = cv.erode(thresh_erode, np.ones((7, 7), dtype=np.uint8), iterations=20)
out = cv.bitwise_and(skel, thresh_erode)
for row in range(300, 601):
if out[row][350] != 0:
wh_pixels_left.append((row, 350))
for row in range(300, 601):
if out[row][650] != 0:
wh_pixels_right.append((row, 650))
for col in range(350, 651):
if out[300][col] != 0:
wh_pixels_up.append((300, col))
# find goal point
if len(wh_pixels_up) > 0 and len(wh_pixels_left) == 0 and len(wh_pixels_right) == 0:
goal_point = wh_pixels_up[0]
if len(wh_pixels_right) > 0:
goal_point = wh_pixels_right[0]
if len(wh_pixels_left) > 0:
goal_point = wh_pixels_left[0]
wh_pixels_left.clear()
wh_pixels_right.clear()
wh_pixels_up.clear()
return goal_point