To implement Erosion and Dilation using Python and OpenCV.
- Anaconda - Python 3.7
- OpenCV
Import the necessary pacakages.
Create the text using cv2.putText.
Create the structuring element.
Erode the image.
Dilate the Image.
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = np.zeros((100,400),dtype = 'uint8')
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img ,'CUTE',(60,70),font,2,(255),5,cv2.LINE_AA)
plt.imshow(img)
plt.axis('off')
kernel = np.ones((5,5),np.uint8)
kernel1 = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
cv2.erode(img,kernel)
img_erode = cv2.erode(img,kernel1)
plt.imshow(img_erode)
plt.axis('off')
img_dilate = cv2.dilate(img,kernel1)
plt.imshow(img_dilate)
plt.axis('off')
![326269651-83804a6e-7d26-4ea6-97e3-fa30e15eb268](https://private-user-images.githubusercontent.com/121166075/330433215-c837b553-8352-4bfb-aeb4-e60f675885f0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyODQzNDksIm5iZiI6MTcyNTI4NDA0OSwicGF0aCI6Ii8xMjExNjYwNzUvMzMwNDMzMjE1LWM4MzdiNTUzLTgzNTItNGJmYi1hZWI0LWU2MGY2NzU4ODVmMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQxMzM0MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mNzk4ZWRkZTQ1NWY1ODE3MTRkMGVkMTBiZGM2YWQ3OTMzMmFhNThlYTA2MjJjNDEwN2ZjZGE5MjM3ZDRjZWI1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.rFPL80Wi3bh6Ez54oWgRng7z0hiiihTF4fnQqgKgzc8)
![326269695-71e54801-29bb-4894-ad68-7d34ee364a9c](https://private-user-images.githubusercontent.com/121166075/330433349-e91cf755-7651-4dad-b3d5-03d2ceb91792.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyODQzNDksIm5iZiI6MTcyNTI4NDA0OSwicGF0aCI6Ii8xMjExNjYwNzUvMzMwNDMzMzQ5LWU5MWNmNzU1LTc2NTEtNGRhZC1iM2Q1LTAzZDJjZWI5MTc5Mi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQxMzM0MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xZTJkNTcxYTI5N2U1YWFiNGY0MmY5ZTgzZGY1MGJkOWE2MDcxMzQ2NDYyNjU2MDRiNWNjMzQ2NTYxZDlkZjNhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.qtBj2sNgFijFBl0fjaVp6C_5KWvgk96ig6XXOtfSXfQ)
![326269730-3579027b-a658-44f8-9c60-5a423c0ce6f9](https://private-user-images.githubusercontent.com/121166075/330433447-f837c1f8-f1cd-47a7-ac62-95ef647e94cf.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUyODQzNDksIm5iZiI6MTcyNTI4NDA0OSwicGF0aCI6Ii8xMjExNjYwNzUvMzMwNDMzNDQ3LWY4MzdjMWY4LWYxY2QtNDdhNy1hYzYyLTk1ZWY2NDdlOTRjZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQxMzM0MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yZWQ1ZDlkODE0ODc0ZGM1YWFhOTQ0OWU1NDllMzE3ZmM5NmZkYTVmNWZmNTA0NTY4NjViOWY1ZmFhMWU3ZGJkJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.dKTjHG-l5qTe6cztWk1htpxx8M2hPchizIMIfpqHv6U)
Thus the generated text image is eroded and dilated using python and OpenCV.