import glob
import cv2 as cv
import numpy as np
from time import time 
from matplotlib import pyplot as plt
 
# load test image
def load_images():
	my_path = glob.glob("sign_photos/slunce/prikazany_smer/*.jpg")
	images = []
	for img in my_path:
		img_read = cv.imread(img)
		images.append(img_read)
	return(images)

# edges detection	
def edge_detection(image, filter_method, sigma, threshold_value):	
	image_2 = image.copy()
	# filter image with different methods
	if filter_method == 'blur':
		filtered_image = cv.blur(image, (3, 3))
	elif filter_method == 'GaussianBlur':
		filtered_image = cv.GaussianBlur(image, (3, 3), 0)
	elif filter_method == 'medianBlur':
		filtered_image = cv.medianBlur(image, 3)
	elif filter_method == 'bilateralFilter':
		filtered_image = cv.bilateralFilter(image, 3, 175, 175)
	else:
		filtered_image = image.copy()
	
	# get threshold values
	if threshold_value == 'median':
		value = np.median(image)
	elif threshold_value == 'mean':
		value = np.mean(image)
	else:
		value = eval(threshold_value)
	print(value, sigma)
	
	# lower and upper threshold values
	threshold = auto_canny(sigma, value)
	
	# edge detection							   lower_value	 upper_value
	edge_detected_image = cv.Canny(filtered_image, threshold[0], threshold[1])
	
	# find edge contours
	_, contours, _= cv.findContours(edge_detected_image, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
	
	for contour in contours:
		# ignore too small and large area of contours
		h, w, _ = image.shape
		image_area = w*h
		contour_area = cv.contourArea(contour)
		
		if (contour_area < image_area * 0.0005) | (contour_area > image_area * 0.02):
			continue
		
		# find possitions of detected contours
		x, y, w, h = cv.boundingRect(contour)
		
		# draw ractangle around detected object
		cv.rectangle(image, (x, y), (x+w, y+h), (0,255,255), 5)	# zluta
	
	return(edge_detected_image, image)

# auto Canny threshold values
def auto_canny(sigma, value):

	# lower and upper threshold values
	lower = int(max(0, (1.0 - sigma) * value))
	upper = int(min(255, (1.0 + sigma) * value))
	
	return(lower, upper)
	
# convert color to print
def convert_color(image):
	RGB_detected_signs = cv.cvtColor(image, cv.COLOR_BGR2RGB)
	return(RGB_detected_signs)

# print results
def print_images(edge_detected_image, RGB_detected_signs, filter_method):
	plt.figure('Edge based detection')
	plt.subplot(121),plt.imshow(edge_detected_image, cmap='gray')
	plt.title('Detekce hran'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(RGB_detected_signs)
	plt.title('Detected signs'), plt.xticks([]), plt.yticks([])
	plt.suptitle(filter_method)
	plt.show() 

# filter methods
filter_methods = ['without_filter', 'blur', 'GaussianBlur', 'medianBlur', 'bilateralFilter']#   
threshold_values = ['15', '30', '60', '120', '180', 'median', 'mean']  #   

# run main program
# filter method
for filter_method in filter_methods:
	print(filter_method)
	# threshold value
	for threshold_value in threshold_values:
		# sigma value
		sigma = 0
		for step in range(0, 5):
			sigma = sigma + 0.1			
			# run testing images
			images = load_images()
			for image in images:	
				begin = time()			
				edge_detected_image, image = edge_detection(image, filter_method, sigma, threshold_value)
				# print time of detection
				print("čas detekce = ", (time() - begin) * 1000, "[ms]")
				# print results
				RGB_detected_signs = convert_color(image)
				print_images(edge_detected_image, RGB_detected_signs, filter_method)
	
	









			
