import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.image as mpimg

import cv2

%%bash
ls -l | grep .tiff

img = mpimg.imread('Lab_3_DIP.tiff')

plt.figure(figsize=(15,10))
plt.imshow(img)

"""
Explanation: Lab 3 - Morphological image processing <a class="tocSkip">
Import dependencies
End of explanation
"""


plt.figure(figsize=(20,20))


kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
dilation = cv2.dilate(erosion,kernel,iterations = 1)


plt.subplot(1,3,1),
plt.imshow(img),
plt.title('img')

plt.subplot(1,3,2),
plt.imshow(erosion),
plt.title('erosion(img, 1)')

plt.subplot(1,3,3),
plt.imshow(dilation),
plt.title('dilate(erosion(img, 1), 1)')

plt.figure(figsize=(20,30))


kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
dilation = cv2.dilate(erosion,kernel,iterations = 1)


plt.subplot(4,3,1),
plt.imshow(img),
plt.title('img')

plt.subplot(4,3,2),
plt.imshow(erosion),
plt.title('erosion(img, 1)')

plt.subplot(4,3,3),
plt.imshow(dilation),
plt.title('dilate(erosion(img, 1), 1)')



erosion2 = cv2.erode(img,kernel,iterations = 2)
erosion3 = cv2.erode(img,kernel,iterations = 3)
erosion4 = cv2.erode(img,kernel,iterations = 4)

plt.subplot(4,3,4),
plt.imshow(erosion2),
plt.title('erosion(img, 2)')

plt.subplot(4,3,5),
plt.imshow(erosion3),
plt.title('erosion(img, 3)')

plt.subplot(4,3,6),
plt.imshow(erosion4),
plt.title('erosion(img, 4)')


dilation2 = cv2.dilate(img,kernel,iterations = 2)
dilation3 = cv2.dilate(img,kernel,iterations = 3)
dilation4 = cv2.dilate(img,kernel,iterations = 4)

plt.subplot(4,3,7),
plt.imshow(dilation2),
plt.title('dilate(img, 2)')

plt.subplot(4,3,8),
plt.imshow(dilation3),
plt.title('dilate(img, 3)')

plt.subplot(4,3,9),
plt.imshow(dilation4),
plt.title('dilate(img, 4)')


dil_1_ero_2 = cv2.dilate(
    cv2.erode(img,kernel,iterations = 2)
    ,kernel,iterations = 1
)
dil_2_ero_1 = cv2.dilate(
    cv2.erode(img,kernel,iterations = 1)
    ,kernel,iterations = 2
)
dil_2_ero_2 = cv2.dilate(
    cv2.erode(img,kernel,iterations = 2)
    ,kernel,iterations = 2
)
plt.subplot(4,3,10),
plt.imshow(dil_1_ero_2),
plt.title('dilate(erosion(img, 2), 1)')

plt.subplot(4,3,11),
plt.imshow(dil_2_ero_1),
plt.title('dilate(erosion(img, 1), 2)')

plt.subplot(4,3,12),
plt.imshow(dil_2_ero_2),
plt.title('dilate(erosion(img, 2), 2)')


# plt.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0.1)
plt.show()

plt.figure(figsize=(70,40))

plt.subplot(1,3,1),
plt.imshow(dilation),
plt.title('dilate(erosion(img, 1), 1)')

plt.subplot(1,3,2),
plt.imshow(dil_2_ero_1),
plt.title('dilate(erosion(img, 1), 2)')

plt.subplot(1,3,3),
plt.imshow(dil_2_ero_2),
plt.title('dilate(erosion(img, 2), 2)')

"""
Explanation: Erosion / dilation steps
End of explanation
"""


