Blame view

Python/Application/Skeleton2.py 1.95 KB
3f217332   Justine   mise à jour du git
1
  import sys
81de032f   Justine   Ajout de l'applic...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  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)
3f217332   Justine   mise à jour du git
26
27
28
29
  		cv2.dilate(eroded, kernel, temp)
  		cv2.subtract(thresh, temp, temp)
  		cv2.bitwise_or(skeleton, temp, skeleton)
  		thresh, eroded = eroded, thresh 
81de032f   Justine   Ajout de l'applic...
30
  
3f217332   Justine   mise à jour du git
31
  		if cv2.countNonZero(thresh) == 0:
81de032f   Justine   Ajout de l'applic...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  			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):
3f217332   Justine   mise à jour du git
46
47
48
49
  		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'))
81de032f   Justine   Ajout de l'applic...
50
51
52
53
  		if not image:
  			image = 'FondNoir.png'
  		image = cv2.imread(str(image),0)
  	print('\t1. Rectangle\n\t2. Ellipse\n\t3. Cross\n')
3f217332   Justine   mise à jour du git
54
55
56
57
58
  	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')
81de032f   Justine   Ajout de l'applic...
59
60
61
  		if not choice:
  			choice = '1'
  	while int(size) % 2 == 0:
3f217332   Justine   mise à jour du git
62
63
64
65
  		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')
81de032f   Justine   Ajout de l'applic...
66
67
68
69
70
71
72
73
  		if not size:
  			size = '3'
  	Skeletization(image, choice, size)
  
  	cv2.waitKey(0)
  	cv2.destroyAllWindows()
  	
  	return