import os
from logging import getLogger
from functools import partial
import numpy as np
import cv2
from scipy.signal import find_peaks
from scipy.ndimage import gaussian_filter1d
from scipy.stats import linregress
from ocrd_utils import (
    shift_coordinates,
    rotate_coordinates,
    transform_coordinates,
)
from .rotate import (
    rotate_image,
    rotate_image_enlarge,
)
from .resize import resize_image
from .contour import (
    return_parent_contours,
    filter_contours_area_of_image_tables,
    return_contours_of_image,
    filter_contours_area_of_image,
    return_contours_of_interested_textline,
    find_center_of_contours,
    find_contours_mean_y_diff,
)
from . import (
    find_num_col_deskew,
    box2rect,
)

def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
    (h, w) = img_patch.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, -thetha, 1.0)
    x_d = M[0, 2]
    y_d = M[1, 2]

    thetha = thetha / 180.0 * np.pi
    rotation_matrix = np.array([[np.cos(thetha), -np.sin(thetha)], [np.sin(thetha), np.cos(thetha)]])

    x_cont = contour_text_interest[:, 0, 0]
    y_cont = contour_text_interest[:, 0, 1]
    x_cont = x_cont - np.min(x_cont)
    y_cont = y_cont - np.min(y_cont)

    y_min_cont, x_min_cont = 0, 0
    y_max_cont, x_max_cont = img_patch.shape

    xv = np.linspace(x_min_cont, x_max_cont, 1000)
    textline_patch_sum_along_width = img_patch.sum(axis=axis)
    first_nonzero = 0  # (next((i for i, x in enumerate(mada_n) if x), None))

    y = textline_patch_sum_along_width[:]  # [first_nonzero:last_nonzero]
    y_padded = np.zeros(len(y) + 40)
    y_padded[20 : len(y) + 20] = y
    x = np.array(range(len(y)))

    peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
    if 1 > 0:
        try:
            y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
            y_padded_up_to_down_e = -y_padded + np.max(y_padded)
            y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
            y_padded_up_to_down_padded_e[20 : len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
            y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)

            peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
            peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
            neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])

            arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
                y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
            diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)

            arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
            arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]

            peaks_new = peaks_e[:]
            peaks_neg_new = peaks_neg_e[:]

            clusters_to_be_deleted = []
            if len(arg_diff_cluster) > 0:
                clusters_to_be_deleted.append(
                    arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
                for i in range(len(arg_diff_cluster) - 1):
                    clusters_to_be_deleted.append(
                        arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
                                                arg_diff_cluster[i + 1] + 1])
                clusters_to_be_deleted.append(
                    arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
            if len(clusters_to_be_deleted) > 0:
                peaks_new_extra = []
                for m in range(len(clusters_to_be_deleted)):
                    min_cluster = np.min(peaks_e[clusters_to_be_deleted[m]])
                    max_cluster = np.max(peaks_e[clusters_to_be_deleted[m]])
                    peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
                    for m1 in range(len(clusters_to_be_deleted[m])):
                        peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1] - 1]]
                        peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1]]]
                        peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg_e[clusters_to_be_deleted[m][m1]]]
                peaks_new_tot = []
                for i1 in peaks_new:
                    peaks_new_tot.append(i1)
                for i1 in peaks_new_extra:
                    peaks_new_tot.append(i1)
                peaks_new_tot = np.sort(peaks_new_tot)

            else:
                peaks_new_tot = peaks_e[:]

            textline_con, hierarchy = return_contours_of_image(img_patch)
            textline_con_fil = filter_contours_area_of_image(img_patch,
                                                             textline_con, hierarchy,
                                                             max_area=1, min_area=0.0008)
            if len(np.diff(peaks_new_tot))>1:
                y_diff_mean = np.mean(np.diff(peaks_new_tot))  # self.find_contours_mean_y_diff(textline_con_fil)
                sigma_gaus = int(y_diff_mean * (7.0 / 40.0))
            else:
                sigma_gaus = 12
        except:
            sigma_gaus = 12
        if sigma_gaus < 3:
            sigma_gaus = 3

    y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
    y_padded_up_to_down = -y_padded + np.max(y_padded)
    y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
    y_padded_up_to_down_padded[20 : len(y_padded_up_to_down) + 20] = y_padded_up_to_down
    y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)

    peaks, _ = find_peaks(y_padded_smoothed, height=0)
    peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)

    return (x, y,
            x_d, y_d,
            xv,
            x_min_cont, y_min_cont,
            x_max_cont, y_max_cont,
            first_nonzero,
            y_padded_up_to_down_padded,
            y_padded_smoothed,
            peaks, peaks_neg,
            rotation_matrix)

def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
    h, w = img_patch.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, -thetha, 1.0)
    x_d = M[0, 2]
    y_d = M[1, 2]
    rotation_matrix = M[:2, :2]
    contour_text_interest_copy = contour_text_interest.copy()

    x_cont = contour_text_interest[:, 0, 0]
    y_cont = contour_text_interest[:, 0, 1]
    x_cont = x_cont - np.min(x_cont)
    y_cont = y_cont - np.min(y_cont)

    x_min_cont = 0
    x_max_cont = img_patch.shape[1]
    y_min_cont = 0
    y_max_cont = img_patch.shape[0]

    xv = np.linspace(x_min_cont, x_max_cont, 1000)
    textline_patch_sum_along_width = img_patch.sum(axis=1)
    first_nonzero = 0  # (next((i for i, x in enumerate(mada_n) if x), None))

    y = textline_patch_sum_along_width[:]  # [first_nonzero:last_nonzero]
    y_padded = np.zeros(len(y) + 40)
    y_padded[20:len(y) + 20] = y
    x = np.array(range(len(y)))

    peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
    
    try:
        y_padded_smoothed_e= gaussian_filter1d(y_padded, 2)
        y_padded_up_to_down_e=-y_padded+np.max(y_padded)
        y_padded_up_to_down_padded_e=np.zeros(len(y_padded_up_to_down_e)+40)
        y_padded_up_to_down_padded_e[20:len(y_padded_up_to_down_e)+20]=y_padded_up_to_down_e
        y_padded_up_to_down_padded_e= gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
        
        peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
        peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
        neg_peaks_max=np.max(y_padded_up_to_down_padded_e[peaks_neg_e])

        arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
            y_padded_up_to_down_padded_e[peaks_neg_e]/float(neg_peaks_max)<0.3]
        diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
        
        arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
        arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
        peaks_new=peaks_e[:]
        peaks_neg_new=peaks_neg_e[:]

        clusters_to_be_deleted=[]
        if len(arg_diff_cluster)>0:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
            for i in range(len(arg_diff_cluster)-1):
                clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
                                                                        arg_diff_cluster[i+1]+1])
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
        if len(clusters_to_be_deleted)>0:
            peaks_new_extra=[]
            for m in range(len(clusters_to_be_deleted)):
                min_cluster=np.min(peaks_e[clusters_to_be_deleted[m]])
                max_cluster=np.max(peaks_e[clusters_to_be_deleted[m]])
                peaks_new_extra.append( int( (min_cluster+max_cluster)/2.0) )
                for m1 in range(len(clusters_to_be_deleted[m])):
                    peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]-1]]
                    peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]]]
                    peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg_e[clusters_to_be_deleted[m][m1]]]
            peaks_new_tot=[]
            for i1 in peaks_new:
                peaks_new_tot.append(i1)
            for i1 in peaks_new_extra:
                peaks_new_tot.append(i1)
            peaks_new_tot=np.sort(peaks_new_tot)
        else:
            peaks_new_tot=peaks_e[:]

        textline_con,hierarchy=return_contours_of_image(img_patch)
        textline_con_fil=filter_contours_area_of_image(img_patch,
                                                        textline_con, hierarchy,
                                                        max_area=1, min_area=0.0008)

        if len(np.diff(peaks_new_tot))>0:
            y_diff_mean=np.mean(np.diff(peaks_new_tot))#self.find_contours_mean_y_diff(textline_con_fil)
            sigma_gaus=int(  y_diff_mean * (7./40.0) )
        else:
            sigma_gaus=12
            
    except:
        sigma_gaus=12
    if sigma_gaus<3:
        sigma_gaus=3

    y_padded_smoothed= gaussian_filter1d(y_padded, sigma_gaus)
    y_padded_up_to_down=-y_padded+np.max(y_padded)
    y_padded_up_to_down_padded=np.zeros(len(y_padded_up_to_down)+40)
    y_padded_up_to_down_padded[20:len(y_padded_up_to_down)+20]=y_padded_up_to_down
    y_padded_up_to_down_padded= gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
    peaks, _ = find_peaks(y_padded_smoothed, height=0)
    peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)
        
    try:
        neg_peaks_max=np.max(y_padded_smoothed[peaks])
        arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
            y_padded_up_to_down_padded[peaks_neg]/float(neg_peaks_max)<0.42]
        diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
        
        arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
        arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
        
    except:
        arg_neg_must_be_deleted=[]
        arg_diff_cluster=[]
    try:
        peaks_new=peaks[:]
        peaks_neg_new=peaks_neg[:]
        clusters_to_be_deleted=[]
        if len(arg_diff_cluster)>=2 and len(arg_diff_cluster)>0:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
            for i in range(len(arg_diff_cluster)-1):
                clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
                                                                      arg_diff_cluster[i+1]+1])
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
        elif len(arg_neg_must_be_deleted)>=2 and len(arg_diff_cluster)==0:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
    
        if  len(arg_neg_must_be_deleted)==1:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted)
        if len(clusters_to_be_deleted)>0:
            peaks_new_extra=[]
            for m in range(len(clusters_to_be_deleted)):
                min_cluster=np.min(peaks[clusters_to_be_deleted[m]])
                max_cluster=np.max(peaks[clusters_to_be_deleted[m]])
                peaks_new_extra.append( int( (min_cluster+max_cluster)/2.0) )
                for m1 in range(len(clusters_to_be_deleted[m])):
                    peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]-1]]
                    peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]]]
                    peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg[clusters_to_be_deleted[m][m1]]]
            peaks_new_tot=[]
            for i1 in peaks_new:
                peaks_new_tot.append(i1)
            for i1 in peaks_new_extra:
                peaks_new_tot.append(i1)
            peaks_new_tot=np.sort(peaks_new_tot)
            
            peaks=peaks_new_tot[:]
            peaks_neg=peaks_neg_new[:]
        else:
            peaks_new_tot=peaks[:]
            peaks=peaks_new_tot[:]
            peaks_neg=peaks_neg_new[:]
    except:
        pass
    if len(y_padded_smoothed[peaks]) > 1:
        mean_value_of_peaks=np.mean(y_padded_smoothed[peaks])
        std_value_of_peaks=np.std(y_padded_smoothed[peaks])
    else:
        mean_value_of_peaks = np.nan
        std_value_of_peaks = np.nan
    peaks_values=y_padded_smoothed[peaks]
    peaks_neg = peaks_neg - 20 - 20
    peaks = peaks - 20
    for jj in range(len(peaks_neg)):
        if peaks_neg[jj] > len(x) - 1:
            peaks_neg[jj] = len(x) - 1
    for jj in range(len(peaks)):
        if peaks[jj] > len(x) - 1:
            peaks[jj] = len(x) - 1

    textline_boxes = []
    textline_boxes_rot = []
    
    if len(peaks_neg) == len(peaks) + 1 and len(peaks) >= 3:
        for jj in range(len(peaks)):
            
            if jj==(len(peaks)-1):
                dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
                dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
                
                if peaks_values[jj]>mean_value_of_peaks-std_value_of_peaks/2.:
                    point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up)  ##+int(dis_to_next_up*1./4.0)
                    point_down =y_max_cont-1
                    ##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down)
                    #point_up
                    # np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)
                else:
                    point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up)  ##+int(dis_to_next_up*1./4.0)
                    point_down =y_max_cont-1
                    ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down)
                    #point_up
                    # np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)

                point_down_narrow = peaks[jj] + first_nonzero + int(
                    1.4 * dis_to_next_down)
                ###-int(dis_to_next_down*1./2)
            else:
                dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
                dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
                
                if peaks_values[jj]>mean_value_of_peaks-std_value_of_peaks/2.:
                    point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)
                else:
                    point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = peaks[jj] + first_nonzero + int(1.33 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)

                point_down_narrow = peaks[jj] + first_nonzero + int(
                    1.1 * dis_to_next_down)  ###-int(dis_to_next_down*1./2)

            if point_down_narrow >= img_patch.shape[0]:
                point_down_narrow = img_patch.shape[0] - 2
            

            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                            for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]

            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)  # max(x_min_interest,x_min_cont)
                x_max = np.max(xvinside)  # min(x_max_interest,x_max_cont)

            p1 = np.dot(rotation_matrix, [int(x_min), int(point_up)])
            p2 = np.dot(rotation_matrix, [int(x_max), int(point_up)])
            p3 = np.dot(rotation_matrix, [int(x_max), int(point_down)])
            p4 = np.dot(rotation_matrix, [int(x_min), int(point_down)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
            
            if x_min_rot1<0:
                x_min_rot1=0
            if x_min_rot4<0:
                x_min_rot4=0
            if point_up_rot1<0:
                point_up_rot1=0
            if point_up_rot2<0:
                point_up_rot2=0

            x_min_rot1=x_min_rot1-x_help
            x_max_rot2=x_max_rot2-x_help
            x_max_rot3=x_max_rot3-x_help
            x_min_rot4=x_min_rot4-x_help
            
            point_up_rot1=point_up_rot1-y_help
            point_up_rot2=point_up_rot2-y_help
            point_down_rot3=point_down_rot3-y_help
            point_down_rot4=point_down_rot4-y_help

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    elif len(peaks) < 1:
        pass

    elif len(peaks) == 1:
        distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                          tuple(int(x) for x in np.array([xv[mj], peaks[0] + first_nonzero])), True)
                     for mj in range(len(xv))]
        distances = np.array(distances)

        xvinside = xv[distances >= 0]
        if len(xvinside) == 0:
            x_min = x_min_cont
            x_max = x_max_cont
        else:
            x_min = np.min(xvinside)  # max(x_min_interest,x_min_cont)
            x_max = np.max(xvinside)  # min(x_max_interest,x_max_cont)
        #x_min = x_min_cont
        #x_max = x_max_cont

        y_min = y_min_cont
        y_max = y_max_cont

        p1 = np.dot(rotation_matrix, [int(x_min), int(y_min)])
        p2 = np.dot(rotation_matrix, [int(x_max), int(y_min)])
        p3 = np.dot(rotation_matrix, [int(x_max), int(y_max)])
        p4 = np.dot(rotation_matrix, [int(x_min), int(y_max)])

        x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
        x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
        x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
        x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
        
        if x_min_rot1<0:
            x_min_rot1=0
        if x_min_rot4<0:
            x_min_rot4=0
        if point_up_rot1<0:
            point_up_rot1=0
        if point_up_rot2<0:
            point_up_rot2=0
        
        x_min_rot1=x_min_rot1-x_help
        x_max_rot2=x_max_rot2-x_help
        x_max_rot3=x_max_rot3-x_help
        x_min_rot4=x_min_rot4-x_help
        
        point_up_rot1=point_up_rot1-y_help
        point_up_rot2=point_up_rot2-y_help
        point_down_rot3=point_down_rot3-y_help
        point_down_rot4=point_down_rot4-y_help

        textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                            [[int(x_max_rot2), int(point_up_rot2)]],
                                            [[int(x_max_rot3), int(point_down_rot3)]],
                                            [[int(x_min_rot4), int(point_down_rot4)]]]))
        textline_boxes.append(np.array([[[int(x_min), int(y_min)]],
                                        [[int(x_max), int(y_min)]],
                                        [[int(x_max), int(y_max)]],
                                        [[int(x_min), int(y_max)]]]))
    elif len(peaks) == 2:
        dis_to_next = np.abs(peaks[1] - peaks[0])
        for jj in range(len(peaks)):
            if jj == 0:
                point_up = 0#peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next)
                if point_up < 0:
                    point_up = 1
                point_down = peaks_neg[1] + first_nonzero# peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
            elif jj == 1:
                point_down =peaks_neg[1] + first_nonzero# peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
                if point_down >= img_patch.shape[0]:
                    point_down = img_patch.shape[0] - 2
                try:
                    point_up = peaks_neg[2] + first_nonzero#peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
                except:
                    point_up =peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
                    
            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                         for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]
            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)
                x_max = np.max(xvinside)

            p1 = np.dot(rotation_matrix, [int(x_min), int(point_up)])
            p2 = np.dot(rotation_matrix, [int(x_max), int(point_up)])
            p3 = np.dot(rotation_matrix, [int(x_max), int(point_down)])
            p4 = np.dot(rotation_matrix, [int(x_min), int(point_down)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
            
            if x_min_rot1<0:
                x_min_rot1=0
            if x_min_rot4<0:
                x_min_rot4=0
            if point_up_rot1<0:
                point_up_rot1=0
            if point_up_rot2<0:
                point_up_rot2=0                   
                
            x_min_rot1=x_min_rot1-x_help
            x_max_rot2=x_max_rot2-x_help
            x_max_rot3=x_max_rot3-x_help
            x_min_rot4=x_min_rot4-x_help
            
            point_up_rot1=point_up_rot1-y_help
            point_up_rot2=point_up_rot2-y_help
            point_down_rot3=point_down_rot3-y_help
            point_down_rot4=point_down_rot4-y_help

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    else:
        for jj in range(len(peaks)):
            if jj == 0:
                dis_to_next = peaks[jj + 1] - peaks[jj]
                # point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
                point_up = peaks[jj] + first_nonzero - int(1. / 1.9 * dis_to_next)
                if point_up < 0:
                    point_up = 1
                # point_down=peaks[jj]+first_nonzero+int(1./3*dis_to_next)
                point_down = peaks[jj] + first_nonzero + int(1. / 1.9 * dis_to_next)
            elif jj == len(peaks) - 1:
                dis_to_next = peaks[jj] - peaks[jj - 1]
                # point_down=peaks[jj]+first_nonzero+int(1./3*dis_to_next)
                point_down = peaks[jj] + first_nonzero + int(1. / 1.7 * dis_to_next)
                if point_down >= img_patch.shape[0]:
                    point_down = img_patch.shape[0] - 2
                # point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
                point_up = peaks[jj] + first_nonzero - int(1. / 1.9 * dis_to_next)
            else:
                dis_to_next_down = peaks[jj + 1] - peaks[jj]
                dis_to_next_up = peaks[jj] - peaks[jj - 1]

                point_up = peaks[jj] + first_nonzero - int(1. / 1.9 * dis_to_next_up)
                point_down = peaks[jj] + first_nonzero + int(1. / 1.9 * dis_to_next_down)
                
            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                         for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]
            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)  # max(x_min_interest,x_min_cont)
                x_max = np.max(xvinside)  # min(x_max_interest,x_max_cont)

            p1 = np.dot(rotation_matrix, [int(x_min), int(point_up)])
            p2 = np.dot(rotation_matrix, [int(x_max), int(point_up)])
            p3 = np.dot(rotation_matrix, [int(x_max), int(point_down)])
            p4 = np.dot(rotation_matrix, [int(x_min), int(point_down)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
            
            if x_min_rot1<0:
                x_min_rot1=0
            if x_min_rot4<0:
                x_min_rot4=0
            if point_up_rot1<0:
                point_up_rot1=0
            if point_up_rot2<0:
                point_up_rot2=0                
                
            x_min_rot1=x_min_rot1-x_help
            x_max_rot2=x_max_rot2-x_help
            x_max_rot3=x_max_rot3-x_help
            x_min_rot4=x_min_rot4-x_help
            
            point_up_rot1=point_up_rot1-y_help
            point_up_rot2=point_up_rot2-y_help
            point_down_rot3=point_down_rot3-y_help
            point_down_rot4=point_down_rot4-y_help

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    return peaks, textline_boxes_rot

def separate_lines_vertical(img_patch, contour_text_interest, thetha):
    thetha = thetha + 90
    contour_text_interest_copy = contour_text_interest.copy()
    x, y, x_d, y_d, xv, x_min_cont, y_min_cont, x_max_cont, y_max_cont, \
        first_nonzero, y_padded_up_to_down_padded, y_padded_smoothed, \
        peaks, peaks_neg, rotation_matrix = dedup_separate_lines(img_patch, contour_text_interest, thetha, 0)

    # plt.plot(y_padded_up_to_down_padded)
    # plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*')
    # plt.title('negs')
    # plt.show()

    # plt.plot(y_padded_smoothed)
    # plt.plot(peaks,y_padded_smoothed[peaks],'*')
    # plt.title('poss')
    # plt.show()

    neg_peaks_max = np.max(y_padded_up_to_down_padded[peaks_neg])

    arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
        y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
    diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)

    arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
    arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]

    peaks_new = peaks[:]
    peaks_neg_new = peaks_neg[:]
    clusters_to_be_deleted = []

    if len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) >= 2:
        clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
        for i in range(len(arg_diff_cluster) - 1):
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
                                                                  arg_diff_cluster[i + 1] + 1])
        clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
    elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
        clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
    else:
        clusters_to_be_deleted.append(arg_neg_must_be_deleted)
    if len(clusters_to_be_deleted) > 0:
        peaks_new_extra = []
        for m in range(len(clusters_to_be_deleted)):
            min_cluster = np.min(peaks[clusters_to_be_deleted[m]])
            max_cluster = np.max(peaks[clusters_to_be_deleted[m]])
            peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
            for m1 in range(len(clusters_to_be_deleted[m])):
                peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1] - 1]]
                peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1]]]
                peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg[clusters_to_be_deleted[m][m1]]]
        peaks_new_tot = []
        for i1 in peaks_new:
            peaks_new_tot.append(i1)
        for i1 in peaks_new_extra:
            peaks_new_tot.append(i1)
        peaks_new_tot = np.sort(peaks_new_tot)

        peaks = peaks_new_tot[:]
        peaks_neg = peaks_neg_new[:]

    else:
        peaks_new_tot = peaks[:]
        peaks = peaks_new_tot[:]
        peaks_neg = peaks_neg_new[:]
    
    if len(y_padded_smoothed[peaks])>1:
        mean_value_of_peaks = np.mean(y_padded_smoothed[peaks])
        std_value_of_peaks = np.std(y_padded_smoothed[peaks])
    else:
        mean_value_of_peaks = np.nan
        std_value_of_peaks = np.nan
        
    peaks_values = y_padded_smoothed[peaks]

    peaks_neg = peaks_neg - 20 - 20
    peaks = peaks - 20

    for jj in range(len(peaks_neg)):
        if peaks_neg[jj] > len(x) - 1:
            peaks_neg[jj] = len(x) - 1

    for jj in range(len(peaks)):
        if peaks[jj] > len(x) - 1:
            peaks[jj] = len(x) - 1

    textline_boxes = []
    textline_boxes_rot = []

    if len(peaks_neg) == len(peaks) + 1 and len(peaks) >= 3:
        for jj in range(len(peaks)):

            if jj == (len(peaks) - 1):
                dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
                dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])

                if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0:
                    point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = x_max_cont - 1
                    ##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down)
                    #point_up
                    # np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)
                else:
                    point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = x_max_cont - 1
                    ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down)
                    #point_up
                    # np.max(y_cont)
                    #peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)

                point_down_narrow = peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down)
                ###-int(dis_to_next_down*1./2)
            else:
                dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
                dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])

                if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0:
                    point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)
                else:
                    point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up)
                    ##+int(dis_to_next_up*1./4.0)
                    point_down = peaks[jj] + first_nonzero + int(1.33 * dis_to_next_down)
                    ###-int(dis_to_next_down*1./4.0)

                point_down_narrow = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down)
                ###-int(dis_to_next_down*1./2)

            if point_down_narrow >= img_patch.shape[0]:
                point_down_narrow = img_patch.shape[0] - 2
            
            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                         for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]
            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)  # max(x_min_interest,x_min_cont)
                x_max = np.max(xvinside)  # min(x_max_interest,x_max_cont)

            p1 = np.dot(rotation_matrix, [int(point_up), int(y_min_cont)])
            p2 = np.dot(rotation_matrix, [int(point_down), int(y_min_cont)])
            p3 = np.dot(rotation_matrix, [int(point_down), int(y_max_cont)])
            p4 = np.dot(rotation_matrix, [int(point_up), int(y_max_cont)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d

            if x_min_rot1 < 0:
                x_min_rot1 = 0
            if x_min_rot4 < 0:
                x_min_rot4 = 0
            if point_up_rot1 < 0:
                point_up_rot1 = 0
            if point_up_rot2 < 0:
                point_up_rot2 = 0

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    elif len(peaks) < 1:
        pass
    elif len(peaks) == 1:
        x_min = x_min_cont
        x_max = x_max_cont

        y_min = y_min_cont
        y_max = y_max_cont

        p1 = np.dot(rotation_matrix, [int(point_up), int(y_min_cont)])
        p2 = np.dot(rotation_matrix, [int(point_down), int(y_min_cont)])
        p3 = np.dot(rotation_matrix, [int(point_down), int(y_max_cont)])
        p4 = np.dot(rotation_matrix, [int(point_up), int(y_max_cont)])

        x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
        x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
        x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
        x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d

        if x_min_rot1 < 0:
            x_min_rot1 = 0
        if x_min_rot4 < 0:
            x_min_rot4 = 0
        if point_up_rot1 < 0:
            point_up_rot1 = 0
        if point_up_rot2 < 0:
            point_up_rot2 = 0

        textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                            [[int(x_max_rot2), int(point_up_rot2)]],
                                            [[int(x_max_rot3), int(point_down_rot3)]],
                                            [[int(x_min_rot4), int(point_down_rot4)]]]))
        textline_boxes.append(np.array([[[int(x_min), int(y_min)]],
                                        [[int(x_max), int(y_min)]],
                                        [[int(x_max), int(y_max)]],
                                        [[int(x_min), int(y_max)]]]))
    elif len(peaks) == 2:
        dis_to_next = np.abs(peaks[1] - peaks[0])
        for jj in range(len(peaks)):
            if jj == 0:
                point_up = 0  # peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next)
                if point_up < 0:
                    point_up = 1
                point_down = peaks[jj] + first_nonzero + int(1.0 / 1.8 * dis_to_next)
            elif jj == 1:
                point_down = peaks[jj] + first_nonzero + int(1.0 / 1.8 * dis_to_next)
                if point_down >= img_patch.shape[0]:
                    point_down = img_patch.shape[0] - 2
                point_up = peaks[jj] + first_nonzero - int(1.0 / 1.8 * dis_to_next)
            
            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                         for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]
            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)
                x_max = np.max(xvinside)

            p1 = np.dot(rotation_matrix, [int(point_up), int(y_min_cont)])
            p2 = np.dot(rotation_matrix, [int(point_down), int(y_min_cont)])
            p3 = np.dot(rotation_matrix, [int(point_down), int(y_max_cont)])
            p4 = np.dot(rotation_matrix, [int(point_up), int(y_max_cont)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d

            if x_min_rot1 < 0:
                x_min_rot1 = 0
            if x_min_rot4 < 0:
                x_min_rot4 = 0
            if point_up_rot1 < 0:
                point_up_rot1 = 0
            if point_up_rot2 < 0:
                point_up_rot2 = 0

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    else:
        for jj in range(len(peaks)):
            if jj == 0:
                dis_to_next = peaks[jj + 1] - peaks[jj]
                # point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
                point_up = peaks[jj] + first_nonzero - int(1.0 / 1.9 * dis_to_next)
                if point_up < 0:
                    point_up = 1
                # point_down=peaks[jj]+first_nonzero+int(1./3*dis_to_next)
                point_down = peaks[jj] + first_nonzero + int(1.0 / 1.9 * dis_to_next)
            elif jj == len(peaks) - 1:
                dis_to_next = peaks[jj] - peaks[jj - 1]
                # point_down=peaks[jj]+first_nonzero+int(1./3*dis_to_next)
                point_down = peaks[jj] + first_nonzero + int(1.0 / 1.7 * dis_to_next)
                if point_down >= img_patch.shape[0]:
                    point_down = img_patch.shape[0] - 2
                # point_up=peaks[jj]+first_nonzero-int(1./3*dis_to_next)
                point_up = peaks[jj] + first_nonzero - int(1.0 / 1.9 * dis_to_next)
            else:
                dis_to_next_down = peaks[jj + 1] - peaks[jj]
                dis_to_next_up = peaks[jj] - peaks[jj - 1]

                point_up = peaks[jj] + first_nonzero - int(1.0 / 1.9 * dis_to_next_up)
                point_down = peaks[jj] + first_nonzero + int(1.0 / 1.9 * dis_to_next_down)
            
            distances = [cv2.pointPolygonTest(contour_text_interest_copy,
                                              tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
                                              True)
                         for mj in range(len(xv))]
            distances = np.array(distances)

            xvinside = xv[distances >= 0]
            if len(xvinside) == 0:
                x_min = x_min_cont
                x_max = x_max_cont
            else:
                x_min = np.min(xvinside)  # max(x_min_interest,x_min_cont)
                x_max = np.max(xvinside)  # min(x_max_interest,x_max_cont)

            p1 = np.dot(rotation_matrix, [int(point_up), int(y_min_cont)])
            p2 = np.dot(rotation_matrix, [int(point_down), int(y_min_cont)])
            p3 = np.dot(rotation_matrix, [int(point_down), int(y_max_cont)])
            p4 = np.dot(rotation_matrix, [int(point_up), int(y_max_cont)])

            x_min_rot1, point_up_rot1 = p1[0] + x_d, p1[1] + y_d
            x_max_rot2, point_up_rot2 = p2[0] + x_d, p2[1] + y_d
            x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
            x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d

            if x_min_rot1 < 0:
                x_min_rot1 = 0
            if x_min_rot4 < 0:
                x_min_rot4 = 0
            if point_up_rot1 < 0:
                point_up_rot1 = 0
            if point_up_rot2 < 0:
                point_up_rot2 = 0

            textline_boxes_rot.append(np.array([[[int(x_min_rot1), int(point_up_rot1)]],
                                                [[int(x_max_rot2), int(point_up_rot2)]],
                                                [[int(x_max_rot3), int(point_down_rot3)]],
                                                [[int(x_min_rot4), int(point_down_rot4)]]]))
            textline_boxes.append(np.array([[[int(x_min), int(point_up)]],
                                            [[int(x_max), int(point_up)]],
                                            [[int(x_max), int(point_down)]],
                                            [[int(x_min), int(point_down)]]]))
    return peaks, textline_boxes_rot

def separate_lines_new_inside_tiles2(img_patch, _):
    y = img_patch.sum(axis=1)
    y_padded = np.pad(y, (20,))
    x = np.arange(len(y))

    peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
    try:
        y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
        y_padded_up_to_down_e = -y_padded + np.max(y_padded)
        y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
        y_padded_up_to_down_padded_e[20 : len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
        y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)

        peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
        peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
        neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])

        arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
            y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
        diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)

        arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
        arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]

        peaks_new = peaks_e[:]
        peaks_neg_new = peaks_neg_e[:]

        clusters_to_be_deleted = []
        if len(arg_diff_cluster) > 0:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
            for i in range(len(arg_diff_cluster) - 1):
                clusters_to_be_deleted.append(
                    arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
                                            arg_diff_cluster[i + 1] + 1])
            clusters_to_be_deleted.append(
                arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
        if len(clusters_to_be_deleted) > 0:
            peaks_new_extra = []
            for m in range(len(clusters_to_be_deleted)):
                min_cluster = np.min(peaks_e[clusters_to_be_deleted[m]])
                max_cluster = np.max(peaks_e[clusters_to_be_deleted[m]])
                peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
                for m1 in range(len(clusters_to_be_deleted[m])):
                    peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1] - 1]]
                    peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1]]]
                    peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg_e[clusters_to_be_deleted[m][m1]]]
            peaks_new_tot = []
            for i1 in peaks_new:
                peaks_new_tot.append(i1)
            for i1 in peaks_new_extra:
                peaks_new_tot.append(i1)
            peaks_new_tot = np.sort(peaks_new_tot)
        else:
            peaks_new_tot = peaks_e[:]

        textline_con, hierarchy = return_contours_of_image(img_patch)
        textline_con_fil = filter_contours_area_of_image(img_patch,
                                                         textline_con, hierarchy,
                                                         max_area=1, min_area=0.0008)
        if len(np.diff(peaks_new_tot)):
            y_diff_mean = np.mean(np.diff(peaks_new_tot))  # self.find_contours_mean_y_diff(textline_con_fil)
            sigma_gaus = int(y_diff_mean * (7.0 / 40.0))
        else:
            sigma_gaus = 12

    except:
        sigma_gaus = 12
    if sigma_gaus < 3:
        sigma_gaus = 3

    y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
    y_padded_neg = np.pad(np.max(y_padded) - y_padded, (20,))
    y_padded_neg_smoothed = gaussian_filter1d(y_padded_neg, sigma_gaus)

    peaks, _ = find_peaks(y_padded_smoothed, height=0)
    peaks_neg, _ = find_peaks(y_padded_neg_smoothed, height=0)

    peaks_new = peaks[:]
    peaks_neg_new = peaks_neg[:]

    try:
        arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
            y_padded_neg_smoothed[peaks_neg] <
            y_padded_smoothed[peaks].max() * 0.24]
        diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)

        arg_diff = np.arange(len(diff_arg_neg_must_be_deleted))
        arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]

        clusters_to_be_deleted = []
        if len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) >= 2:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
            for i in range(len(arg_diff_cluster) - 1):
                clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
                                                                      arg_diff_cluster[i + 1] + 1])
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
        elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
        else:
            clusters_to_be_deleted.append(arg_neg_must_be_deleted)
        if len(clusters_to_be_deleted) > 0:
            peaks_new_extra = []
            for m in range(len(clusters_to_be_deleted)):
                min_cluster = np.min(peaks[clusters_to_be_deleted[m]])
                max_cluster = np.max(peaks[clusters_to_be_deleted[m]])
                peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
                for m1 in range(len(clusters_to_be_deleted[m])):
                    peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1] - 1]]
                    peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1]]]
                    peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg[clusters_to_be_deleted[m][m1]]]
            peaks_new_tot = []
            for i1 in peaks_new:
                peaks_new_tot.append(i1)
            for i1 in peaks_new_extra:
                peaks_new_tot.append(i1)
            peaks_new_tot = np.sort(peaks_new_tot)

            # plt.plot(y_padded_neg_smoothed)
            # plt.plot(peaks_neg,y_padded_neg_smoothed[peaks_neg],'*')
            # plt.show()

            # plt.plot(y_padded_neg_smoothed)
            # plt.plot(peaks_neg_new,y_padded_neg_smoothed[peaks_neg_new],'*')
            # plt.show()

            # plt.plot(y_padded_smoothed)
            # plt.plot(peaks,y_padded_smoothed[peaks],'*')
            # plt.show()

            # plt.plot(y_padded_smoothed)
            # plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*')
            # plt.show()
            peaks = peaks_new_tot[:]
            peaks_neg = peaks_neg_new[:]
    except:
        pass

    else:
        peaks_new_tot = peaks[:]
        peaks = peaks_new_tot[:]
        peaks_neg = peaks_neg_new[:]
    
    # if len(y_padded_smoothed[peaks]) > 1:
    #     mean_value_of_peaks = np.mean(y_padded_smoothed[peaks])
    #     std_value_of_peaks = np.std(y_padded_smoothed[peaks])
    # else:
    #     mean_value_of_peaks = np.nan
    #     std_value_of_peaks = np.nan
        
    # peaks_values = y_padded_smoothed[peaks]

    def clip(positions):
        # prevent wrap around array bounds
        return np.maximum(0, np.minimum(img_patch.shape[0] - 1, positions))

    peaks_neg_true = clip(np.array(peaks_neg) - 40)
    peaks_pos_true = clip(np.array(peaks) - 20)

    # ax1 = plt.subplot(1, 2, 1, title="textline mask slice")
    # plt.imshow(img_patch, aspect="auto")
    # ax2 = plt.subplot(1, 2, 2, title="projection profile", sharey=ax1)
    # plt.plot(y, x)
    # ax2.scatter(y[peaks_neg_true], peaks_neg_true, color='r', label="neg (0)")
    # ax2.scatter(y[peaks_pos_true], peaks_pos_true, color='g', label="pos (1)")
    # plt.legend()
    # plt.show()

    offsets = np.arange(-6, 6)
    def add_offsets(positions):
        # let y range around peak positions (without slice indexing)
        return (positions[np.newaxis] + offsets[:, np.newaxis]).flatten()

    if peaks_neg_true.size:
        img_patch[clip(add_offsets(peaks_neg_true))] = 0

    if peaks_pos_true.size:
        img_patch[clip(add_offsets(peaks_pos_true))] = 1

    kernel = np.ones((5, 5), np.uint8)
    # img_patch = cv2.erode(img_patch,kernel,iterations = 3)
    img_patch = cv2.erode(img_patch, kernel, iterations=1)
    return img_patch

def separate_lines_new_inside_tiles(img_path, _):
    mada_n = img_path.sum(axis=1)

    ##plt.plot(mada_n)
    ##plt.show()

    first_nonzero = 0  # (next((i for i, x in enumerate(mada_n) if x), None))

    y = mada_n[:]  # [first_nonzero:last_nonzero]
    y_help = np.zeros(len(y) + 40)
    y_help[20 : len(y) + 20] = y
    x = np.array(range(len(y)))

    peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
    if len(peaks_real) <= 2 and len(peaks_real) > 1:
        sigma_gaus = 10
    else:
        sigma_gaus = 5

    z = gaussian_filter1d(y_help, sigma_gaus)
    zneg_rev = -y_help + np.max(y_help)
    zneg = np.zeros(len(zneg_rev) + 40)
    zneg[20 : len(zneg_rev) + 20] = zneg_rev
    zneg = gaussian_filter1d(zneg, sigma_gaus)

    peaks, _ = find_peaks(z, height=0)
    peaks_neg, _ = find_peaks(zneg, height=0)

    for nn in range(len(peaks_neg)):
        if peaks_neg[nn] > len(z) - 1:
            peaks_neg[nn] = len(z) - 1
        if peaks_neg[nn] < 0:
            peaks_neg[nn] = 0

    diff_peaks = np.abs(np.diff(peaks_neg))

    cut_off = 20
    peaks_neg_true = []
    forest = []

    for i in range(len(peaks_neg)):
        if i == 0:
            forest.append(peaks_neg[i])
        if i < (len(peaks_neg) - 1):
            if diff_peaks[i] <= cut_off:
                forest.append(peaks_neg[i + 1])
            if diff_peaks[i] > cut_off:
                if not np.isnan(forest[np.argmin(z[forest])]):
                    peaks_neg_true.append(forest[np.argmin(z[forest])])
                forest = []
                forest.append(peaks_neg[i + 1])
        if i == (len(peaks_neg) - 1):
            if not np.isnan(forest[np.argmin(z[forest])]):
                peaks_neg_true.append(forest[np.argmin(z[forest])])

    diff_peaks_pos = np.abs(np.diff(peaks))

    cut_off = 20
    peaks_pos_true = []
    forest = []

    for i in range(len(peaks)):
        if i == 0:
            forest.append(peaks[i])
        if i < (len(peaks) - 1):
            if diff_peaks_pos[i] <= cut_off:
                forest.append(peaks[i + 1])
            if diff_peaks_pos[i] > cut_off:
                if not np.isnan(forest[np.argmax(z[forest])]):
                    peaks_pos_true.append(forest[np.argmax(z[forest])])
                forest = []
                forest.append(peaks[i + 1])
        if i == (len(peaks) - 1):
            if not np.isnan(forest[np.argmax(z[forest])]):
                peaks_pos_true.append(forest[np.argmax(z[forest])])


    if len(peaks_neg_true) > 0:
        peaks_neg_true = np.array(peaks_neg_true)
        """
        #plt.figure(figsize=(40,40))
        #plt.subplot(1,2,1)
        #plt.title('Textline segmentation von Textregion')
        #plt.imshow(img_path)
        #plt.xlabel('X')
        #plt.ylabel('Y')
        #plt.subplot(1,2,2)
        #plt.title('Dichte entlang X')
        #base = pyplot.gca().transData
        #rot = transforms.Affine2D().rotate_deg(90)
        #plt.plot(zneg,np.array(range(len(zneg))))
        #plt.plot(zneg[peaks_neg_true],peaks_neg_true,'*')
        #plt.gca().invert_yaxis()

        #plt.xlabel('Dichte')
        #plt.ylabel('Y')
        ##plt.plot([0,len(y)], [grenze,grenze])
        #plt.show()
        """
        peaks_neg_true = peaks_neg_true - 20 - 20

        for i in range(len(peaks_neg_true)):
            img_path[peaks_neg_true[i] - 6 : peaks_neg_true[i] + 6, :] = 0

    else:
        pass

    if len(peaks_pos_true) > 0:
        peaks_pos_true = np.array(peaks_pos_true)
        peaks_pos_true = peaks_pos_true - 20

        for i in range(len(peaks_pos_true)):
            img_path[peaks_pos_true[i] - 8 : peaks_pos_true[i] + 8, :] = 1
    else:
        pass
    kernel = np.ones((5, 5), np.uint8)

    # img_path = cv2.erode(img_path,kernel,iterations = 3)
    img_path = cv2.erode(img_path, kernel, iterations=2)
    return img_path

def separate_lines_vertical_cont(textline_mask, box_ind):
    kernel = np.ones((5, 5), np.uint8)
    min_area = 0
    max_area = 1

    _, thresh = cv2.threshold(textline_mask.astype(np.uint8), 0, 255, 0)
    contours, hierarchy = cv2.findContours(thresh.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = return_parent_contours(contours, hierarchy)
    contours = filter_contours_area_of_image_tables(thresh,
                                                    contours, hierarchy,
                                                    max_area=max_area,
                                                    min_area=min_area)
    contours_final = []
    for contour in contours:
        img = np.zeros_like(textline_mask, dtype=np.uint8)
        img = cv2.fillPoly(img, pts=[contour], color=255)
        img = cv2.dilate(img, kernel, iterations=4)
        _, thresh = cv2.threshold(img, 0, 255, 0)
        contours_text_rot, _ = cv2.findContours(thresh.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        contours_final.append(contours_text_rot[0])

    return None, contours_final

def textline_contours_postprocessing(textline_mask, angle, contour_parent):
    x, y, w, h = cv2.boundingRect(contour_parent)
    label = 255
    textline_mask = textline_mask * label
    kernel = np.ones((5, 5), np.uint8)
    textline_mask = cv2.morphologyEx(textline_mask, cv2.MORPH_OPEN, kernel)
    textline_mask = cv2.morphologyEx(textline_mask, cv2.MORPH_CLOSE, kernel)
    textline_mask = cv2.erode(textline_mask, kernel, iterations=2)
    # textline_mask = cv2.erode(textline_mask, kernel, iterations=1)

    textline_mask_d = rotate_image_enlarge(textline_mask, angle)
    #textline_mask_d[textline_mask_d != 0] = 1

    # if np.abs(angle)>.5 and textline_mask.shape[0]/float(textline_mask.shape[1])>3:

    contour_parent = contour_parent - [x, y]
    img = np.zeros((h, w), dtype=np.uint8)
    img = cv2.fillPoly(img, pts=[contour_parent], color=255)
    img_d = rotate_image_enlarge(img, angle)

    _, thresh = cv2.threshold(img_d, 0, 255, 0)
    contours_parent_d, _ = cv2.findContours(thresh.astype(np.uint8),
                                            cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
    contour_parent_d = contours_parent_d[
        np.argmax(map(cv2.contourArea, contours_parent_d))]

    _, contours_rotated_clean = separate_lines(
        textline_mask_d, contour_parent_d,
        # already deskewed!
        #angle, x_off, y_off)
        0, 0, 0)

    # undo relative coordinates
    transform = rotate_coordinates(np.eye(3), -angle, 0.5 * np.array([h, w]))
    transform = shift_coordinates(transform, [x, y])
    contours_rotated_clean = [np.round(transform_coordinates(contour[:, 0],
                                                             transform)[:, np.newaxis])
                              .astype(int)
                              for contour in contours_rotated_clean
                              if len(contour) > 3]
    return contours_rotated_clean

def separate_lines_new2(img_crop, _, num_col, slope_region, logger=None, plotter=None):
    """
    morph textline mask to cope with warped lines by independently deskewing horizontal slices
    """
    if logger is None:
        logger = getLogger(__package__)
    if not np.prod(img_crop.shape):
        return img_crop

    height, width = img_crop.shape
    num_patches = max(1, width // (200 if num_col == 1 else 140))
    length_x = width // num_patches
    # margin = int(0.04 * length_x) just recently this was changed because it break lines into 2
    margin = int(0.04 * length_x)
    # if margin<=4:
    # margin = int(0.08 * length_x)
    # margin=0

    width_mid = length_x - 2 * margin

    img_crop_revised = np.zeros_like(img_crop)
    for index_x_d in range(0, width, width_mid):
        index_x_u = index_x_d + length_x
        if index_x_u > width:
            if index_x_u >= width + width_mid:
                break # already in last window
            index_x_u = width
            index_x_d = width - length_x

        # box = (slice(index_y_d, index_y_u), slice(index_x_d, index_x_u))
        # img_patch = img_crop[box]
        box = (slice(None), slice(index_x_d, index_x_u))
        img_xline = img_crop[box]

        if img_xline.any():
            slope_xline = return_deskew_slop(img_xline, 2, logger=logger, plotter=plotter)
        else:
            continue

        if (abs(slope_region) < 25 and
            abs(slope_xline) > 25):
            slope_xline = slope_region
        # if abs(slope_region)>70 and abs(slope_xline)<25:
        #     slope_xline = slope_region

        pad_above = pad_below = int(img_xline.shape[0] * 0.1)
        pad_left = pad_right = img_xline.shape[1]
        img_xline_padded = np.pad(img_xline, ((pad_above, pad_below),
                                              (pad_left, pad_right)))
        # plt.subplot(2, 2, 1, title="xline padded")
        # plt.imshow(img_xline_padded)
        img_xline_rotated = rotate_image(img_xline_padded, slope_xline)
        #img_xline_rotated[img_xline_rotated != 0] = 1
        # plt.subplot(2, 2, 2, title="xline rotated")
        # plt.imshow(img_xline_rotated)
        img_xline_separated = separate_lines_new_inside_tiles2(img_xline_rotated, 0)
        # plt.subplot(2, 2, 3, title="xline separated")
        # plt.imshow(img_xline_separated)
        img_xline_separated = rotate_image(img_xline_separated, -slope_xline)
        #img_xline_separated[img_xline_separated != 0] = 1
        # plt.subplot(2, 2, 4, title="xline unrotated")
        # plt.imshow(img_xline_separated)
        # plt.show()

        # unpad
        img_xline_separated = img_xline_separated[
            pad_above: -pad_below,
            pad_left: -pad_right]

        # window
        window = (slice(None), slice(margin, -margin or None))
        img_crop_revised[box][window] = img_xline_separated[window]
        # plt.subplot(1, 2, 1, title="original box")
        # plt.imshow(img_crop[box])
        # plt.gca().add_patch(patches.Rectangle((margin, 0), length_x - 2 * margin, height, alpha=0.5, color='gray'))
        # plt.subplot(1, 2, 2, title="revised box")
        # plt.imshow(img_crop_revised[box])
        # plt.gca().add_patch(patches.Rectangle((margin, 0), length_x - 2 * margin, height, alpha=0.5, color='gray'))
        # plt.show()

    return img_crop_revised

def do_image_rotation(angle, img=None, sigma_des=1.0, logger=None):
    if logger is None:
        logger = getLogger(__package__)
    img_rot = rotate_image(img, angle)
    img_rot[img_rot!=0] = 1
    try:
        var = find_num_col_deskew(img_rot, sigma_des, 20.3)
    except:
        logger.exception("cannot determine variance for angle %.2f°", angle)
        var = 0
    return var

def return_deskew_slop(img,
                       sigma_des,
                       n_tot_angles=100,
                       main_page=False,
                       logger=None,
                       plotter=None,
                       name=None):
    if main_page and plotter:
        plotter.save_plot_of_textline_density(img, name)

    height, width = img.shape[:2]
    max_shape = int(np.max(img.shape) * 1.1)

    onset_x = int(0.5 * (max_shape - width))
    onset_y = int(0.5 * (max_shape - height))

    img_resized = np.zeros((max_shape, max_shape))
    img_resized[onset_y: onset_y + height,
                onset_x: onset_x + width] = img

    def best_angle(angles):
        return get_smallest_skew(img_resized, sigma_des, angles,
                                 logger=logger,
                                 name=name,
                                 plotter=plotter)

    if main_page and width > height:
        angles = np.array([-45, 0, 45, 90,])
        angle, _ = best_angle(angles)

        angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles)
        angle, _ = best_angle(sigma_des)
    elif main_page:
        #angles = np.linspace(-12, 12, n_tot_angles)#np.array([0 , 45 , 90 , -45])
        angles = np.concatenate((np.linspace(-12, -7, n_tot_angles // 4),
                                 np.linspace(-6, 6, n_tot_angles // 2),
                                 np.linspace(7, 12, n_tot_angles // 4)))
        angle, var = best_angle(angles)

    else:
        angles = np.linspace(-25, 25, int(0.5 * n_tot_angles) + 10)
        angle, var = best_angle(angles)

    # precision stage:
    angles = np.linspace(angle - 2.5, angle + 2.5, n_tot_angles // 2)
    angle, _ = best_angle(angles)

    return angle

def get_smallest_skew(img, sigma_des, angles, logger=None, plotter=None, name=None):
    if logger is None:
        logger = getLogger(__package__)
    results = [do_image_rotation(angle, img=img, sigma_des=sigma_des, logger=logger)
               for angle in angles]
    if plotter:
        plotter.save_plot_of_rotation_angle(angles, results, name)
    try:
        var_res = np.array(results)
        assert var_res.any()
        idx = np.argmax(var_res)
        angle = angles[idx]
        var = var_res[idx]
    except:
        logger.exception("cannot determine best angle among %s", str(angles))
        angle = 0
        var = 0
    return angle, var

def do_work_of_slopes_new_curved(
        contour_par,
        textline_mask_tot_ea=None,
        num_col=1, slope_deskew=0.0,
        logger=None, MAX_SLOPE=999,
        KERNEL=None, plotter=None,
        name=None,
):
    if KERNEL is None:
        KERNEL = np.ones((5, 5), np.uint8)
    if logger is None:
        logger = getLogger(__package__)
    logger.debug("enter do_work_of_slopes_new_curved")

    x, y, w, h = cv2.boundingRect(contour_par)

    mask_parent = np.zeros((h, w), dtype=np.uint8)
    mask_parent = cv2.fillPoly(mask_parent, pts=[contour_par - [x, y]], color=1)
    all_text_region_raw = textline_mask_tot_ea[y: y + h, x: x + w] * mask_parent
    if not np.any(all_text_region_raw):
        return [], slope_deskew
    img_int_p = np.copy(all_text_region_raw)
    # correct for relative area
    rel_area = 1.0 * textline_mask_tot_ea.size / img_int_p.size

    # img_int_p=cv2.erode(img_int_p,KERNEL,iterations = 2)
    # plt.imshow(img_int_p)
    # plt.show()

    slope = slope_deskew
    if h >= 0.1 * w:
        try:
            textline_con, hierarchy = return_contours_of_image(img_int_p)
            textline_con_fil = filter_contours_area_of_image(img_int_p, textline_con,
                                                             hierarchy,
                                                             min_area=0.0008 * rel_area)
            if len(textline_con_fil) > 1:
                cx, cy = find_center_of_contours(textline_con_fil)
                y_diff_mean = np.median(np.diff(np.sort(np.array(cy))))
                x_diff_mean = np.median(np.diff(np.sort(np.array(cx))))
                if h > w and x_diff_mean / w > 2 * y_diff_mean / h:
                    # print(len(textline_con_fil), "transposed", x_diff_mean, y_diff_mean)
                    transposed = True
                    img_int_p = img_int_p.T
                    sigma = x_diff_mean
                else:
                    transposed = False
                    sigma = y_diff_mean
                slope = return_deskew_slop(img_int_p, max(1.0, 0.1 * sigma),
                                           logger=logger,
                                           name=name,
                                           plotter=plotter)
                if transposed:
                    slope = -90 - slope if slope < 0 else 90 - slope
                if abs(slope - slope_deskew) < 0.5:
                    slope = slope_deskew
            elif len(textline_con_fil):
                if h > 3 * w:
                    # print(1, "transposed", h, w)
                    transposed = True
                    img_int_p = img_int_p.T
                else:
                    transposed = False
                # do linear regression on mask to get slope
                img_ys, img_xs = img_int_p.nonzero()
                regression = linregress(x=img_xs, y=img_ys)
                slope = 180 / np.pi * np.arctan(regression.slope)
                # print(regression, regression.stderr)
                if transposed:
                    slope = 90 - slope
                if regression.stderr > 0.005:
                    slope = slope_deskew
        except:
            logger.exception("cannot determine angle of contours")
            slope = slope_deskew

    # print(slope, slope_deskew)

    if abs(slope) < 45:
        # apply horizontal tiling, deskew each patch independently
        mask_textlines_separated_d = separate_lines_new2(all_text_region_raw, 0,
                                                         num_col, slope,
                                                         logger=logger, plotter=plotter)
        # plt.subplot(1, 2, 1, title="textline mask of region")
        # plt.imshow(all_text_region_raw)
        # plt.subplot(1, 2, 2, title="separated+deskewed")
        # plt.imshow(mask_textlines_separated_d)
        # plt.show()

        textline_contours = return_contours_of_interested_textline(
            mask_textlines_separated_d, 1, min_area=3e-9 * rel_area)

        textlines_cnt_per_region = []
        for contour in textline_contours:
            mask_line = np.zeros_like(mask_parent)
            mask_line = cv2.fillPoly(mask_line, pts=[contour], color=1)
            mask_line = cv2.dilate(mask_line, KERNEL, iterations=5 if num_col == 0 else 4)
            # plt.subplot(1, 2, 1, title="parent mask")
            # plt.imshow(mask_parent)
            # plt.subplot(1, 2, 2, title="single textline")
            # plt.imshow(mask_line)
            # plt.show()

            textline_contours2 = return_contours_of_interested_textline(
                mask_line, 1, min_area=3e-9 * rel_area)
            textline_areas2 = np.array(list(map(cv2.contourArea, textline_contours2)))
            try:
                contour2 = textline_contours2[np.argmax(textline_areas2)]
                textlines_cnt_per_region.append(contour2 + [x, y])
            except Exception as why:
                logger.error(why)
    else:
        textlines_cnt_per_region = textline_contours_postprocessing(all_text_region_raw,
                                                                    slope, contour_par)

    return textlines_cnt_per_region[::-1], slope

def do_work_of_slopes_new_light(
        box_text, contour, contour_par,
        textline_mask_tot_ea=None, slope_deskew=0,
        logger=None
):
    if logger is None:
        logger = getLogger(__package__)
    logger.debug('enter do_work_of_slopes_new_light')

    x, y, w, h = box_text
    mask_textline = np.zeros(textline_mask_tot_ea.shape)
    mask_textline = cv2.fillPoly(mask_textline, pts=[contour], color=(1,1,1))
    all_text_region_raw = textline_mask_tot_ea * mask_textline
    all_text_region_raw = all_text_region_raw[y: y + h, x: x + w].astype(np.uint8)

    mask_only_con_region = np.zeros(textline_mask_tot_ea.shape)
    mask_only_con_region = cv2.fillPoly(mask_only_con_region, pts=[contour_par], color=(1, 1, 1))

    all_text_region_raw = np.copy(textline_mask_tot_ea)
    all_text_region_raw[mask_only_con_region == 0] = 0
    cnt_clean_rot_raw, hir_on_cnt_clean_rot = return_contours_of_image(all_text_region_raw)
    cnt_clean_rot = filter_contours_area_of_image(all_text_region_raw, cnt_clean_rot_raw, hir_on_cnt_clean_rot,
                                                  max_area=1, min_area=0.00001)

    return cnt_clean_rot, slope_deskew
