# -*- coding: utf-8 -*-

import cv2
import numpy as np
import matplotlib.pyplot as plt

plt.close('all'); 
 
# Load the image
A = cv2.imread('p4.bmp')
L,C,D = np.shape(A)

A_rgb = cv2.cvtColor(A, cv2.COLOR_BGR2RGB)

# plt.figure(1)
# show 3 RGB bands
plt.subplot(341); plt.imshow(A_rgb[:,:,0],cmap='gray'); plt.title('Red')
plt.subplot(342); plt.imshow(A_rgb[:,:,1],cmap='gray'); plt.title('Green')
plt.subplot(343); plt.imshow(A_rgb[:,:,2],cmap='gray'); plt.title('Blue')
plt.subplot(344); plt.imshow(A_rgb); plt.title('RGB')


''' Histogrammes '''
hh=np.zeros(256)
for i in A_rgb[:,:,2]:
    hh[i]+=1;

for i in range(3):
    h = cv2.calcHist([A_rgb],[i],None,[256],[0,255])
    plt.subplot(3,4,(i+5)), plt.plot(h); 
    plt.subplot(3,4,8), plt.plot(h); 
    h = np.cumsum(h)
    plt.subplot(3,4,(i+9)), plt.plot(h/max(h)); 

''' Ealisation Histogrammes '''  
A_HSV = cv2.cvtColor(A, cv2.COLOR_BGR2HSV)
A_HSV[:,:,2] = cv2.equalizeHist(A_HSV[:,:,2]);
A_EQ = cv2.cvtColor(A_HSV, cv2.COLOR_HSV2RGB)

plt.figure(2)
# show 3 RGB bands
plt.subplot(341); plt.imshow(A_EQ[:,:,0],cmap='gray'); plt.title('Red')
plt.subplot(342); plt.imshow(A_EQ[:,:,1],cmap='gray'); plt.title('Green')
plt.subplot(343); plt.imshow(A_EQ[:,:,2],cmap='gray'); plt.title('Blue')

plt.subplot(344); plt.imshow(A_rgb); plt.title('RGB')
plt.subplot(3,4,8);plt.imshow(A_HSV[:,:,2],cmap='gray'); plt.title('V égalisé')
plt.subplot(3,4,12);plt.imshow(A_EQ); plt.title('RGB égalisé')


for i in range(3):
    h = cv2.calcHist([A_EQ],[i],None,[256],[0,255])
    plt.subplot(3,4,(i+5)), plt.stem(h); 
    h = np.cumsum(h)
    plt.subplot(3,4,(i+9)), plt.plot(h/max(h)); 

A_rgb_EQ  = np.empty((L,C,D),dtype=np.uint8); 

for i in range(3):
    A_rgb_EQ[:,:,i] = cv2.equalizeHist(A_rgb[:,:,i]);
    
plt.figure(3)   
plt.subplot(221); plt.imshow(A_rgb); plt.title('RGB')
plt.subplot(222); plt.imshow(A_rgb_EQ); plt.title('RGB Egalisé')
plt.subplot(223); plt.imshow(A_rgb); plt.title('RGB')
plt.subplot(224);plt.imshow(A_EQ); plt.title('RGB égalisé par HSV')


''' Filtrage '''
#A_rgb = A_rgb [1500:2000,0:500,:]
#L,C = np.shape(A_rgb); Br = np.random.randint(0,20,size=(L,C)); A_rgb=A_rgb+Br;  A_rgb = np.uint8(A_rgb)
k1 = np.array([[0, 0, 0], [0, 1, 0],  [0, 0, 0]]); A_k1= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k1)
k2 = np.array([[1, 1, 1], [1, 1, 1],  [1, 1, 1]])/9; A_k2= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k2); A_k2 = np.around(A_k2); A_k2 = np.uint8(A_k2)
#k2 = np.ones((5,5))/25; A_k2= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k2); A_k2 = np.around(A_k2); A_k2 = np.uint8(A_k2)
k3 = np.array([[1, 2, 1], [2, 4, 2],  [1, 2, 1]])/16; A_k3= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k3);A_k3 = np.around(A_k3); A_k3 = np.uint8(A_k3)
k4 = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]); A_k4= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k4); A_k4=255.0*A_k4/A_k4.max(); A_k4 = np.uint8(A_k4)

plt.figure(4)   
plt.subplot(221); plt.imshow(A_k1); plt.title('RGB')
plt.subplot(222); plt.imshow(A_k2); plt.title('Filtre moyenneur 3x3')
plt.subplot(223); plt.imshow(A_k3); plt.title('Filtre Gaussien 3x3')
plt.subplot(224); plt.imshow(A_k4); plt.title('Filtre Laplacien')


k11 = np.array([[-1, 0, 1], [-2, 0, 2],  [-1, 0, 1]]); A_k11= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k11); 
k12 = np.array([[-1, -2, -1], [0, 0, 0],  [1, 2, 1]]); A_k12= cv2.filter2D(src=A_rgb, ddepth=-1, kernel=k12); 
A_Sobel = np.sqrt(A_k11**2+A_k12**2); A_Sobel=255.0*A_Sobel/A_Sobel.max() ; A_Sobel = np.around(A_Sobel); A_Sobel = np.uint8(A_Sobel)

#A_k11=255.0*A_k11/A_k11.max(); A_k12=255.0*A_k12/A_k12.max()
plt.figure(5)   
plt.subplot(221); plt.imshow(A_k1); plt.title('RGB')
plt.subplot(222); plt.imshow(A_Sobel); plt.title('Filtre Sobel')
plt.subplot(223); plt.imshow(A_k11); plt.title('Gradient Horizontal')
plt.subplot(224); plt.imshow(A_k12); plt.title('Gradient Vertiictal')

  