<jupyter_start><jupyter_text><jupyter_code>!wget https://media.istockphoto.com/vectors/set-of-green-leaves-with-different-shapes-vector-id1174864605
!wget https://dr282zn36sxxg.cloudfront.net/datastreams/f-d%3A49ce4ff7728fa85c6370cbfcf45b12d1275f37ffa997418cc3286d91%2BIMAGE_THUMB_POSTCARD_TINY%2BIMAGE_THUMB_POSTCARD_TINY.1
!wget https://www.researchgate.net/profile/Alberto-Cereser/publication/312605716/figure/fig31/AS:453724666634242@1485187798120/Erosion-and-dilation-are-two-basic-morphologic-operations-used-to-process-the-shapes.png
# Python program to demonstrate erosion and 
# dilation of images.
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
  
# Reading the input image
img = cv2.imread('/content/set-of-green-leaves-with-different-shapes-vector-id1174864605', 0)

############
bordersize = 10
img = cv2.copyMakeBorder(
    img,
    top=bordersize,
    bottom=bordersize,
    left=bordersize,
    right=bordersize,
    borderType=cv2.BORDER_CONSTANT,
    value=[10, 10, 10]
)
#########################
  
# Taking a matrix of size 5 as the kernel
kernel = np.ones((10,10), np.uint8)

# custom kernel
# kernel = np.array([[0, 0, 1, 0, 0],
#                    [0, 1, 1, 1, 0], 
#                    [1, 1, 1, 1, 1],
#                    [0, 1, 1, 1, 0], 
#                    [0, 0, 1, 0, 0]], np.uint8)
# print(kernel)
# The first parameter is the original image,
# kernel is the matrix with which image is 
# convolved and third parameter is the number 
# of iterations, which will determine how much 
# you want to erode/dilate a given image. 
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)


Hori = np.concatenate((img, img_erosion, img_dilation), axis=1)

cv2_imshow(Hori)
  
# opening 
# Opening removes small objects from the foreground (usually taken as the bright pixels) of an image,
# placing them in the background

img_erosion = cv2.erode(img, kernel, iterations=1)
img_opening = cv2.dilate(img_erosion, kernel, iterations=1)
img_opening = cv2.resize(img_opening, (400, 300)) 
cv2_imshow(img_opening)

# closing 
# closing removes small holes in the foreground,
# changing small islands of background into foreground

img_dilation = cv2.dilate(img, kernel, iterations=1)
img_closing = cv2.erode(img_dilation, kernel, iterations=1)
img_closing = cv2.resize(img_closing, (400, 300)) 

cv2_imshow(img_closing)
!wget https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTiX1LilsMKabgyceebVe33YpJFFlXp22x4lA&usqp=CAU
img3 = cv2.imread('/content/images?q=tbn:ANd9GcTiX1LilsMKabgyceebVe33YpJFFlXp22x4lA', 0)
img3 = cv2.resize(img3, (300, 300)) 


kernel3 = np.ones((5,5), np.uint8)
# kernel3 = np.array([[0, 0, 1, 0, 0],
#                    [0, 1, 1, 1, 0], 
#                    [1, 1, 1, 1, 1],
#                    [0, 1, 1, 1, 0], 
#                    [0, 0, 1, 0, 0]], np.uint8)
img3_erosion = cv2.erode(img3, kernel3, iterations=1)
img3_dilation = cv2.dilate(img3, kernel3, iterations=1)


print("---------------Original---------------|---------------Erosion----------------|---------------Dialation-----------------")
Hori3 = np.concatenate((img3, img3_erosion, img3_dilation), axis=1)
cv2_imshow(Hori3)


# opening 
# Opening removes small objects from the foreground (usually taken as the bright pixels) of an image,
# placing them in the background

img3_erosion = cv2.erode(img3, kernel3, iterations=1)
img3_opening = cv2.dilate(img3_erosion, kernel3, iterations=1)


# closing 
# closing removes small holes in the foreground,
# changing small islands of background into foreground

img3_dilation = cv2.dilate(img3, kernel3, iterations=1)
img3_closing = cv2.erode(img3_dilation, kernel3, iterations=1)

print("---------------Original---------------|---------------Opening----------------|---------------Closing-----------------")
Hori3_b = np.concatenate((img3, img3_opening, img3_closing), axis=1)
cv2_imshow(Hori3_b)
!wget https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTiX1LilsMKabgyceebVe33YpJFFlXp22x4lA&usqp=CAU
img1 = cv2.imread('/content/11042_2020_8636_Fig1_HTML.png', 0)
cv2_imshow(img1)
img4 = cv2.imread('/content/D52E65AA-946F-44E0-9246-044B4C9BA132.jpeg', 0)
img4 = cv2.resize(img4, (300, 100)) 


kernel3 = np.ones((5,5), np.uint8)
# kernel4 = np.array([[0, 0, 1, 0, 0],
#                    [0, 1, 1, 1, 0], 
#                    [1, 1, 1, 1, 1],
#                    [0, 1, 1, 1, 0], 
#                    [0, 0, 1, 0, 0]], np.uint8)
img4_erosion = cv2.erode(img4, kernel3, iterations=1)
img4_dilation = cv2.dilate(img4, kernel3, iterations=1)


print("---------------Original---------------|---------------Erosion----------------|---------------Dilation-----------------")
Hori4 = np.concatenate((img4, img4_erosion, img4_dilation), axis=1)
cv2_imshow(Hori4)
# opening 
# Opening removes small objects from the foreground (usually taken as the bright pixels) of an image,
# placing them in the background

img4_erosion = cv2.erode(img4, kernel4, iterations=1)
img4_opening = cv2.dilate(img4_erosion, kernel4, iterations=1)


# closing 
# closing removes small holes in the foreground,
# changing small islands of background into foreground

img4_dilation = cv2.dilate(img4, kernel4, iterations=1)
img4_closing = cv2.erode(img4_dilation, kernel4, iterations=1)

print("---------------Original---------------|---------------Opening----------------|---------------Closing-----------------")
Hori4_b = np.concatenate((img4, img4_opening, img4_closing), axis=1)
cv2_imshow(Hori4_b)

!wget https://media.springernature.com/lw685/springer-static/image/art%3A10.1007%2Fs11042-020-08636-9/MediaObjects/11042_2020_8636_Fig1_HTML.png<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18700614574187885, 'fraction_numerical': 0.0800702370500439, 'mean_word_length': 3.200589970501475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '597484', 'license': 'MIT', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2072, 'n_words': 434, 'path': 'Erosion_and_Dilation.ipynb', 'repo_name': 'alirezash97/Computer-Vision', 'size': '5695'}