import cv2
import numpy as np

CONTRAST_VALUE = 0.930
SATURATION_VALUE = 1.410
THRESHOLD_VALUE = 101
REF_SIZE = 64  # Velkost referencneho obrazka pre porovnanie (64x64 px)

letter_Omega = cv2.imread("ReferenceData/Images/Image_GreekOmega.jpg")
letter_Phi = cv2.imread("ReferenceData/Images/Image_GreekPhi.jpg")
letter_Psi = cv2.imread("ReferenceData/Images/Image_GreekPsi.jpg")


def _proc(img, a, s):
	hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
	hsv[:, :, 1] = np.clip(hsv[:, :, 1] * float(s), 0, 255)
	bgr = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
	gray = cv2.cvtColor(cv2.convertScaleAbs(bgr, alpha=float(a), beta=0), cv2.COLOR_BGR2GRAY)
	return gray


letter_Omega_gray = _proc(letter_Omega, CONTRAST_VALUE, SATURATION_VALUE)
letter_Phi_gray = _proc(letter_Phi, CONTRAST_VALUE, SATURATION_VALUE)
letter_Psi_gray = _proc(letter_Psi, CONTRAST_VALUE, SATURATION_VALUE)

_, letter_Omega_threshold = cv2.threshold(letter_Omega_gray, THRESHOLD_VALUE, 255, cv2.THRESH_BINARY_INV)
_, letter_Phi_threshold = cv2.threshold(letter_Phi_gray, THRESHOLD_VALUE, 255, cv2.THRESH_BINARY_INV)
_, letter_Psi_threshold = cv2.threshold(letter_Psi_gray, THRESHOLD_VALUE, 255, cv2.THRESH_BINARY_INV)

letter_Omega_contours, _ = cv2.findContours(letter_Omega_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
letter_Phi_contours, _ = cv2.findContours(letter_Phi_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
letter_Psi_contours, _ = cv2.findContours(letter_Psi_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Letter_Omega_biggest = max(letter_Omega_contours, key=cv2.contourArea)
Letter_Phi_biggest = max(letter_Phi_contours, key=cv2.contourArea)
Letter_Psi_biggest = max(letter_Psi_contours, key=cv2.contourArea)

h_omega, w_omega = letter_Omega_threshold.shape
x, y, w, h = cv2.boundingRect(Letter_Omega_biggest)
pad_x = max(1, int(w * 0.1))
pad_y = max(1, int(h * 0.1))
x1 = max(0, x - pad_x); y1 = max(0, y - pad_y)
x2 = min(w_omega, x + w + pad_x); y2 = min(h_omega, y + h + pad_y)
Letter_Omega_img = cv2.resize(letter_Omega_threshold[y1:y2, x1:x2], (REF_SIZE, REF_SIZE))

h_phi, w_phi = letter_Phi_threshold.shape
x, y, w, h = cv2.boundingRect(Letter_Phi_biggest)
pad_x = max(1, int(w * 0.1))
pad_y = max(1, int(h * 0.1))
x1 = max(0, x - pad_x); y1 = max(0, y - pad_y)
x2 = min(w_phi, x + w + pad_x); y2 = min(h_phi, y + h + pad_y)
Letter_Phi_img = cv2.resize(letter_Phi_threshold[y1:y2, x1:x2], (REF_SIZE, REF_SIZE))

h_psi, w_psi = letter_Psi_threshold.shape
x, y, w, h = cv2.boundingRect(Letter_Psi_biggest)
pad_x = max(1, int(w * 0.1))
pad_y = max(1, int(h * 0.1))
x1 = max(0, x - pad_x); y1 = max(0, y - pad_y)
x2 = min(w_psi, x + w + pad_x); y2 = min(h_psi, y + h + pad_y)
Letter_Psi_img = cv2.resize(letter_Psi_threshold[y1:y2, x1:x2], (REF_SIZE, REF_SIZE))

np.save("ReferenceData/Contours/Image_Omega_Contour.npy", Letter_Omega_img)
np.save("ReferenceData/Contours/Image_Phi_Contour.npy", Letter_Phi_img)
np.save("ReferenceData/Contours/Image_Psi_Contour.npy", Letter_Psi_img)
print("Ulozene referencne obrazky pismen Omega, Phi, Psi do ReferenceData/Contours/")