from usefull_func import *
from math import sqrt, atan2

#En_cours...
def filtre_canny(img):

    def norme_gradient(pixel1, pixel2):
        color_x = pixel1[0]
        color_y = pixel2[0]
        
        norm = round(sqrt(color_x**2 + color_y**2))
        norm = min(norm, 255)

        grad = atan2(color_y, color_x)
        return norm, grad

    def liste_normGrad(im1, im2):
        liste = []
        for j in range(len(im1)):
            ligne = []
            for i in range(len(im1[0])):
                normGrad = norme_gradient(im1[j][i], im2[j][i])
                ligne.append(normGrad)
            liste.append(ligne)
        return liste

    if not is_greyscale(img):
        img = greyscale(img)
    
    mat_x = [[-1,0,1]]
    mat_y = [[1],[0],[-1]]

    #lissage/suppression des bri
    img_no_bruit = convolution_gauss(img)
    Jx = convolution(img, mat_x)
    Jy = convolution(img, mat_y)
    normGrad = liste_normGrad(Jx, Jy)
    #Suppresion des non-maximum

