#!/usr/bin/env python

import cv2
import numpy as np
import sys
from PIL import Image
from matplotlib import pyplot as plt


def Histogram(image):
	color = ('b', 'g', 'r')
	for i,col in enumerate(color):
		histr = cv2.calcHist([image], [i], None, [256], [0,256])
		plt.plot(histr,color = col)
		plt.xlim([0,256])
	plt.show()

def Corners(image):
	img = image.copy()
	gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	gray = np.float32(gray)
	dst = cv2.cornerHarris(gray, 5, 5, 0.15)
	dst = cv2.dilate(dst, None)
	img[dst>0.01*dst.max()] = [0,0,255]
	cv2.imshow('dst', img)
	return dst

def Edges(image):
	img = image.copy()
	edges = cv2.Canny(img, 100, 200)
	plt.subplot(121),plt.imshow(img,cmap='gray')
	plt.title('Original Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(edges,cmap = 'gray')
	plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
	plt.show()
	return edges

def Components(image):	
	b = image.copy()
	# set green and red channels to 0
	b[:, :, 1] = 0
	b[:, :, 2] = 0

	g = image.copy()	
	# set blue and red channels to 0
	g[:, :, 0] = 0
	g[:, :, 2] = 0

	r = image.copy()
	# set blue and green channels to 0
	r[:, :, 0] = 0
	r[:, :, 1] = 0
	
	return b,g,r

#paper = image.copy()
#ret, thresh = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY), 100, 255, cv2.THRESH_BINARY)
#image2, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#for c in contours:
#	rect = cv2.minAreaRect(c)
#	box = cv2.boxPoints(rect)
#	box = np.int0(box)
#	cv2.drawContours(paper, [box], 0, (0, 255, 0),1)
#cv2.imshow('paper', paper)

#image = cv2.imread('couleur.png')
image = cv2.imread('couleur.png')
#edges = Edges(image)
#cv2.imshow('image', image)
#Histogram(image)
dst = Corners(image)
#Histogram(image)
#r, g, b = Components(image)
#test = image[100, 200]
#print(test)
b,g,r = cv2.split(image)
#cv2.imwrite('RedDrone.png', r)
#cv2.imwrite('BlueDrone.png', b)
#cv2.imwrite('GreenDrone.png', g)

cv2.waitKey(0)



# RGB - Blue
#cv2.imshow('B-RGB',b,  cmap = 'gray')
#cv2.imwrite("flecheBleue.png", b)

# RGB - Green
#cv2.imshow('G-RGB', g)
#cv2.imwrite("flecheVerte.png", g)

# RGB - Red
#cv2.imshow('R-RGB', r)
#cv2.imwrite("flecheRouge.png", r)

#cv2.waitKey(0)
