#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
###############################################################################
#
#  Name:         opencv_basic_operations_3
#  Purpose:      Basic operations on images with OpenCV
#  Author:       weigu.lu
#  Date:         2020-11-26
#
#  Copyright 2020 weigu <weigu@weigu.lu>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#  More infos on weigu.lu/other_projects
#
###############################################################################
"""

import cv2 as cv # to run code even if version changes
import numpy as np
import math

IMG_NAME = 'test3.jpg'

THRESH = 65
MAX_VALUE = 255

def show_image(pimg, show_flag_time):
    ''' Show image during x ms if flag is set. Parameter show_flag_time is a
        tuple e.g (1,2000) to show picture for 2s or (0,2000) to prevent the
        show. (1,0) waits on keypress '''
    if show_flag_time[0] == 1:
        cv.imshow('image', pimg)      # cv.imshow(window_name, image)
        cv.waitKey(show_flag_time[1]) # show picture for x ms (x=0 for keypress)
        cv.destroyAllWindows()

def get_red(pimg):
    '''filter the red channel'''
    img_hsv = cv.cvtColor(pimg, cv.COLOR_BGR2HSV)
    red_min = np.array([0, 60, 0])
    red_max = np.array([10, 255, 255])
    mask = cv.inRange(img_hsv, red_min, red_max)
    return mask     #return inverted image

# flags and times in ms to show images
flag = {'short':(1, 500), 'medium':(1, 1000), 'long':(1, 3000), 'key':(1,0)}

img = cv.imread(IMG_NAME)                        # read the image
img_grey = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
show_image(img_grey, flag['short'])
cv.imwrite('hough_lines_grey.png', img_grey)
img_edges = cv.Canny(img_grey,50,150,apertureSize = 3)
show_image(img_edges, flag['short'])
cv.imwrite('hough_lines_edges.png', img_edges)
lines = cv.HoughLinesP(image=img_edges, rho=3, theta=np.pi/180,
                      threshold=100, minLineLength=200, 
                      maxLineGap=30)
if lines is not None:
    for line in lines: # create image with lines        
        mx1, my1, mx2, my2 = line[0]
        cv.line(img, (mx1, my1), (mx2, my2), (0, 255, 255), 2)        
show_image(img, flag['medium'])

img = cv.imread(IMG_NAME)                        # read the image
img2 = img.copy()
mask = get_red(img)
print(mask.shape)
lines = cv.HoughLinesP(image=mask, rho=3, theta=np.pi/180,
                      threshold=100, minLineLength=100, 
                      maxLineGap=30)
print(lines)
counter, mx1a, mx2a, my1a, my2a = 0, 0, 0, 0, 0
if lines is not None:
    for line in lines: # create image with lines        
        mx1, my1, mx2, my2 = line[0]
        cv.line(img2, (mx1, my1), (mx2, my2), (0, 255, 255), 2)        
        mx1a += mx1
        mx2a += mx2
        my1a += my1
        my2a += my2
        counter += 1
mx1a = mx1a // counter
mx2a = mx2a // counter
my1a = my1a // counter
my2a = my2a // counter
cv.line(img, (mx1a, my1a), (mx2a, my2a), (0, 255, 255), 2)        
show_image(img2, flag['medium'])
#cv.imwrite('hough_lines.png', img2)
show_image(img, flag['key'])
#cv.imwrite('hough_line.png', img)


