Morphological Transformations are simple operations generally applied to binary images using convolution as a base to transform the image.
There are two basic operations: erosion and dilation. The other morphological transformations are a composition of those two operations and the image itself.
Erosion
The basic idea of erosion is to "erode" away the boundaries of the "white" image. It works by trying to expand the black area, consuming the foreground image edges. So, all the pixels near boundary will be discarded depending upon the size of kernel.
import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray
from skimage.morphology import erosion
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
image = skimage.util.invert(rgb2gray(io.imread('plate.png')))
plt.imshow(image, cmap='gray' )
plt.show()
kernel = np.ones((6,6),np.uint8)
ouimg = cv2.erode(image,kernel,iterations = 4)
plt.imshow(ouimg, cmap='gray')
plt.show()
The Original "Inverted" Image:
The Eroded Version
If your image is reversed, in other words, the background is white while the foreground is black, Erosion will thicker the numbers, instead of making them thinner. That's because Erosion tries to erode the white boundaries, which in this case will be the background. Please observe it in the images below:
import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray
from skimage.morphology import erosion
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
image = rgb2gray(io.imread('plate.png'))
plt.imshow(image, cmap='gray' )
plt.show()
kernel = np.ones((6,6),np.uint8)
ouimg = cv2.erode(image,kernel,iterations = 4)
plt.imshow(ouimg, cmap='gray')
plt.show()
The original image (not inverted)
The eroded version
Dilation Dilation is the opposing operation to Erosion. While Erosion will make any white boundaries thinner, Dilation will make then thicker.
import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray
from skimage.morphology import erosion
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
image = skimage.util.invert(rgb2gray(io.imread('plate.png')))
kernel = np.ones((6,6),np.uint8)
ouimg = cv2.dilate(image,kernel,iterations = 4)
plt.imshow(ouimg, cmap='gray')
plt.show()
import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray
from skimage.morphology import erosion
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
image = rgb2gray(io.imread('plate.png'))
plt.imshow(image, cmap='gray' )
plt.show()
kernel = np.ones((6,6),np.uint8)
ouimg = cv2.dilate(image,kernel,iterations = 4)
plt.imshow(ouimg, cmap='gray')
plt.show()
The dilated version of the non-inverted
Opening and Closing The opening operation is a composed operation in which we execute an erosion followed by a dilation. It's useful to clean up the image, removing noise from the black portion of the image.
The closing operation is the same thing as the opening one, except that now a dilation is first executed, then an erosion follows.
In the example below, we executed a closing operation in our non inverted image, combined with a pre-filtering loop applying a threshold for any pixels above 0.
import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray
from skimage.morphology import erosion
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
image = rgb2gray(io.imread('plate.png'))
plt.imshow(image, cmap='gray' )
plt.show()
for i in range(0, 3):
k = image[:, :] > 0
image[k] = 255
kernel = np.ones((6,6),np.uint8)
ouimg = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
plt.imshow(ouimg, cmap='gray')
plt.show()
The original image (not inverted)
The result image
Top hat
It's the difference between the input image and the "closed" version of the input image
Black hat
It's the difference between the "opened" version of the input image and the input image
Comentários
Postar um comentário