
from pylab import *
import numpy as np
import random
import matplotlib.cbook as cbook
import random
import time
from scipy.misc import imread
from scipy.misc import imsave
from scipy.misc import imresize
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os


def add_gaussian_noise(im, sigma):
    #The fast way:
    #noise = np.random.normal(0, sigma, im.flatten().shape[0]).reshape(im.shape)
    #return im + noise
    
    #Pixel-by-pixel:
    new_im = zeros(im.shape)
    for i in range(im.shape[0]):
        for j in range(im.shape[1]):
            new_im[i, j] = im[i,j] + np.random.normal(0, sigma)
            
    return new_im
    '''
    Output for np.random.normal(0, 1, 100):
    array([-0.76212262, -0.6872071 , -0.52611266, -0.81518392, -0.81353618,
        0.09261333, -0.34708412, -0.48222584, -0.91126298,  1.15739551,
        1.09993755,  0.19425294,  0.4058364 , -0.48274124,  0.82048098,
        1.7293443 ,  1.36098158,  0.72408743,  1.56613572,  0.07631909,
       -0.39605916, -0.76288133, -0.76991476, -0.46941724,  0.73246658,
       -0.66338691,  0.48895206,  0.37969643, -0.8581511 , -1.10091778,
       -0.31361782, -0.19210265,  0.41381098, -0.09848022,  1.06773044,
        0.45041539, -0.18040387,  1.63477534,  2.00853448,  1.530584  ,
        0.8785562 ,  1.6870824 , -0.62983668,  1.13449002,  1.62530525,
        0.02123588,  0.60939996, -0.41529926,  0.54282461,  0.76193384,
       -0.34141767, -0.03236356, -0.30992087, -0.99162358,  0.93145108,
       -0.3163145 ,  0.52143089,  0.09209418,  0.28373494, -0.79108792,
        0.85914467,  0.29663496, -0.82583234,  0.89119309, -0.18602411,
        0.55828479,  0.59443339,  1.03426478, -0.56007394,  1.15010731,
       -0.67348014,  2.28755741, -0.52386616, -1.127265  , -0.99849553,
        0.87241285, -0.76597075,  0.46093803,  0.47238958,  0.3497888 ,
       -1.18114143, -2.12118293, -0.36482335, -0.88596038, -0.67769965,
        2.1885937 , -1.03120322, -0.34779238,  0.16957686,  0.43689859,
        0.62544631, -1.6023358 , -1.08155522, -1.30897055, -0.18504192,
       -0.93581643, -1.07039971,  0.31171   , -1.13272494,  0.65374509])
    '''

def contrast_norm(im):
    return (im-min(im.flatten()))/(max(im.flatten())-min(im.flatten()))




os.chdir('c:/Users/Guerzhoy/Desktop/CSC320/tutorials/tut1/')

im = imread('edge.png')[:,:,0]/255.0

filtered_i = zeros(im.shape)
conv_i = zeros(im.shape)
diff_filt = array([[-1, 0, 1],
                  [-2, 0, 2],
                  [-1, 0, 1]])

for i in range(diff_filt.shape[0]/2, filtered_i.shape[0]-diff_filt.shape[0]/2):
    for j in range(diff_filt.shape[0]/2, filtered_i.shape[1]-diff_filt.shape[0]/2):
        #Note: in the assignment (and in general), the code below should be sped up by computing it
        #as using a single dot product
        #Note: the convolution is just a dot product with a filter that was
        #reflected around the centre both vertically and horizontally
        for u in range(-(diff_filt.shape[0]/2), diff_filt.shape[0]/2+1):
            for v in range(-(diff_filt.shape[1]/2), diff_filt.shape[1]/2+1):
                filtered_i[i,j] += diff_filt[diff_filt.shape[0]/2+u, diff_filt.shape[1]/2+v]  * \
                                   im[i+u, j+v]
                conv_i[i,j] +=     diff_filt[diff_filt.shape[0]/2+u, diff_filt.shape[1]/2+v]  * \
                                   im[i-u, j-v]
                
                
                
imshow(contrast_norm(filtered_i))                
                 