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

