ShapeDetection.py
2.04 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
import cv2
import numpy as np
import Contour
##!!!!!Faire le pretraitement
#Test : Luminosite, orientation dans l'espace, distance
#Etudier les limites
def FindCircles():
image = None
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), 0)
img = cv2.medianBlur(image, 5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1 = 50, param2 = 30, minRadius = 0, maxRadius = 0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
def FindShapes():
image = None
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))
height, width, channels = image.shape
mask = np.zeros((height, width, 3), dtype = "uint8")
contours = Contour.FindContours(image)
img = image.copy()
#Contour.DrawContours(img, contours, "Contours Shape Detection")
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
if len(approx) == 3 :
print "Triangle"
cv2.drawContours(image, [cnt], 0, (255, 255, 0), -1)
elif len(approx) == 4:
print "Square"
cv2.drawContours(image, [cnt], 0, (255, 255, 255), -1)
elif len(approx) == 5:
print "Pentagon"
cv2.drawContours(image, [cnt], 0, (0, 255, 0), -1)
#elif len(approx) == 8:
# print "Huit"
# cv2.drawContours(image, [cnt], 0, (0, 0, 0), -1)
elif len(approx) > 6 and len(approx) < 9:
print "Arrow"
cv2.drawContours(image, [cnt], 0, (255, 0, 255), -1)
cv2.drawContours(mask, [cnt], 0, (255,255,255), -1)
elif len(approx) > 19:
print "Circle"
cv2.drawContours(image, [cnt], 0, (255, 0, 0), -1)
cv2.imshow('Mask', mask)
cv2.imshow('detected shapes', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return