# Path: Settings/Exceptions.py
# class DroneVisionError(Exception):
# 	def __init__(self, error_key=None):
# 		'''CONSTRUCTOR'''
# 
# 		self.__error_messages = {	'no_blobs_error_msg'								: 'No blobs detected!', \
# 									'blob_distance_is_not_calibrated_error_msg'			: 'Blob distance is not calibrated!', \
# 									'feature_descriptor_not_available_error_msg'		: 'Feature descriptor not avaiable - Use block matching!', \
# 									'find_line_limits_unexpected_angle'					: 'Unexpected angle: provide vertical or horizontal hough lines only',  \
# 									'error_msg_no_heading_angle_detected' 				: 'No heading angles detected', \
# 									'error_msg_no_backup_heading_available'				: 'No edge heading detected, and no backup heading available.', \
# 									'find_line_limits_no_hor_or_vert_found' 			: 'Only vertical or horizontal lines found - need both horizontal and vertical lines to process.', \
# 									'detect_boundary_edge_not_found_all_edge_lines'		: 'Could not compute all edges lines - need all boundary edge lines to process', \
# 									'no_3D_point_matches'								: 'Could not compute 3D points - no matching points.', \
# 									'could_not_get_point_list_from_slave'				: 'Timeout thread - could not get right point list from slave within timeout!', \
# 									'triangulation_error_msg'							: 'Triangulation failed!', \
# 									'3D_point_filtration_failed'						: 'Filtration of 3D points failed!'}
# 
# 		self.msg = None
# 		if error_key != None:
# 			self.msg = self.__error_messages[error_key]
# 
# 	def __str__(self):
# 		return repr(self.msg)
# 
# 	def GetErrorMessage(self, error_key):
# 		'''
# 		 @brief Return all possible error messages
# 		'''
# 		return self.__error_messages[error_key]
# 
# Path: src/DroneVision/DroneVision_src/imgProcessing/frameTools/frameTools.py
# def GetShape(frame):
# 	'''
# 	 @brief Get shape of frame, regardless of depth dimension.
# 
# 	 @param frame 
# 	 
# 	 return (height, width)
# 	'''
# 	return frame.shape[:2]
# 
# def CheckColor(frame):
# 	'''
# 	 @brief Check wether the frame is of grayscale or color.
# 	 	converts the frame to color if necessary.
# 
# 	 @param frame
# 	 
# 	 @return color frame.
# 	'''
# 	shape = frame.shape
# 	if len(shape) == 2:
# 		cl_frame = ToColor(frame)
# 	else:
# 		cl_frame = frame
# 	return cl_frame


import cv2
import numpy as np
from Settings.Exceptions import DroneVisionError
from src.DroneVision.DroneVision_src.imgProcessing.frameTools.frameTools import GetShape, CheckColor
'''
 Author: Hans Erik Heggem
 Email: hans.erik.heggem@gmail.com
 Project: Master's Thesis - Autonomous Inspection Of Wind Blades
 Repository: Master's Thesis - CV (Computer Vision
'''


def DetectCorners(frame, keypoints=[], kernel_size=5, kernel_step_size=1, k=0.04, thresh=1000):
	'''
	 @brief Detect corners using the Harris corner method.
	 	Inspired by: https://github.com/hughesj919/HarrisCorner/blob/master/Corners.py

	 @param frame (grayscale)
	 @param keypoints (list of cv2 keypoints. The frame will be reset if any keypoints are given. (default=empty list))
	 @param kernel_size (It is the size of neighbourhood considered for corner detection (default=5))
	 @param kernel_step_size (default=1)
	 @param k (Harris corner constant - usually between 0.04 - 0.06 (default=0.04))
	 @param thresh (Response threshold (default=1000))

	 @return corners (list of corners given as cv2 keypoints. Get position of keypoint as kp.pt (x,y position) and response of corner as kp.response)
	'''
	corners 			= []
	offset 				= int(kernel_size//2)
	kernel_step_size 	= int(round(kernel_step_size))

	len_kp = len(keypoints)
	if len_kp > 0: # Find corners from keypoints.

# Path: Settings/Exceptions.py
# class DroneVisionError(Exception):
# 	def __init__(self, error_key=None):
# 		'''CONSTRUCTOR'''
# 
# 		self.__error_messages = {	'no_blobs_error_msg'								: 'No blobs detected!', \
# 									'blob_distance_is_not_calibrated_error_msg'			: 'Blob distance is not calibrated!', \
# 									'feature_descriptor_not_available_error_msg'		: 'Feature descriptor not avaiable - Use block matching!', \
# 									'find_line_limits_unexpected_angle'					: 'Unexpected angle: provide vertical or horizontal hough lines only',  \
# 									'error_msg_no_heading_angle_detected' 				: 'No heading angles detected', \
# 									'error_msg_no_backup_heading_available'				: 'No edge heading detected, and no backup heading available.', \
# 									'find_line_limits_no_hor_or_vert_found' 			: 'Only vertical or horizontal lines found - need both horizontal and vertical lines to process.', \
# 									'detect_boundary_edge_not_found_all_edge_lines'		: 'Could not compute all edges lines - need all boundary edge lines to process', \
# 									'no_3D_point_matches'								: 'Could not compute 3D points - no matching points.', \
# 									'could_not_get_point_list_from_slave'				: 'Timeout thread - could not get right point list from slave within timeout!', \
# 									'triangulation_error_msg'							: 'Triangulation failed!', \
# 									'3D_point_filtration_failed'						: 'Filtration of 3D points failed!'}
# 
# 		self.msg = None
# 		if error_key != None:
# 			self.msg = self.__error_messages[error_key]
# 
# 	def __str__(self):
# 		return repr(self.msg)
# 
# 	def GetErrorMessage(self, error_key):
# 		'''
# 		 @brief Return all possible error messages
# 		'''
# 		return self.__error_messages[error_key]
# 
# Path: src/DroneVision/DroneVision_src/imgProcessing/frameTools/frameTools.py
# def GetShape(frame):
# 	'''
# 	 @brief Get shape of frame, regardless of depth dimension.
# 
# 	 @param frame 
# 	 
# 	 return (height, width)
# 	'''
# 	return frame.shape[:2]
# 
# def CheckColor(frame):
# 	'''
# 	 @brief Check wether the frame is of grayscale or color.
# 	 	converts the frame to color if necessary.
# 
# 	 @param frame
# 	 
# 	 @return color frame.
# 	'''
# 	shape = frame.shape
# 	if len(shape) == 2:
# 		cl_frame = ToColor(frame)
# 	else:
# 		cl_frame = frame
# 	return cl_frame


# Path: src/DroneVision/DroneVision_src/imgProcessing/featureDetection/generalDetectors/detectCorners.py
import cv2
import numpy as np
from Settings.Exceptions import DroneVisionError
from src.DroneVision.DroneVision_src.imgProcessing.frameTools.frameTools import GetShape, CheckColor

'''
 Author: Hans Erik Heggem
 Email: hans.erik.heggem@gmail.com
 Project: Master's Thesis - Autonomous Inspection Of Wind Blades
 Repository: Master's Thesis - CV (Computer Vision
'''


def DetectCorners(frame, keypoints=[], kernel_size=5, kernel_step_size=1, k=0.04, thresh=1000):
	'''
	 @brief Detect corners using the Harris corner method.
	 	Inspired by: https://github.com/hughesj919/HarrisCorner/blob/master/Corners.py

	 @param frame (grayscale)
	 @param keypoints (list of cv2 keypoints. The frame will be reset if any keypoints are given. (default=empty list))
	 @param kernel_size (It is the size of neighbourhood considered for corner detection (default=5))
	 @param kernel_step_size (default=1)
	 @param k (Harris corner constant - usually between 0.04 - 0.06 (default=0.04))
	 @param thresh (Response threshold (default=1000))

	 @return corners (list of corners given as cv2 keypoints. Get position of keypoint as kp.pt (x,y position) and response of corner as kp.response)
	'''
	corners 			= []
	offset 				= int(kernel_size//2)
	kernel_step_size 	= int(round(kernel_step_size))

	len_kp = len(keypoints)
	if len_kp > 0: # Find corners from keypoints.

		corners_frame = np.zeros(GetShape(frame), dtype=np.uint8)