Skeleton2.py
1.95 KB
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
import sys
import cv2
import numpy as np
def Skeletization(img, shape, size):
if shape == '1':
shape = cv2.MORPH_RECT
elif shape == '2':
shape = cv2.MORPH_ELLIPSE
elif shape == '3':
shape = cv2.MORPH_CROSS
else:
return
cv2.normalize(img, img, 0, 255, cv2.NORM_MINMAX)
skeleton = np.zeros(img.shape, np.uint8)
eroded = np.zeros(img.shape, np.uint8)
temp = np.zeros(img.shape, np.uint8)
_,thresh = cv2.threshold(img, 127, 255, 0)
kernel = cv2.getStructuringElement(shape,(int(size), int(size)))
while(True):
cv2.erode(thresh, kernel, eroded)
cv2.dilate(eroded, kernel, temp)
cv2.subtract(thresh, temp, temp)
cv2.bitwise_or(skeleton, temp, skeleton)
thresh, eroded = eroded, thresh
if cv2.countNonZero(thresh) == 0:
break
kernel = np.ones((20,20), np.uint8)
skeleton = cv2.morphologyEx(skeleton, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('DefaultSkeleton.png', skeleton)
cv2.imshow('skeleton', skeleton)
return skeleton
def KernelChoice():
image = None
size = '0'
choice = '0'
print('\t\tSkeleton Menu\n')
while (image is None):
if sys.version_info >= (3, 0):
image = str(input('\tImage to use ? By default couleur.png \n'))
else:
image = str(raw_input('\tImage to use ? By default couleur.png \n'))
if not image:
image = 'FondNoir.png'
image = cv2.imread(str(image),0)
print('\t1. Rectangle\n\t2. Ellipse\n\t3. Cross\n')
while choice < '1' or choice > '3':
if sys.version_info >= (3, 0):
choice = input('\n\tKernel Choice. Rectangle by Default\n')
else :
choice = raw_input('\n\tKernel Choice. Rectangle by Default\n')
if not choice:
choice = '1'
while int(size) % 2 == 0:
if sys.version_info >= (3, 0):
size = input('\n\tPlease specify the kernel size (Odd number). By default it\'s 3\n')
else :
size = raw_input('\n\tPlease specify the kernel size (Odd number). By default it\'s 3\n')
if not size:
size = '3'
Skeletization(image, choice, size)
cv2.waitKey(0)
cv2.destroyAllWindows()
return