Theory
Today I would like to introduce some of the binary image processing methods that will be used during preprocessing or postprocessing. Depending on the data that we will apply the treatment method to match the appropriate filter it will help the image after processing to eliminate noise, light balance .. affect quality. of the photo. Today’s article I would like to introduce 4 methods:
- Erosion
- Dilation
- Opening
- Closing
Before going into learn we will look through the images below to get a glimpse of the processing of the above methods.
a-original image
b-dilation
c-erosion
e-opening
f-closing
We have the formula for determining erosion and dialation as follows:
With :
and
Where s is the pixel matrix of the image, f is a filter that can be an array or a binary matrix depending on the type of data we will choose the size of the fiter accordingly.
Erosion
Erosion, also known as erosion, is one of the two basic operators in mathematical morphology. It is often used in binary images but there are some versions that will be applied on gray images but for the purposes of my article today the focus is on binary images only.
The purpose of this method will help:
- Removes isolated noise pixels
- Removing noise pixels around the object helps make the edges (edges) of the object become smoother
- Removing the edge (edge) of an object helps the object become smaller and places those border pixels as the background layer of the object.
We have the following example:
Dilation
Dilation, or expansion, is the remaining operator mentioned above, as opposed to erosion that is also applied in binary images. The purpose of this method will help:
- With out-of-focus images can help stitch photos together
- With noise around the object becomes the border of the object
- Help to stand out the object in the image more
We have the following example:
Opening
Open = Erode next Dilate
With :
Closing
Close = Dilate next Erode With
We have the following visual image:
Depending on the data type and requirements of the problem, flexible methods will be applied.
Practice
Code come on, my most interesting part here
Erosion
1 2 3 4 5 | import cv2 from IPython.display import Image image = cv2.read(path, cv2.IMREAD_GRAYSCALE) #doc hinh anh |
1 2 3 4 5 6 7 | #ta có thể áp dụng các bộ lọc sepFilter2D(), filter2D(), blur(), boxFilter(), bilateralFilter(), medianBlur() #dưới đây mình xin áp dụng bộ lọc trung vi 3x3 img = cv2.medianBlur(image, 3) cv2.imwrite(path, img) #lưu ảnh vào đường dẫn Image(path) #hiển thị ảnh |
Dilation
1 2 3 4 | import cv2 from IPython.display import Image image = cv2.read(path, cv2.IMREAD_GRAYSCALE) #doc hinh anh |
1 2 3 4 | img = cv2.convertScaleAbs(image, 1.1, 5) cv2.imwrite(path, img) Image(path) |
References:
The article above was summarized when I attended the class of Mr. Dinh Viet Sang – lecturer of Hanoi University of Technology and some of the documents I refer to below:
- Library opencv
- Computer Vision: Algorithms and Applications, 2010 by Richard Szelisk
Thank you for following up on my article