ShapeDetection.py 2.04 KB
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