Blame view

Python/Application/Contour.py 3.27 KB
81de032f   Justine   Ajout de l'applic...
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
  import cv2
  import numpy as np
  import numpy as np
  import cv2
  
  def FindContours(img):
  	imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  	#cv2.imshow("Gray", imgray)
  	ret,thresh = cv2.threshold(imgray,127,255,0)
  	#cv2.imshow("threshold", thresh)
  	_,contours,hierarchy = cv2.findContours(thresh, 1, 2)
  	return contours
  
  def FindCenterofMass(contours):
  	liste = []	
  	area = []
  	centerMass = []
  	for i in range (0,len(contours)):
  		cnt = contours[i]
  		#print cnt
  		M = cv2.moments(cnt)
  		area.append(cv2.contourArea(cnt))
  		tmp = cv2.contourArea(cnt)
  		if (M['m00'] != 0) and (tmp > 1000):
  			cx = int(M['m10']/M['m00'])
  			cy = int(M['m01']/M['m00'])
  			centerMass.append((cx,cy))
  		else:
  			liste.append(i)
  	return 	liste, area, centerMass
  
  
  
  def CleanContours(img):
  	contours = FindContours(img)
  	liste, _, _ = FindCenterofMass(contours)
  	for j in range (0,len(liste)):
  		a = liste[j]
  		if (a < len(contours)): 
  			contours.pop(a)
  	return contours
  
  def DrawBoundingBox(img):
  	contours = FindContours(img)
  	for cnt in contours:
  		x,y,w,h = cv2.boundingRect(cnt)
  		img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
  	cv2.imshow('Bounding Box', img)
  	return img
  
  def DrawEnclosingCircle(img):
  	contours = FindContours(img)
  	for cnt in contours:
  		(x,y),radius = cv2.minEnclosingCircle(cnt)
  		center = (int(x),int(y))
  		radius = int(radius)
  		img = cv2.circle(img,center,radius,(0,255,0),2)
  	cv2.imshow('Enclosing Circle', img)
  	return img
  
  def DrawContours(img, contours, titre):
  	img = cv2.drawContours(img, contours, -1, (0,255,0), 3)
  	cv2.imshow(titre, img)
  	return img
  
  def DrawCenterofMass(img):
  	contours = FindContours(img)
  	_, _, centerMass = FindCenterofMass(contours)
  	for x, y in centerMass:
  		img=cv2.circle(img, (x,y), 10, (124,255,255), 1)
  	cv2.imshow('Center of Mass', img)
  	return img
  
  def DrawArea(img):
  	contours = CleanContours(img)
  	img = cv2.drawContours(img, contours, -1, (255, 0, 0), cv2.FILLED)
  	cv2.imshow('Area', img)
  	return img	
  
  def ContourChoiceProcessing(img, choice):
  	if choice == '1':
  		contours = FindContours(img)
  		img = DrawContours(img, contours, 'Contours')
  		return img
  	elif choice == '2':
  		contours = CleanContours(img)
  		img = DrawContours(img, contours, 'Clean Contours')
  		return img
  	elif choice == '3':
  		img = DrawCenterofMass(img)
  		return img
  	elif choice == '4':
  		img = DrawArea(img)
  		return img
  	elif choice == '5':
  		img = ContourChoiceProcessing(img, '2')
  		img = DrawCenterofMass(img)
  		return img
  	elif choice == '6':
  		img = DrawArea(img)
  		img = DrawCenterofMass(img)
  		return img
  	elif choice == '7':
  		img = DrawBoundingBox(img)
  		return img 
  	elif choice == '8':
  		img = DrawEnclosingCircle(img)
  		return img
  	else:
  		return
  
  def ContourChoice():
  	image = None
  	img = None
  	print('\t\tContours Features Menu\n')
  	while (image is None):
  		image = str(raw_input('\tImage to use? By default couleur.png \n'))
  		if not image:
  			image = 'couleur.png'
  		image = cv2.imread(str(image))
  	print ('\t1. Contours\n\t2. Clean Contours\n\t3. Center of Mass\n\t4. Area\n\t5. Contours + Center of Mass\n\t6. Area + Center of Mass\n\t7. Bounding Box\n\t8. Enclosing Circle\n')
  	choix = raw_input('\n\tMultiple choices possible\n')
  	for i in range (0, len(choix)):
  		img = image.copy()
  		img = ContourChoiceProcessing(img, choix[i])
  	if img is not None:
  		cv2.waitKey(0)
  		cv2.destroyAllWindows()
  	return