def MatchAll(I, template, thr):
    Ibcp = copy(I)
    (w,h,t) = shape(template)
    CrossCorr = cv2.matchTemplate(I, template, cv2.TM_CCORR_NORMED)
    cv2.normalize(CrossCorr,CrossCorr,0,255,cv2.NORM_MINMAX)
    
    for idxr, r in enumerate(CrossCorr):
        for idxc, c in enumerate(r):
            if c>= thr:
                x=idxc
                y=idxr
                x1=idxc+h
                x2=idxr+w
                cv2.rectangle(Ibcp, (x,y), (x1,x2),(60,45,255),2)
        
        
    tools.show_Multiple_images_pylab(Oryginal=I, CrossCor=CrossCorr, result = Ibcp)
def MatchAll(I, template, thr):
    Ibcp = copy(I)
    (w, h, t) = shape(template)
    CrossCorr = cv2.matchTemplate(I, template, cv2.TM_CCORR_NORMED)
    cv2.normalize(CrossCorr, CrossCorr, 0, 255, cv2.NORM_MINMAX)

    for idxr, r in enumerate(CrossCorr):
        for idxc, c in enumerate(r):
            if c >= thr:
                x = idxc
                y = idxr
                x1 = idxc + h
                x2 = idxr + w
                cv2.rectangle(Ibcp, (x, y), (x1, x2), (60, 45, 255), 2)

    tools.show_Multiple_images_pylab(Oryginal=I,
                                     CrossCor=CrossCorr,
                                     result=Ibcp)
def addGaussianRandomNoise(I, mu, sigma):
    M,N=I.shape
    gaussianNoise = np.random.normal(mu, sigma,(M,N))
    gaussianNoise=tools.to_uint8(gaussianNoise)
    I += gaussianNoise
    return I 
def addRandomUniformNoise(I):
    M,N=I.shape
    noise = np.random.rand(M,N)*60
    noise=tools.to_uint8(noise)#
    I += noise
    return I
    M,N=I.shape
    I[((np.random.rand(M,N))<density)] = [0]
    I[((np.random.rand(M,N))<density)] = [255]
    return I





#------------------------------BODY---------------------------------------

I = cv2.imread("lena.png")
I = cv2.cvtColor(I, cv2.COLOR_RGB2GRAY)

#Dst = BoxFilter(I,7)
#tools.show_Multiple_images_OpenCV(Oryginal=I, Filtered = Dst, Difference = cv2.absdiff(I, Dst))

#tools.show_1_image_OpenCV(addSaltNPepperNoise(I, 0.07))

Iun = addRandomUniformNoise(copy(I))
Igrn = addGaussianRandomNoise(copy(I), 0, 50)
Isp = addSaltNPepperNoise(copy(I), 0.07)

    
tools.show_Multiple_images_OpenCV(Iun=BoxFilter(Iun, 5), Igrn=BoxFilter(Igrn, 5), SnP=BoxFilter(I, 5))





def addGaussianRandomNoise(I, mu, sigma):
    M, N = I.shape
    gaussianNoise = np.random.normal(mu, sigma, (M, N))
    gaussianNoise = tools.to_uint8(gaussianNoise)
    I += gaussianNoise
    return I
def addRandomUniformNoise(I):
    M, N = I.shape
    noise = np.random.rand(M, N) * 60
    noise = tools.to_uint8(noise)  #
    I += noise
    return I
    gaussianNoise = np.random.normal(mu, sigma, (M, N))
    gaussianNoise = tools.to_uint8(gaussianNoise)
    I += gaussianNoise
    return I


def addSaltNPepperNoise(I, density):
    M, N = I.shape
    I[((np.random.rand(M, N)) < density)] = [0]
    I[((np.random.rand(M, N)) < density)] = [255]
    return I


#------------------------------BODY---------------------------------------

I = cv2.imread("lena.png")
I = cv2.cvtColor(I, cv2.COLOR_RGB2GRAY)

#Dst = BoxFilter(I,7)
#tools.show_Multiple_images_OpenCV(Oryginal=I, Filtered = Dst, Difference = cv2.absdiff(I, Dst))

#tools.show_1_image_OpenCV(addSaltNPepperNoise(I, 0.07))

Iun = addRandomUniformNoise(copy(I))
Igrn = addGaussianRandomNoise(copy(I), 0, 50)
Isp = addSaltNPepperNoise(copy(I), 0.07)

tools.show_Multiple_images_OpenCV(Iun=BoxFilter(Iun, 5),
                                  Igrn=BoxFilter(Igrn, 5),
                                  SnP=BoxFilter(I, 5))