import os
import sys
import glob
import math
import numpy as np
from skimage import feature
from scipy import ndimage as ndi
from skimage import exposure,data

def ShowEdges(img,gaussian_choice=0,sigma=1):
    
    if gaussian_choice:
        gaussian_image = ndi.gaussian_filter(img, 4)
        img = img*(1-gaussian_choice)+gaussian_choice*gaussian_image
    edges = feature.canny(img,sigma=sigma)
    return edges


def HFFilter(img):
    F_image = np.fft.fft2(img)
    fshift = np.fft.fftshift(F_image)
    height,width = img.shape
    distance0 = int(height/2.0)
    result = np.zeros_like(fshift)
    for row_idx in range(height):
        for col_idx in range(width):
            distance = float((row_idx-height)**2+(col_idx-width)**2)**0.5
            ratio = math.exp(-(distance)/(2*(distance0)))
            result[row_idx][col_idx] = (1-ratio)*fshift[row_idx][col_idx]

    result = np.fft.ifftshift(result)
    result = np.fft.ifft2(result)
    final_result = np.zeros_like(img)
    for idx in range(height):
        for j_idx in range(width):
            
            final_result[idx][j_idx] = (result[idx][j_idx].real)
#     p2, p98 = np.percentile(final_result, (1, 99))
#     final_result = exposure.rescale_intensity(final_result,in_range=(p2,p98))
    return final_result
            