python
from google.colab import files
from google.colab import drive
drive.mount('/content/drive')
python
import cv2 #opencv
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
python
img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
python
#Original image
plt.subplot(2,3,1)
plt.title("Original", fontsize=10)
plt.imshow(gray)
kernel = np.ones((3,3),np.uint8)
#Select the one with high brightness from the expanded kernel size area
plt.subplot(2,3,4)
plt.title("dilate", fontsize=10)
dst = cv2.dilate(gray,kernel,iterations = 1) #iterations: Number of expansions
plt.imshow(dst)
#Select the one with low brightness from the contraction kernel size area
plt.subplot(2,3,5)
plt.title("erode", fontsize=10)
dst = cv2.erode(gray,kernel,iterations = 1) #iterations: Number of shrinkages
plt.imshow(dst)
plt.show()
If used properly, it can be used for complementing missing values and removing noise.
Recommended Posts